00001 #ifndef RepositoryMapcodeFile_H 00002 #define RepositoryMapcodeFile_H 00003 00004 00010 #include <stdio.h> 00011 #include "xfile.h" 00012 #include <string> 00013 #include "TokenFinder.h" 00014 #include "WordIter.h" 00015 #include "TokenIter.h" 00016 00017 #include "IMapcodeRepository.h" 00018 #include "Mapcode.h" 00019 #include "defs.h" 00020 00021 00022 00023 class RepositoryMapcodeFile : public IMapcodeRepository { 00024 public: 00026 void save( const Mapcode * mapcode, const string fileName ); 00027 00029 Mapcode * load( string filenName ); 00030 }; 00031 00032 void 00033 RepositoryMapcodeFile::save( const Mapcode * mapcode, const string fileName ) { 00034 long storage_type = 0; 00035 string buffer; 00036 long lSize; 00037 int i,j; 00038 00039 00040 CxFile dataf( storage_type ); 00041 dataf.Open( fileName.c_str(), "w" ); 00042 00043 int d = mapcode->getNumVar(); 00044 int xDim = mapcode->getDimensions(0); 00045 int yDim = mapcode->getDimensions(1); 00046 int l = mapcode->getLattice(); 00047 int n = mapcode->getNeighborType(); 00048 int m = mapcode->getMapSize(); 00049 00050 buffer = inttostr( d ) + ""; 00051 buffer += " " + inttostr( xDim ) + " " + inttostr( yDim ) + " "; 00052 buffer += " " + inttolattice( l ) + " " + inttoneigh( n ) + "\n"; 00053 00054 00055 for (i=1; i<= m; i++ ) { 00056 for (j=1; j<= d; j++ ) 00057 buffer += " " + flttostr( mapcode->getCodebook()[i][j] ) + " "; 00058 buffer += "\n"; 00059 } 00060 00061 lSize = buffer.size(); 00062 dataf.Write( buffer.c_str(), lSize, 1 ); 00063 dataf.Close(); 00064 00065 }; 00066 00067 Mapcode * 00068 RepositoryMapcodeFile::load( string fileName ) { 00069 long storage_type = 0; 00070 long lSize; 00071 char* buffer; 00072 typedef vector<float> VecInt; 00073 typedef vector< VecInt > MatrixInt; 00074 TvecLabel lvec; 00075 MatrixInt mat; 00076 VecInt vec; 00077 MatrixInt::iterator im; 00078 VecInt::iterator iv; 00079 00080 int dimension, xDim, yDim; 00081 string neighboor_str, lattice_str; 00082 00083 int cont, contd; 00084 00085 00086 CxFile dataf( storage_type ); 00087 dataf.Open( fileName.c_str(), "rb" ); 00088 00089 lSize = dataf.Size(); 00090 00091 buffer = (char*) malloc (lSize); 00092 00093 if (buffer == NULL) exit (2); 00094 00095 dataf.Read( buffer, 1, lSize ); 00096 string data( buffer ); 00097 00098 token::WordIter wi(data); 00099 00100 cont = 0; contd = 1; 00101 while (wi != token::WordIter()) 00102 { 00103 if ( isValid( *wi ) ) { 00104 if (cont==0) 00105 { 00106 dimension = strtoint( *wi ); 00107 } 00108 else 00109 if ( cont == 1 ) 00110 { 00111 xDim = strtoint( *wi ); 00112 } 00113 else 00114 if ( cont == 2 ) 00115 { 00116 yDim = strtoint( *wi ); 00117 } 00118 else 00119 if ( cont == 3 ) 00120 { 00121 lattice_str = *wi ; 00122 } 00123 else 00124 if ( cont == 4 ) 00125 { 00126 neighboor_str = *wi ; 00127 } 00128 else 00129 { 00130 if ( contd == dimension ) 00131 { 00132 contd = 1; 00133 vec.push_back( strtofloat(*wi) ); 00134 mat.push_back( vec ); 00135 vec.clear(); 00136 } 00137 else 00138 if ( isNumeric( *wi ) ) 00139 { 00140 vec.push_back( strtofloat(*wi) ); 00141 contd++; 00142 } 00143 else 00144 lvec.push_back( *wi ); 00145 } 00146 cont++; 00147 } 00148 ++wi; 00149 } 00150 00151 TMatrix mdata = create_matrix( 1, mat.size(), 1, dimension ); 00152 int i, j; i = j = 1; 00153 00154 for (im=mat.begin(); im<mat.end(); im++) { 00155 j = 1; 00156 for (iv=(*im).begin(); iv < (*im).end(); iv++) 00157 { 00158 mdata[i][j] = *iv; 00159 j++; 00160 } 00161 i++; 00162 } 00163 00164 Mapcode * mapcode_data = new Mapcode; 00165 00166 00167 mapcode_data->setNumVar( dimension ); 00168 mapcode_data->setDimensions(0, xDim ); 00169 mapcode_data->setDimensions(1, yDim ); 00170 mapcode_data->setDimension( DTWO ); 00171 mapcode_data->setCodebook( mdata ); 00172 mapcode_data->setLattice( lattice_str ); 00173 mapcode_data->setTopolParamsText( "Two" ); 00174 mapcode_data->setNeighborType( neighboor_str ); 00175 00176 00177 free(buffer); 00178 dataf.Close(); 00179 00180 return mapcode_data; 00181 }; 00182 00183 #endif