00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "MathIO.hpp"
00027
00028
00029
00030
00031
00032 #include <cstdlib>
00033
00034
00035
00036
00037 namespace kn {
00038
00040 template void loadMatrix(Matrix<double>&, const std::string &);
00041 template void loadMatrix(Matrix<float>&, const std::string &);
00042 template void loadMatrix(Matrix<int>&, const std::string &);
00043
00044 template void exportMatrix(const Matrix<double>&, const std::string &, const bool &, const std::string &);
00045 template void exportMatrix(const Matrix<float>&, const std::string &, const bool &, const std::string &);
00046 template void exportMatrix(const Matrix<int>&, const std::string &, const bool &, const std::string &);
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 bool skipEmptyLines(std::ifstream &dataFile){
00058
00059 std::streampos currentPos;
00060 std::string data;
00061 bool stillEmpty;
00062 bool emptyLineRead = false;
00063
00064 do{
00065 currentPos = dataFile.tellg();
00066 std::getline(dataFile,data);
00067
00068
00069 stillEmpty = true;
00070 unsigned int i = 0;
00071 while(i<data.size()){
00072 if(data[i]!=' ' && data[i]!='\t'){
00073 stillEmpty = false;
00074 i = data.size();
00075 }
00076 ++i;
00077 }
00078
00079 if(stillEmpty) emptyLineRead = true;
00080 } while (stillEmpty);
00081
00082
00083 dataFile.seekg(currentPos);
00084
00085 return emptyLineRead;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 bool skipComments(std::ifstream &dataFile){
00095
00096 std::streampos currentPos;
00097 std::string comments;
00098 bool stillComment;
00099 bool commentsRead = false;
00100 do{
00101
00102 currentPos = dataFile.tellg();
00103 std::getline(dataFile,comments);
00104
00105
00106 unsigned int i = 0;
00107 while( (comments[i]==' ' || comments[i]=='\t') && i<comments.size())
00108 ++i;
00109
00110
00111 stillComment = false;
00112 if(comments[i]=='#'){
00113 stillComment = true;
00114 commentsRead = true;
00115 }
00116 } while (stillComment);
00117
00118
00119 dataFile.seekg(currentPos);
00120
00121 return commentsRead;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 bool readSize(std::ifstream &dataFile, const std::string &keyword, double &value){
00133
00134
00135 std::streampos currentPos = dataFile.tellg();
00136
00137
00138 std::string data;
00139 std::getline(dataFile,data);
00140
00141
00142 if(data.compare(0,keyword.size(),keyword) != 0){
00143 dataFile.seekg(currentPos);
00144 return false;
00145 }
00146
00147
00148 data = data.substr(keyword.size()+1,data.size()-(keyword.size()+1));
00149 const char *nptr = data.c_str();
00150 char *endptr = 0;
00151 value = strtod(nptr,&endptr);
00152 if(endptr == nptr || endptr == 0){
00153 dataFile.seekg(currentPos);
00154 return false;
00155 }
00156
00157
00158 return true;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168 bool readMatrixHeader(std::ifstream &matrixFile, unsigned int &row, unsigned int &column){
00169
00170
00171 while(skipComments(matrixFile) || skipEmptyLines(matrixFile));
00172
00173
00174 double value = 0.0;
00175 if(!readSize(matrixFile,"row",value))
00176 return false;
00177 row = (unsigned int) value;
00178
00179
00180 while(skipComments(matrixFile) || skipEmptyLines(matrixFile));
00181
00182
00183 if(!readSize(matrixFile,"col",value))
00184 throw MathException("readMatrixHeader : invalid format : 'col' expected");
00185 column = (unsigned int) value;
00186
00187
00188 while(skipComments(matrixFile) || skipEmptyLines(matrixFile));
00189
00190 return true;
00191 }
00192
00193
00194
00195
00196
00197 }
00198