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
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/FORMAT/JCAMPFile.h>
#include <BALL/DATATYPE/regularExpression.h>
#include <BALL/MATHS/common.h>
namespace BALL
{
bool JCAMPFile::JCAMPValue::operator == (const JCAMPValue& value) const
{
return string_value == value.string_value &&
numeric_value == value.numeric_value &&
type == value.type;
}
bool JCAMPFile::JCAMPValue::operator != (const JCAMPValue& value) const
{
return !(*this == value);
}
JCAMPFile::JCAMPFile(const String& name, OpenMode open_mode)
: LineBasedFile(),
header_(),
entries_()
{
LineBasedFile::open(name, open_mode);
enableTrimWhitespaces(true);
}
void JCAMPFile::read()
{
// Clear the old contents of the header/entry maps.
header_.clear();
entries_.clear();
// First I try to read the title.
// Only parameters appearing *after* the title are used.
// Expressions for the valid declarations in a JCAMPFile
const RegularExpression array("^##\\$([^=]+)= \\(0\\.\\.([1-9][0-9]*)\\)");
const RegularExpression regular("^##\\$([^=]+)= (.*)");
const RegularExpression header("^##([^=]+)= (.*)");
std::vector<Substring> groups;
while (readLine())
{
JCAMPValue value;
try
{
//overread a comment
if (getLine().hasPrefix("$$"))
{
continue;
}
else if (getLine().hasPrefix("##$"))
{
// Check whether the definition is an array.
if (array.find(getLine(), groups))
{
value.type = ARRAY;
static String key = groups[1];
Size number_of_values = groups[2].toString().toInt() + 1;
// first try to parse the remainder of this line
std::vector<String> remainder;
getLine().split(remainder, ")"); // take the part right of the array definition
if (remainder.size() > 2)
{
throw Exception::ParseError(__FILE__, __LINE__, getName(),
String("Error parsing array entry on line ")
+ String(getLineNumber()));
}
if (remainder.size() == 2)
{
std::vector<String> fields;
remainder[1].split(fields);
for (Position i=0; i<fields.size(); i++)
{
if (!fields[i].isFloat())
{
Log.error() << "Warning: " << fields[i] << " is not a float!" << std::endl;
}
else
{
value.numeric_value.push_back(fields[i].toDouble());
}
}
}
while (value.numeric_value.size() < number_of_values)
{
if (!readLine() || getLine().hasPrefix("#"))
{
break;
}
static std::vector<String> fields;
getLine().split(fields);
for (Position i = 0; i < fields.size(); i++)
{
if (!fields[i].isFloat())
{
Log.error() << "Warning: " << fields[i] << " is not a float!" << std::endl;
}
else
{
value.numeric_value.push_back(fields[i].toDouble());
}
}
}
// Store the entry only if the parsing was successful.
if (value.numeric_value.size() == number_of_values)
{
entries_.insert(KeyValuePair(key, value));
}
else
{
throw Exception::ParseError(__FILE__, __LINE__, getName(),
String("Error parsing array entry on line ")
+ String(getLineNumber()) + String(" (number of values read:")
+ String(value.numeric_value.size())
+ " - number of values expected: " + String(number_of_values) + ")");
}
// next entry.
continue;
}
// =================== read non-array entry ===============================
else if (regular.find(getLine(), groups))
{
// =================== read numeric entry ===============================
if (groups[2].toString().isFloat())
{
value.numeric_value.push_back(groups[2].toString().toFloat());
value.type = NUMERIC;
}
// =================== read string entry ================================
else
{
value.string_value = groups[2];
value.type = STRING;
}
entries_.insert(KeyValuePair(groups[1], value));
// next entry.
continue;
}
}
else if (header.find(getLine(), groups))
{
if (groups[1] == "END")
{
// Abort for the "##END=" entry.
break;
}
value.type = STRING;
value.string_value = groups[2];
// Insert the header entry into the header map
header_.insert(std::pair<String, String>(groups[1].toString(), groups[2].toString()));
// Next entry.
continue;
}
else if (getLine().hasPrefix("##END"))
{
break;
}
}
catch(Exception::InvalidFormat&)
{
Log.error() << "Could not convert string to number" << std::endl;
// throwing exception ParseError anyhow in next line
}
// We could not parse this line -- abort.
throw Exception::ParseError(__FILE__, __LINE__, getName(), getLine());
};
}
bool JCAMPFile::write()
{
if (!isOpen() || getOpenMode() != File::MODE_OUT)
{
throw(File::CannotWrite(__FILE__, __LINE__, name_));
}
HeaderMap::ConstIterator header_it(header_.begin());
for (; header_it != header_.end(); ++header_it)
{
(*this) << "##" << header_it->first << "= " << header_it->second << "\n";
}
EntryMap::ConstIterator entry_it(entries_.begin());
for (; entry_it != entries_.end(); ++entry_it)
{
(*this) << "##$" << entry_it->first << "= ";
switch (entry_it->second.type)
{
case STRING:
(*this) << entry_it->second.string_value.c_str() << "\n";
break;
case NUMERIC:
(*this) << entry_it->second.numeric_value[0] << "\n";
break;
case ARRAY:
(*this) << "(0.." << entry_it->second.numeric_value.size() << ")\n";
for (Position i = 0; i < entry_it->second.numeric_value.size(); ++i)
{
(*this) << entry_it->second.numeric_value[i];
// Pack eight numbers in one line, then a newline at the end.
if (((i + 1) % 8) == 0)
{
(*this) << "\n";
}
else
{
(*this) << " ";
}
}
// Make sure we terminate the line with a newline.
if ((entry_it->second.numeric_value.size() % 8) != 0)
{
(*this) << "\n";
}
break;
}
}
return true;
}
double JCAMPFile::getDoubleValue(const String& name) const
{
if (!entries_.has(name)) return 0.0;
const JCAMPValue& val(entries_[name]);
if (val.type == NUMERIC)
{
return val.numeric_value[0];
}
else if (val.type == STRING)
{
return val.string_value.trim().toDouble();
}
else if ((val.type == ARRAY) && (val.numeric_value.size() > 0))
{
return val.numeric_value[0];
}
return 0.0;
}
Index JCAMPFile::getIntValue(const String& name) const
{
if (!entries_.has(name)) return 0;
const JCAMPValue& val(entries_[name]);
if (val.type == NUMERIC)
{
int i = (int)Maths::round(val.numeric_value[0]);
return i; //round(val.numeric_value[0]).toInt();
}
else if(val.type == STRING)
{
return (Index)val.string_value.trim().toInt();
}
else if ((val.type == ARRAY) && (val.numeric_value.size() > 0))
{
return (Index)val.numeric_value[0];
}
return 0;
}
const JCAMPFile& JCAMPFile::operator = (const JCAMPFile& file)
{
header_ = file.header_;
entries_ = file.entries_;
LineBasedFile::operator = (file);
return *this;
}
bool JCAMPFile::operator == (const JCAMPFile& f) const
{
return header_ == f.header_ &&
entries_ == f.entries_;
}
bool JCAMPFile::operator != (const JCAMPFile& f) const
{
return ! (*this == f);
}
} // namespace BALL
|