Commit a11c7efe948d0839451ca85779c1e2bb85ead1f4

Authored by David Mayerich
1 parent b5c53037

fixed stim::filename error for Windows root directories

Showing 2 changed files with 42 additions and 61 deletions   Show diff stats
stim/math/matrix.h
... ... @@ -21,7 +21,7 @@ namespace stim{
21 21 mat4_float //floating point type, determined automatically
22 22 };
23 23  
24   - size_t mat4Format_size(mat4Format f){
  24 + static size_t mat4Format_size(mat4Format f){
25 25 switch(f){
26 26 case mat4_float64: return 8;
27 27 case mat4_float32:
... ... @@ -33,7 +33,7 @@ namespace stim{
33 33 }
34 34 }
35 35  
36   - void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){
  36 + static void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){
37 37 //save the matrix file here (use the mat4 function above)
38 38 //data format: https://maxwell.ict.griffith.edu.au/spl/matlab-page/matfile_format.pdf (page 32)
39 39  
... ... @@ -43,11 +43,11 @@ namespace stim{
43 43 int p = format;
44 44 int t = 0;
45 45 MOPT = m * 1000 + o * 100 + p * 10 + t; //calculate the type value
46   - int mrows = sx;
47   - int ncols = sy;
  46 + int mrows = (int)sx;
  47 + int ncols = (int)sy;
48 48 int imagf = 0; //assume real (for now)
49 49 varname.push_back('\0'); //add a null to the string
50   - int namlen = varname.size(); //calculate the name size
  50 + int namlen = (int)varname.size(); //calculate the name size
51 51  
52 52 size_t bytes = sx * sy * mat4Format_size(format);
53 53 std::ofstream outfile(filename, std::ios::binary);
... ... @@ -59,7 +59,6 @@ namespace stim{
59 59 outfile.write((char*)&varname[0], namlen);
60 60 outfile.write((char*)data, bytes); //write the matrix data
61 61 outfile.close();
62   -
63 62 }
64 63  
65 64 template <class T>
... ... @@ -421,35 +420,6 @@ public:
421 420 }
422 421 }
423 422 stim::save_mat4((char*)M, filename, name, rows(), cols(), format);
424   - /*int MOPT = 0; //initialize the MOPT type value to zero
425   - int m = 0; //little endian
426   - int o = 0; //reserved, always 0
427   - int p = mat4_float;
428   - if (mat4_float) {
429   - if (sizeof(T) == 4) p = mat4_float32;
430   - else if (sizeof(T) == 8) p = mat4_float64;
431   - else {
432   - std::cout << "stim::matrix ERROR - incorrect format specified" << std::endl;
433   - exit(1);
434   - }
435   - }
436   - int t = 0;
437   - MOPT = m * 1000 + o * 100 + p * 10 + t; //calculate the type value
438   - int mrows = rows();
439   - int ncols = cols();
440   - int imagf = 0; //assume real (for now)
441   - name += "\0"; //add a null to the string
442   - int namlen = name.size(); //calculate the name size
443   -
444   - std::ofstream outfile(filename, std::ios::binary);
445   - outfile.write((char*)&MOPT, 4);
446   - outfile.write((char*)&mrows, 4);
447   - outfile.write((char*)&ncols, 4);
448   - outfile.write((char*)&imagf, 4);
449   - outfile.write((char*)&namlen, 4);
450   - outfile.write((char*)&name[0], namlen);
451   - outfile.write((char*)M, bytes()); //write the matrix data
452   - outfile.close();*/
453 423 }
454 424 };
455 425  
... ...
stim/parser/filename.h
... ... @@ -65,44 +65,46 @@ protected:
65 65 std::string current_drive;
66 66 std::vector<std::string> current_dir;
67 67 parse_path(current_drive, current_dir, current); //get the current drive and directories
  68 + if (current_dir.size() > 0) {
  69 +
  70 + // step through each directory in the relative path, adjusting the current directory
  71 + // index depending on whether the relative path is specified with '.' or '..'
  72 + int current_i = (int)current_dir.size() - 1;
  73 + int relative_i;
  74 + for (relative_i = 0; relative_i < relative.size(); relative_i++) {
  75 + if (relative[relative_i] == "..") current_i--;
  76 + else if (relative[relative_i] != ".") break;
  77 + }
  78 + if (current_i < 0) {
  79 + std::cerr << "ERROR stim::filepath - relative path is incompatible with working directory" << std::endl;
  80 + exit(1);
  81 + }
68 82  
69   - // step through each directory in the relative path, adjusting the current directory
70   - // index depending on whether the relative path is specified with '.' or '..'
71   - int current_i = (int)current_dir.size() - 1;
72   - int relative_i;
73   - for(relative_i = 0; relative_i < relative.size(); relative_i++){
74   - if(relative[relative_i] == "..") current_i--;
75   - else if(relative[relative_i] != ".") break;
76   - }
77   - if(current_i < 0){
78   - std::cerr<<"ERROR stim::filepath - relative path is incompatible with working directory"<<std::endl;
79   - exit(1);
80   - }
81   -
82   - absolute.clear();
83   - for(size_t i = 0; i <= current_i; i++){
84   - absolute.push_back(current_dir[i]);
85   - }
86   - for(size_t i = relative_i; i < relative.size(); i++){
87   - absolute.push_back(relative[i]);
  83 + absolute.clear();
  84 + for (size_t i = 0; i <= current_i; i++) {
  85 + absolute.push_back(current_dir[i]);
  86 + }
  87 + for (size_t i = relative_i; i < relative.size(); i++) {
  88 + absolute.push_back(relative[i]);
  89 + }
88 90 }
89 91 }
90 92  
91 93 /// Parses a directory string into a drive (NULL if not Windows) and list of hierarchical directories
92 94 /// Returns true if the path is relative, false if it is absolute
93 95 void parse_path(std::string &drive, std::vector<std::string> &absolute, std::string dir){
94   - drive = ""; //initialize the drive to NULL (it will stay that way for Windows)
  96 + drive = ""; //initialize the drive to NULL (it will stay that way for Windows)
95 97 std::vector<std::string> path;
96   - bool relative = true; //the default path is relative
  98 + bool relative = true; //the default path is relative
97 99  
98   - if(dir.length() == 0) return; //return if the file locator is empty
  100 + if(dir.length() == 0) return; //return if the file locator is empty
99 101 std::string unix_dir = unix_div(dir); //replace any Windows division characters with Unix
100 102  
101 103 if(unix_dir.length() > 1 && unix_dir[1] == ':'){ //if a drive identifier is given
102 104 if(unix_dir[0] > 64 && unix_dir[0] < 91) //if the drive letter is upper case
103   - _drive = unix_dir[0] + 32; //convert it to lower case
  105 + drive = unix_dir[0] + 32; //convert it to lower case
104 106 else if(unix_dir[0] > 96 && unix_dir[0] < 123) //if the drive character is lower case
105   - _drive = unix_dir[0]; //store it as-is
  107 + drive = unix_dir[0]; //store it as-is
106 108 else{ //otherwise throw an error
107 109 std::cerr<<"ERROR stim::filename - drive letter is invalid: "<<unix_dir[0]<<std::endl;
108 110 exit(1);
... ... @@ -113,11 +115,13 @@ protected:
113 115 if(drive.size() != 0){
114 116 relative = false;
115 117 }
116   - if(unix_dir.size() > 0){ //if there is a directory specified, remove surrounding slashes
117   - if(unix_dir[0] == '/'){ //if there is a leading slash
  118 + if (unix_dir.size() > 0) { //if there is a directory specified, remove surrounding slashes
  119 + if (unix_dir[0] == '/') { //if there is a leading slash
118 120 relative = false; //the path is not relative
119 121 unix_dir = unix_dir.substr(1, unix_dir.length() - 1); //remove the slash
120 122 }
  123 + }
  124 + if(unix_dir.size() > 0){
121 125 if(unix_dir[unix_dir.size()-1] == '/')
122 126 unix_dir = unix_dir.substr(0, unix_dir.length() - 1);
123 127 }
... ... @@ -266,6 +270,13 @@ public:
266 270 return result;
267 271 }
268 272  
  273 + std::string fname(){
  274 + std::string result = prefix();
  275 + result += ".";
  276 + result += extension();
  277 + return result;
  278 + }
  279 +
269 280 /// create a matching file locator with the path changed to s
270 281 stim::filename path(std::string s){
271 282 stim::filename result = *this;
... ...