Changeset 1558 in Sophya for trunk/SophyaLib/TArray
- Timestamp:
- Jul 2, 2001, 6:14:32 PM (24 years ago)
- Location:
- trunk/SophyaLib/TArray
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/TArray/tarray.cc
r1550 r1558 1043 1043 /*! 1044 1044 \param is : input stream (ASCII) 1045 */ 1046 template <class T> 1047 void TArray<T>::ReadASCII(istream& is) 1045 \param nr : Number of non empty (or comment) lines in stream (return value) 1046 \param nc : Number of columns (= ntot/nlines) (return value) 1047 \return Number of decoded elements 1048 */ 1049 template <class T> 1050 sa_size_t TArray<T>::ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc) 1048 1051 { 1049 1052 EnumeratedSequence es; 1050 sa_size_t nr, nc; 1051 es.FillFromFile(is, nr, nc); 1053 sa_size_t n = es.FillFromFile(is, nr, nc); 1054 if ( (n < 1) || (nr < 1) || (nc < 1) ) return(n); 1055 if (!IsAllocated()) { 1056 sa_size_t sz[2]; 1057 if (arrtype_ == 2) { // C'est un vecteur 1058 sz[0] = sz[1] = 1; 1059 sz[veceli_] = n; 1060 } 1061 else { 1062 sz[RowsKA()] = nr; 1063 sz[ColsKA()] = nc; 1064 } 1065 ReSize(2, sz); 1066 } 1067 SetSeq(es); 1068 cout << "TArray<T>::ReadASCII()/Info: " << n << " elements read from stream " 1069 << " (Row,Col= " << nr << "," << nc << ")" << endl; 1070 return(n); 1052 1071 } 1053 1072 … … 1055 1074 /*! 1056 1075 \param os : output stream (ASCII) 1076 \sa Print 1057 1077 */ 1058 1078 template <class T> -
trunk/SophyaLib/TArray/tarray.h
r1550 r1558 182 182 183 183 // Lecture,Ecriture sur fichier ASCII 184 virtual void ReadASCII(istream& is);184 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc); 185 185 virtual void WriteASCII(ostream& os) const; 186 186 -
trunk/SophyaLib/TArray/utilarr.cc
r1550 r1558 112 112 for(int k=0; k<8; k++) 113 113 cout << " k= " << k << " es(k)= " << es(k) << endl; 114 115 // Decoding a sequence from a string 116 EnumeratedSequence ess; 117 int nbad; 118 ess.Append("56.5 (1.,-1.) 4 8 16", nbad); 119 cout << ess; 114 120 \endcode 115 121 */ 116 122 123 //! Default constructor 117 124 EnumeratedSequence::EnumeratedSequence() 118 125 { 126 } 127 128 //! Copy constructor 129 EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es) 130 { 131 Append(es); 119 132 } 120 133 … … 131 144 } 132 145 146 //! Appends a new value to the sequence 133 147 EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v) 134 148 { … … 137 151 } 138 152 153 //! Initialize the sequence with a single value \b v 139 154 EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v) 140 155 { … … 144 159 } 145 160 146 EnumeratedSequence & EnumeratedSequence::Merge(EnumeratedSequence const & seq) 147 { 148 for(int k=0; k<seq.vecv_.size(); k++) 149 vecv_.push_back(seq.vecv_[k]); 150 return(*this); 151 } 152 161 //! Copy operator 162 EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq) 163 { 164 Clear(); 165 Append(seq); 166 return(*this); 167 } 168 169 170 //! Prints the list to the output stream \b os 153 171 void EnumeratedSequence::Print(ostream& os) const 154 172 { … … 159 177 else os << " " ; 160 178 } 179 os << endl; 161 180 return; 162 181 } 163 182 164 sa_size_t EnumeratedSequence::FillFromString(string const & str) 165 { 166 cerr << " EnumeratedSequence::FillFromString() pas implemente !" << endl; 167 return (0); 168 } 169 183 //! Append the \b seq to the end of the sequence. 184 /*! 185 \return the number of added elements. 186 */ 187 sa_size_t EnumeratedSequence::Append(EnumeratedSequence const & seq) 188 { 189 for(int k=0; k<seq.vecv_.size(); k++) 190 vecv_.push_back(seq.vecv_[k]); 191 return(seq.vecv_.size()); 192 } 193 194 //! Decodes the string, appending values to the end of the sequence. 195 /*! 196 \param str : string to be decoded 197 \param nbad : number of unmatched quotes or parenthesis 198 \return the number of added elements. 199 */ 200 sa_size_t EnumeratedSequence::Append(string const & str, int& nbad) 201 { 202 nbad = 0; 203 sa_size_t n = 0; 204 size_t l = str.length(); 205 if (l < 1) return(0); 206 if ((str[0] == '#') || (str[0] == '*')) return(0); 207 size_t q = 0; 208 size_t p = str.find_first_not_of(" \t"); 209 if ((str[p] == '+') || (str[p] == '-')) { 210 if (p == l-1) return(0); 211 if (!isdigit(str[p+1])) return(0); 212 } 213 else if (!isdigit(str[p]) && (str[p] != '\'') && (str[p] != '(') ) return(0); 214 215 while(q < l) { 216 p = str.find_first_not_of(" \t",q); 217 if (p >= l) break; 218 if (str[p] == '\'') { // Decodage d'un string 219 q = str.find('\'',p+1); 220 if (q < l) { 221 vecv_.push_back(MuTyV(str.substr(p+1,q-p-1))); 222 n++; q++; 223 } 224 else nbad++; 225 } 226 else if (str[p] == '(') { // Decodage d'un complex 227 q = str.find(')',p); 228 if (q < l) { 229 q++; 230 MuTyV mtv(str.substr(p,q-p)); 231 complex<double> z = mtv; 232 vecv_.push_back(MuTyV(z)); 233 n++; 234 } 235 else nbad++; 236 } 237 else { 238 q = str.find_first_of(" \t",p); 239 if (!isdigit(str[p]) && !(str[p] == '+') && !(str[p] == '-') ) { // une chaine 240 continue; 241 } 242 else { // C'est un nombre 243 if (str.find('.',p) < q) { // c'est un flottant 244 r_8 x = atof(str.substr(p,q-p).c_str()); 245 vecv_.push_back(MuTyV(x)); 246 } 247 else { // un entier 248 int_8 l = atol(str.substr(p,q-p).c_str()); 249 vecv_.push_back(MuTyV(l)); 250 } 251 n++; 252 } 253 } 254 } 255 return (n); 256 } 257 258 //! Decodes the input ASCII stream, creating a sequence of values 259 /*! \param is : Input ASCII stream 260 \param nr : Number of non empty (or comment) lines in stream (return value) 261 \param nc : Number of columns (= ntot/nlines) (return value) 262 \return Number of decoded elements 263 */ 170 264 sa_size_t EnumeratedSequence::FillFromFile(istream& is, 171 265 sa_size_t& nr, sa_size_t& nc) 172 266 { 173 cerr << " EnumeratedSequence::FillFromFile() pas implemente !" << endl; 174 return (0); 267 nr = 0; 268 nc = 0; 269 sa_size_t n = 0; 270 char buff[256]; 271 string line; 272 int nbad, nbadtot, nel; 273 nbadtot = nbad = 0; 274 while (!is.eof()) { 275 is.clear(); 276 is.getline(buff, 256); 277 line += buff; 278 if (is.good()) { 279 nel = Append(line, nbad); 280 cout << " Decoding line = " << line << " Nel= " << nel << endl; 281 if (nel > 0) { 282 nr++; n += nel; 283 } 284 nbadtot += nbad; 285 line = ""; 286 } 287 } 288 if (line.length() > 0) { 289 nel = Append(line, nbad); 290 cout << " Decoding Eline = " << line << " Nel= " << nel << endl; 291 if (nel > 0) { 292 nr++; n += nel; 293 } 294 nbadtot += nbad; 295 line = ""; 296 } 297 if (nbadtot > 0) 298 cout << "EnumeratedSequence::FillFromFile()/Warning " << nbadtot 299 << " bad match (quotes or parenthesis) in stream " << endl; 300 nc = n/nr; 301 return (n); 175 302 } 176 303 -
trunk/SophyaLib/TArray/utilarr.h
r1550 r1558 94 94 public: 95 95 explicit EnumeratedSequence(); 96 EnumeratedSequence(EnumeratedSequence const & es); 96 97 virtual ~EnumeratedSequence(); 97 98 virtual MuTyV & Value(sa_size_t k) const ; … … 102 103 EnumeratedSequence & operator = (MuTyV const & v); 103 104 104 EnumeratedSequence & Merge(EnumeratedSequence const & seq); 105 EnumeratedSequence & operator = (EnumeratedSequence const & es); 106 105 107 inline void Clear() { vecv_.clear(); } 106 108 void Print(ostream& os) const; 107 109 108 sa_size_t FillFromString(string const & str); 110 sa_size_t Append(EnumeratedSequence const & seq); 111 sa_size_t Append(string const & str, int & nbad); 109 112 sa_size_t FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc); 110 113
Note:
See TracChangeset
for help on using the changeset viewer.