1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* Copyright (c) 2001-2002 MUSIC TECHNOLOGY GROUP (MTG)
* UNIVERSITAT POMPEU FABRA
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <list>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "Array.hxx"
#include "Complex.hxx"
#include "DataTypes.hxx"
using CLAM::TData;
using CLAM::Complex;
namespace CLAMTest {
using namespace CLAM;
bool WriteOctaveVector(std::ofstream &f,
const Array<Complex > &array,
const std::string &name)
{
// Vector header.
f << "# Created by CLAM library" << std::endl
<< "# name: " << name.c_str() << std::endl
<< "# type: complex matrix" << std::endl
<< "# rows: 1" << std::endl
<< "# columns: " << array.Size() << std::endl;
int i;
for (i=0;i<array.Size(); i++)
f << " (" << array[i].Real() << "," << array[i].Imag() << ")";
f << std::endl;
if (!f)
return false;
return true;
}
bool WriteOctaveVector(std::ofstream &f,
const Array<TData > &array,
const std::string &name)
{
// Vector header.
f << "# Created by CLAM library" << std::endl
<< "# name: " << name.c_str() << std::endl
<< "# type: matrix" << std::endl
<< "# rows: 1" << std::endl
<< "# columns: " << array.Size() << std::endl;
int i;
for (i=0;i<array.Size(); i++)
f << " " << array[i];
f << std::endl;
if (!f)
return false;
return true;
}
bool LoadOctaveTDataVector(std::istream &f,
const std::string &name,
Array<TData> &array)
{
enum { eUnknown, // Reading vector header, about which we still know nothing.
eMatch , // Reading vector header, with matching name
eOther, // Reading vector header, with non-matching name or type
eSkipData, // Skiping data.
eData, // Reading our data.
eError // Name not found, or found with wrong type
} state = eUnknown;
// const std::string state_str[] = {
// std::string("eUnknown"),
// std::string("eMatch"),
// std::string("eOther"),
// std::string("eSkipData"),
// std::string("eData"),
// std::string("eError")
// };
std::string line;
std::string name_line = "# name: ";
name_line += name;
name_line += '\n';
std::string type_start = "# type: ";
std::string rows_start = "# rows: ";
std::string columns_start = "# columns: ";
const std::string good_type_line = "# type: matrix\n";
const std::string good_rows_line = "# rows: 1\n";
std::stringstream sstr;
sstr << array.Size();
std::string good_columns_line = "# columns: ";
good_columns_line += sstr.str();
// std::cerr << "Looking for: " << std::endl
// << name_line
// << good_type_line
// << good_rows_line
// << good_columns_line;
std::stringstream ss;
int pos;
TData *buffer = array.GetPtr();
// NOTE: The parser asumes that there is always "type", "rows"
// and "columns" fields in the header of a vector definition.
// f.seekg(0);
while (f) {
std::getline(f,line);
// std::cerr << " R: " << line << std::endl;
switch(state) {
case eUnknown:
if (line[0]!='#') { // End of header
state=eSkipData;
break;
}
if (line == name_line) {
state=eMatch;
break;
}
if (line != good_type_line &&
line.substr(0,type_start.size())==type_start ) {
state=eOther;
break;
}
if (line != good_rows_line &&
line.substr(0,rows_start.size())==rows_start) {
state=eOther;
break;
}
if (line != good_columns_line &&
line.substr(0,columns_start.size()) == columns_start) {
state=eOther;
break;
}
break;
case eOther:
if (line[0]!='#') { // End of header
state=eSkipData;
break;
}
if (line == name_line)
throw(Err("LoadOctaveTData: found vector with wrong parameters"));
break;
case eSkipData:
if (line[0]=='#') {
// First header line. We can skip it.
state=eUnknown;
break;
}
break;
case eData:
throw(Err("CLAM: Inconsistent state")); //Should never get here
case eMatch:
if ( line != good_type_line &&
line.substr(0,type_start.size())==type_start )
throw(Err("LoadOctaveTData: found vector with wrong type"));
if (line != good_rows_line &&
line.substr(0,rows_start.size())==rows_start)
throw(Err("LoadOctaveTData: found vector with wrong number of rows"));
if (line != good_columns_line &&
line.substr(0,columns_start.size()) == columns_start)
throw(Err("LoadOctaveTData: found vector with wrong number of columns"));
if (line[0]=='#') // End of header
break;
ss.str(line);
ss.seekg(0);
pos=0;
while (1) {
TData point;
ss >> point;
if (ss.eof())
if (pos == array.Size()-1)
return true;
else {
std::cerr << "Eof found while reading pos " << pos << std::endl;
return false;
}
if (ss.good())
if (pos == array.Size()-1) {
std::cerr << "pos = << "<<array.Size()<<"and more data available"<<std::endl;
return false;
}
if (ss.bad()) {
std::cerr << "Input error" << std::endl;
return false;
}
buffer[pos++]=point;
}
default:
throw(Err("Parse error loading octave vector file"));
}
// std::cerr << state_str[state] << std::endl;
}
return false;
}
bool LoadOctaveComplexVector(std::istream &f,
const std::string &name,
Array<Complex> &array)
{
enum { eUnknown, // Reading vector header, about which we still know nothing.
eMatch , // Reading vector header, with matching name
eOther, // Reading vector header, with non-matching name or type
eSkipData, // Skiping data.
eData, // Reading our data.
eError // Name not found, or found with wrong type
} state = eUnknown;
// const std::string state_str[] = {
// std::string("eUnknown"),
// std::string("eMatch"),
// std::string("eOther"),
// std::string("eSkipData"),
// std::string("eData"),
// std::string("eError")
// };
std::string line;
std::stringstream sstr;
sstr.str(name);
sstr.seekp(name.size());
// sstr << std::endl;
std::string name_line = "# name: ";
name_line += sstr.str();
std::string type_start = "# type: ";
std::string rows_start = "# rows: ";
std::string columns_start = "# columns: ";
sstr.str("");
// sstr << std::endl;
std::string good_type_line = "# type: complex matrix";
good_type_line += sstr.str();
std::string good_rows_line = "# rows: 1";
good_rows_line += sstr.str();
sstr.str("");
sstr << array.Size();// << std::endl;
std::string good_columns_line = "# columns: ";
good_columns_line += sstr.str();
// std::cerr << "Looking for: " << std::endl
// << name_line
// << good_type_line
// << good_rows_line
// << good_columns_line;
std::stringstream ss;
int pos;
Complex *buffer = array.GetPtr();
// NOTE: The parser asumes that there is always "type", "rows"
// and "columns" fields in the header of a vector definition.
// f.seekg(0);
while (f) {
std::getline(f,line);
// std::cerr << " R: " << line << std::endl;
switch(state) {
case eUnknown:
if (line[0]!='#') { // End of header
state=eSkipData;
break;
}
if (line == name_line) {
state=eMatch;
break;
}
if (line != good_type_line &&
line.substr(0,type_start.size())==type_start ) {
state=eOther;
break;
}
if (line != good_rows_line &&
line.substr(0,rows_start.size())==rows_start) {
state=eOther;
break;
}
if (line != good_columns_line &&
line.substr(0,columns_start.size()) == columns_start) {
state=eOther;
break;
}
break;
case eOther:
if (line[0]!='#') { // End of header
state=eSkipData;
break;
}
if (line == name_line)
throw(Err("LoadOctaveTData: found vector with wrong parameters"));
break;
case eSkipData:
if (line[0]=='#') {
// First header line. We can skip it.
state=eUnknown;
break;
}
break;
case eData:
throw(Err("CLAM: Inconsistent state")); //Should never get here
case eMatch:
if ( line != good_type_line &&
line.substr(0,type_start.size())==type_start )
throw(Err("LoadOctaveTData: found vector with wrong type"));
if (line != good_rows_line &&
line.substr(0,rows_start.size())==rows_start)
throw(Err("LoadOctaveTData: found vector with wrong number of rows"));
if (line != good_columns_line &&
line.substr(0,columns_start.size()) == columns_start)
throw(Err("LoadOctaveTData: found vector with wrong number of columns"));
if (line[0]=='#') // End of header
break;
ss.str(line);
ss.seekg(0);
pos=0;
while (1) {
std::string point;
ss >> point;
std::stringstream spoint(point);
char c;
TData real,imag;
spoint >> c >> real >> c >> imag >> c;
if (ss.bad()) {
std::cerr << "Input error" << std::endl;
return false;
}
buffer[pos++]=Complex(real,imag);
if (ss.good())
if (pos == array.Size()) {
std::cerr << "pos = << "<<array.Size()<<"and more data available"<<std::endl;
return false;
}
if (ss.eof())
if (pos == array.Size())
return true;
else {
std::cerr << "Eof found while reading pos " << pos << std::endl;
return false;
}
}
default:
throw(Err("Parse error loading octave vector file"));
}
// std::cerr << state_str[state] << std::endl;
}
return false;
}
}
|