Waterflow
Visualize water in terrain
src/fileHandler.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 #include "fileHandler.h"
00005 #include <iostream>
00006 
00007 // ===== Constructor =====
00008 
00009 FileHandler::FileHandler(int dWidth, int dHeight)
00010 {
00011     fArray = nullptr;
00012     width = dWidth;
00013     height = dHeight;
00014     arrayLength = width * height;   
00015 }
00016 
00017 // ===== Methods =====
00018 
00019 void FileHandler::SaveData(std::string path)
00020 {
00021     std::ofstream outFile;//(path,std::fstream::trunc);
00022     outFile.open(path, std::fstream::binary | std::fstream::trunc | std::fstream::out);
00023     if(!outFile.is_open()){
00024         std::cerr << "File not opened for saving" << std::endl;
00025     }
00026     
00027     int32_t w32 = width;
00028     int32_t h32 = height;
00029     char const * w = reinterpret_cast<char const *>(&w32);
00030     char const * h = reinterpret_cast<char const *>(&h32);
00031     outFile.write(w, 4);
00032     outFile.write(h, 4);
00033     
00034     
00035     for (int n = 0; n < arrayLength; n++)
00036     {
00037         char const * p = reinterpret_cast<char const *>(&fArray[n]);
00038         outFile.write(p, 4);
00039     }
00040     
00041     outFile.close();
00042 }
00043 
00044 int FileHandler::LoadData(std::string path)
00045 {
00046     std::ifstream inFile;
00047     inFile.open(path, std::ios::ate);
00048     // Returns -1 if file cannot be read.
00049     if (!inFile.is_open())
00050     {
00051         return -1;
00052     }
00053     int bCount = ((int(inFile.tellg())) - 2 * sizeof(int32_t));
00054     arrayLength = bCount / 4;
00055     fArray = new float[arrayLength];
00056 
00057     inFile.close();
00058     inFile.open(path, std::ios_base::binary);
00059 
00060     char buf[4];
00061     int32_t whTemp;
00062     
00063     inFile.read(buf, 4);
00064     memcpy(&whTemp, &buf, sizeof(whTemp));
00065     if ( width != whTemp) {
00066         std::cerr << "Load data width doesn't match DEM data width" << std::endl;
00067         return -1;
00068     }
00069     
00070     width = whTemp;
00071     
00072     inFile.read(buf, 4);
00073     memcpy(&whTemp, &buf, sizeof(whTemp));
00074     
00075     if ( height != whTemp) {
00076         std::cerr << "Load data height doesn't match DEM data height" << std::endl;
00077         return -1;
00078     }
00079     
00080     height = whTemp;
00081 
00082     int n = 0;
00083     while (n < arrayLength)
00084     {
00085         inFile.read(buf, 4);
00086         memcpy(&fArray[n], &buf, 4);
00087         n++;
00088     }
00089     return 0;
00090 }
00091 
00092 // ===== Getters =====
00093 
00094 float** FileHandler::GetArray()
00095 {
00096     return &fArray;
00097 }
00098 
00099 int FileHandler::GetArrayLength()
00100 {
00101     return arrayLength;
00102 }
00103 
00104 int FileHandler::GetDataWidth()
00105 {
00106     return width;
00107 }
00108 int FileHandler::GetDataHeight()
00109 {
00110     return height;
00111 }
 All Classes Files Functions Variables Enumerations