Commit 91d472acfcb2d46280c78c9ff3e5d8c7e7d82de3

Authored by David Mayerich
1 parent 41e2f5ab

added file_size function and a function utility directory

Showing 2 changed files with 33 additions and 18 deletions   Show diff stats
stim/envi/envi.h
... ... @@ -7,6 +7,7 @@
7 7 #include "../envi/bil.h"
8 8 #include "../math/fd_coefficients.h"
9 9 #include <stim/parser/filename.h>
  10 +#include <stim/util/filesize.h>
10 11 #include <iostream>
11 12 #include <fstream>
12 13 //#include "../image/image.h"
... ... @@ -83,22 +84,8 @@ public:
83 84 return true;
84 85 }
85 86  
86   - static size_t file_size(std::string filename){
87   - FILE *p_file = NULL;
88   - p_file = fopen(filename.c_str(),"rb");
89   - if(!p_file){
90   - std::cout<<"ERROR: could not open "<<filename<<std::endl;
91   - exit(1);
92   - }
93   - fseek(p_file,0,SEEK_END);
94   - size_t size = ftell(p_file);
95   - fclose(p_file);
96   - return size;
97   - }
98   -
99 87 //test to determine if the specified file is an ENVI file
100 88 static bool is_envi(std::string fname, std::string hname = ""){
101   - return true;
102 89 stim::filename data_file(fname);
103 90 stim::filename header_file;
104 91 if(hname == ""){ //if the header isn't provided
... ... @@ -111,10 +98,7 @@ public:
111 98 if(H.load(header_file) == false) //load the header file, if it doesn't load return false
112 99 return false;
113 100 size_t targetBytes = H.data_bytes(); //get the number of bytes that SHOULD be in the data file
114   - //std::ifstream infile(data_file.str(), std::ifstream::binary); //load the data file
115   - //if(!infile) return false; //if the data file doesn't load, return false
116   - //size_t bytes = infile.tellg(); //get the number of actual bytes in the file
117   - size_t bytes = file_size(fname);
  101 + size_t bytes = stim::file_size(fname);
118 102 if(bytes != targetBytes) return false; //if the data doesn't match the header, return false
119 103 return true; //otherwise everything looks fine
120 104  
... ...
stim/util/filesize.h 0 → 100644
  1 +#ifndef STIM_UTIL_FILESIZE_H
  2 +#define STIM_UTIL_FILESIZE_H
  3 +
  4 +#ifdef _WIN32
  5 +#include <Windows.h>
  6 +#endif
  7 +
  8 +namespace stim{
  9 +static size_t file_size(std::string filename){
  10 +#ifdef _WIN32
  11 + HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  12 + if(hFile == INVALID_HANDLE_VALUE) return 0;
  13 + LARGE_INTEGER size;
  14 + if(!GetFileSizeEx(hFile, &size)){
  15 + CloseHandle(hFile);
  16 + return 0;
  17 + }
  18 + CloseHandle(hFile);
  19 + return (size_t)size.QuadPart;
  20 +#elif
  21 +
  22 +
  23 +
  24 +#endif
  25 +}
  26 +
  27 +} //end namespace stim
  28 +
  29 +
  30 +
  31 +#endif
0 32 \ No newline at end of file
... ...