12 // Precision in base 10
13 // for float is 6 significant figures
14 // for double is 16 significant figures
18 FileXML::FileXML(char left_delimiter, char right_delimiter)
20 tag.set_delimiters(left_delimiter, right_delimiter);
21 this->left_delimiter = left_delimiter;
22 this->right_delimiter = right_delimiter;
24 string = new char[available];
26 position = length = 0;
33 if(!share_string) delete [] string;
34 if(output_length) delete [] output;
39 printf("FileXML::dump:\n%s\n", string);
42 int FileXML::terminate_string()
51 length = strlen(string);
57 int FileXML::append_newline()
63 int FileXML::append_tag()
66 append_text(tag.string, tag.len);
71 int FileXML::append_text(char *text)
73 append_text(text, strlen(text));
77 int FileXML::append_text(char *text, long len)
79 while(position + len > available)
81 reallocate_string(available * 2);
84 for(int i = 0; i < len; i++, position++)
86 string[position] = text[i];
91 int FileXML::encode_text(char *text)
93 // We have to encode at least the '<' char
94 // We encode three things:
98 char leftb[] = "<";
99 char rightb[] = ">";
100 char amp[] = "&";
102 int len = strlen(text);
104 for (int i = 0; i < len; i++)
107 case '<': replacement = leftb; break;
108 case '>': replacement = rightb; break;
109 case '&': replacement = amp; break;
110 default: replacement = 0; break;
115 append_text(text + lastpos, i - lastpos);
116 append_text(replacement, strlen(replacement));
120 append_text(text + lastpos, len - lastpos);
126 int FileXML::reallocate_string(long new_available)
130 char *new_string = new char[new_available];
131 for(int i = 0; i < position; i++) new_string[i] = string[i];
132 available = new_available;
139 char* FileXML::read_text()
141 long text_position = position;
144 // use < to mark end of text and start of tag
147 for(; position < length && string[position] != left_delimiter; position++)
152 // allocate enough space
153 if(output_length) delete [] output;
154 output_length = position - text_position;
155 output = new char[output_length + 1];
157 //printf("FileXML::read_text %d %c\n", text_position, string[text_position]);
158 for(i = 0; text_position < position; text_position++)
160 // filter out first newline
161 if((i > 0 && i < output_length - 1) || string[text_position] != '\n')
163 // check if we have to decode special characters
164 // but try to be most backward compatible possible
165 int character = string[text_position];
166 if (string[text_position] == '&')
168 if (text_position + 3 < length)
170 if (string[text_position + 1] == 'l' && string[text_position + 2] == 't' && string[text_position + 3] == ';')
175 if (string[text_position + 1] == 'g' && string[text_position + 2] == 't' && string[text_position + 3] == ';')
181 if (text_position + 4 < length)
183 if (string[text_position + 1] == 'a' && string[text_position + 2] == 'm' && string[text_position + 3] == 'p' && string[text_position + 4] == ';')
190 output[i] = character;
199 int FileXML::read_tag()
202 while(position < length && string[position] != left_delimiter)
207 if(position >= length) return 1;
208 //printf("FileXML::read_tag %s\n", &string[position]);
209 return tag.read_tag(string, position, length);
212 int FileXML::read_text_until(char *tag_end, char *output, int max_len)
215 int out_position = 0;
216 int test_position1, test_position2;
219 while(!result && position < length && out_position < max_len - 1)
221 while(position < length && string[position] != left_delimiter)
223 //printf("FileXML::read_text_until 1 %c\n", string[position]);
224 output[out_position++] = string[position++];
227 if(position < length && string[position] == left_delimiter)
231 result = 1; // assume end
233 for(test_position1 = 0, test_position2 = position + 1; // skip <
234 test_position2 < length &&
235 tag_end[test_position1] != 0 &&
237 test_position1++, test_position2++)
239 // null result when first wrong character is reached
240 //printf("FileXML::read_text_until 2 %c\n", string[test_position2]);
241 if(tag_end[test_position1] != string[test_position2]) result = 0;
244 // no end tag reached to copy <
247 output[out_position++] = string[position++];
251 output[out_position] = 0;
252 // if end tag is reached, position is left on the < of the end tag
257 int FileXML::write_to_file(char *filename)
260 strcpy(this->filename, filename);
261 if(out = fopen(filename, "wb"))
263 fprintf(out, "<?xml version=\"1.0\"?>\n");
264 // Position may have been rewound after storing so we use a strlen
265 if(!fwrite(string, strlen(string), 1, out) && strlen(string))
267 fprintf(stderr, "FileXML::write_to_file 1 \"%s\": %s\n",
279 fprintf(stderr, "FileXML::write_to_file 2 \"%s\": %s\n",
288 int FileXML::write_to_file(FILE *file)
290 strcpy(filename, "");
291 fprintf(file, "<?xml version=\"1.0\"?>\n");
292 // Position may have been rewound after storing
293 if(fwrite(string, strlen(string), 1, file) || !strlen(string))
299 fprintf(stderr, "FileXML::write_to_file \"%s\": %s\n",
307 int FileXML::read_from_file(char *filename, int ignore_error)
311 strcpy(this->filename, filename);
312 if(in = fopen(filename, "rb"))
314 fseek(in, 0, SEEK_END);
316 fseek(in, 0, SEEK_SET);
317 reallocate_string(length + 1);
318 fread(string, length, 1, in);
325 fprintf(stderr, "FileXML::read_from_file \"%s\" %s\n",
334 int FileXML::read_from_string(char *string)
336 strcpy(this->filename, "");
337 reallocate_string(strlen(string) + 1);
338 strcpy(this->string, string);
339 length = strlen(string);
344 int FileXML::set_shared_string(char *shared_string, long available)
346 strcpy(this->filename, "");
351 string = shared_string;
352 this->available = available;
361 // ================================ XML tag
366 total_properties = 0;
375 int XMLTag::set_delimiters(char left_delimiter, char right_delimiter)
377 this->left_delimiter = left_delimiter;
378 this->right_delimiter = right_delimiter;
382 int XMLTag::reset_tag() // clear all structures
385 for(int i = 0; i < total_properties; i++) delete [] tag_properties[i];
386 for(int i = 0; i < total_properties; i++) delete [] tag_property_values[i];
387 total_properties = 0;
391 int XMLTag::write_tag()
394 char *current_property, *current_value;
397 string[len] = left_delimiter;
401 for(i = 0; tag_title[i] != 0 && len < MAX_LENGTH; i++, len++) string[len] = tag_title[i];
404 for(i = 0; i < total_properties && len < MAX_LENGTH; i++)
406 string[len++] = ' '; // add a space before every property
408 current_property = tag_properties[i];
411 for(j = 0; current_property[j] != 0 && len < MAX_LENGTH; j++, len++)
413 string[len] = current_property[j];
416 if(len < MAX_LENGTH) string[len++] = '=';
418 current_value = tag_property_values[i];
421 if( len < MAX_LENGTH) string[len++] = '\"';
423 for(j = 0; current_value[j] != 0 && len < MAX_LENGTH; j++, len++)
425 string[len] = current_value[j];
427 if(len < MAX_LENGTH) string[len++] = '\"';
430 if(len < MAX_LENGTH) string[len++] = right_delimiter; // terminating bracket
434 int XMLTag::read_tag(char *input, long &position, long length)
437 int i, j, terminating_char;
439 // search for beginning of a tag
440 while(input[position] != left_delimiter && position < length) position++;
442 if(position >= length) return 1;
445 while(position < length &&
446 (input[position] == ' ' || // skip spaces
447 input[position] == '\n' || // also skip new lines
448 input[position] == left_delimiter)) // skip <
451 if(position >= length) return 1;
453 tag_start = position;
459 input[position] != '=' &&
460 input[position] != ' ' && // space ends title
461 input[position] != right_delimiter;
464 tag_title[i] = input[position];
468 if(position >= length) return 1;
470 if(input[position] == '=')
472 // no title but first property
474 position = tag_start; // rewind
479 i < MAX_PROPERTIES &&
481 input[position] != right_delimiter;
486 while(position < length &&
487 (input[position] == ' ' || // skip spaces
488 input[position] == '\n' || // also skip new lines
489 input[position] == left_delimiter)) // skip <
492 // read the property description
496 input[position] != right_delimiter &&
497 input[position] != ' ' &&
498 input[position] != '\n' && // also new line ends it
499 input[position] != '=';
502 string[j] = input[position];
507 // store the description in a property array
508 tag_properties[total_properties] = new char[strlen(string) + 1];
509 strcpy(tag_properties[total_properties], string);
511 // find the start of the value
512 while(position < length &&
513 (input[position] == ' ' || // skip spaces
514 input[position] == '\n' || // also skip new lines
515 input[position] == '=')) // skip =
518 // find the terminating char
519 if(position < length && input[position] == '\"')
521 terminating_char = '\"'; // use quotes to terminate
522 if(position < length) position++; // don't store the quote itself
524 else terminating_char = ' '; // use space to terminate
526 // read until the terminating char
530 input[position] != right_delimiter &&
531 input[position] != terminating_char;
534 string[j] = input[position];
538 // store the value in a property array
539 tag_property_values[total_properties] = new char[strlen(string) + 1];
540 strcpy(tag_property_values[total_properties], string);
542 // advance property if one was just loaded
543 if(tag_properties[total_properties][0] != 0) total_properties++;
545 // get the terminating char
546 if(position < length && input[position] != right_delimiter) position++;
550 if(position < length && input[position] == right_delimiter) position++;
552 if(total_properties || tag_title[0]) return 0; else return 1;
556 int XMLTag::title_is(char *title)
558 if(!strcasecmp(title, tag_title)) return 1;
562 char* XMLTag::get_title()
567 int XMLTag::get_title(char *value)
569 if(tag_title[0] != 0) strcpy(value, tag_title);
573 int XMLTag::test_property(char *property, char *value)
576 for(i = 0, result = 0; i < total_properties && !result; i++)
578 if(!strcasecmp(tag_properties[i], property) && !strcasecmp(value, tag_property_values[i]))
586 char* XMLTag::get_property(char *property, char *value)
589 for(i = 0, result = 0; i < total_properties && !result; i++)
591 if(!strcasecmp(tag_properties[i], property))
593 strcpy(value, tag_property_values[i]);
600 char* XMLTag::get_property_text(int number)
602 if(number < total_properties)
603 return tag_properties[number];
608 int XMLTag::get_property_int(int number)
610 if(number < total_properties)
611 return atol(tag_properties[number]);
616 float XMLTag::get_property_float(int number)
618 if(number < total_properties)
619 return atof(tag_properties[number]);
624 char* XMLTag::get_property(char *property)
627 for(i = 0, result = 0; i < total_properties && !result; i++)
629 if(!strcasecmp(tag_properties[i], property))
631 return tag_property_values[i];
638 int32_t XMLTag::get_property(char *property, int32_t default_)
641 get_property(property, temp_string);
642 if(temp_string[0] == 0)
645 return atol(temp_string);
648 int64_t XMLTag::get_property(char *property, int64_t default_)
652 get_property(property, temp_string);
653 if(temp_string[0] == 0)
657 sscanf(temp_string, "%lld", &result);
662 // int XMLTag::get_property(char *property, int default_)
664 // temp_string[0] = 0;
665 // get_property(property, temp_string);
666 // if(temp_string[0] == 0) return default_;
667 // else return atol(temp_string);
670 float XMLTag::get_property(char *property, float default_)
673 get_property(property, temp_string);
674 if(temp_string[0] == 0)
677 return atof(temp_string);
680 double XMLTag::get_property(char *property, double default_)
683 get_property(property, temp_string);
684 if(temp_string[0] == 0)
687 return atof(temp_string);
690 int XMLTag::set_title(char *text) // set the title field
692 strcpy(tag_title, text);
696 int XMLTag::set_property(char *text, int32_t value)
698 sprintf(temp_string, "%ld", value);
699 set_property(text, temp_string);
703 int XMLTag::set_property(char *text, int64_t value)
705 sprintf(temp_string, "%lld", value);
706 set_property(text, temp_string);
710 // int XMLTag::set_property(char *text, int value)
712 // sprintf(temp_string, "%d", value);
713 // set_property(text, temp_string);
717 int XMLTag::set_property(char *text, float value)
719 sprintf(temp_string, "%.6e", value);
720 set_property(text, temp_string);
724 int XMLTag::set_property(char *text, double value)
726 sprintf(temp_string, "%.16e", value);
727 set_property(text, temp_string);
731 int XMLTag::set_property(char *text, char *value)
733 tag_properties[total_properties] = new char[strlen(text) + 1];
734 strcpy(tag_properties[total_properties], text);
735 tag_property_values[total_properties] = new char[strlen(value) + 1];
736 strcpy(tag_property_values[total_properties], value);