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 eprintf("Error while writing to \"%s\": %m\n",
278 eprintf("Error while opening \"%s\" for writing. \n%m\n", filename);
285 int FileXML::write_to_file(FILE *file)
287 strcpy(filename, "");
288 fprintf(file, "<?xml version=\"1.0\"?>\n");
289 // Position may have been rewound after storing
290 if(fwrite(string, strlen(string), 1, file) || !strlen(string))
296 eprintf("Error while writing to \"%s\": %m\n",
303 int FileXML::read_from_file(char *filename, int ignore_error)
307 strcpy(this->filename, filename);
308 if(in = fopen(filename, "rb"))
310 fseek(in, 0, SEEK_END);
311 int new_length = ftell(in);
312 fseek(in, 0, SEEK_SET);
313 reallocate_string(new_length + 1);
314 fread(string, new_length, 1, in);
315 string[new_length] = 0;
322 eprintf("Error while opening \"%s\" for reading. \n%m\n", filename);
329 int FileXML::read_from_string(char *string)
331 strcpy(this->filename, "");
332 reallocate_string(strlen(string) + 1);
333 strcpy(this->string, string);
334 length = strlen(string);
339 int FileXML::set_shared_string(char *shared_string, long available)
341 strcpy(this->filename, "");
346 string = shared_string;
347 this->available = available;
356 // ================================ XML tag
361 total_properties = 0;
370 int XMLTag::set_delimiters(char left_delimiter, char right_delimiter)
372 this->left_delimiter = left_delimiter;
373 this->right_delimiter = right_delimiter;
377 int XMLTag::reset_tag() // clear all structures
380 for(int i = 0; i < total_properties; i++) delete [] tag_properties[i];
381 for(int i = 0; i < total_properties; i++) delete [] tag_property_values[i];
382 total_properties = 0;
386 int XMLTag::write_tag()
389 char *current_property, *current_value;
392 string[len] = left_delimiter;
396 for(i = 0; tag_title[i] != 0 && len < MAX_LENGTH; i++, len++) string[len] = tag_title[i];
399 for(i = 0; i < total_properties && len < MAX_LENGTH; i++)
401 string[len++] = ' '; // add a space before every property
403 current_property = tag_properties[i];
406 for(j = 0; current_property[j] != 0 && len < MAX_LENGTH; j++, len++)
408 string[len] = current_property[j];
411 if(len < MAX_LENGTH) string[len++] = '=';
413 current_value = tag_property_values[i];
416 if( len < MAX_LENGTH) string[len++] = '\"';
418 for(j = 0; current_value[j] != 0 && len < MAX_LENGTH; j++, len++)
420 string[len] = current_value[j];
422 if(len < MAX_LENGTH) string[len++] = '\"';
425 if(len < MAX_LENGTH) string[len++] = right_delimiter; // terminating bracket
429 int XMLTag::read_tag(char *input, long &position, long length)
432 int i, j, terminating_char;
434 // search for beginning of a tag
435 while(input[position] != left_delimiter && position < length) position++;
437 if(position >= length) return 1;
440 while(position < length &&
441 (input[position] == ' ' || // skip spaces
442 input[position] == '\n' || // also skip new lines
443 input[position] == left_delimiter)) // skip <
446 if(position >= length) return 1;
448 tag_start = position;
454 input[position] != '=' &&
455 input[position] != ' ' && // space ends title
456 input[position] != right_delimiter;
459 tag_title[i] = input[position];
463 if(position >= length) return 1;
465 if(input[position] == '=')
467 // no title but first property
469 position = tag_start; // rewind
474 i < MAX_PROPERTIES &&
476 input[position] != right_delimiter;
481 while(position < length &&
482 (input[position] == ' ' || // skip spaces
483 input[position] == '\n' || // also skip new lines
484 input[position] == left_delimiter)) // skip <
487 // read the property description
491 input[position] != right_delimiter &&
492 input[position] != ' ' &&
493 input[position] != '\n' && // also new line ends it
494 input[position] != '=';
497 string[j] = input[position];
502 // store the description in a property array
503 tag_properties[total_properties] = new char[strlen(string) + 1];
504 strcpy(tag_properties[total_properties], string);
506 // find the start of the value
507 while(position < length &&
508 (input[position] == ' ' || // skip spaces
509 input[position] == '\n' || // also skip new lines
510 input[position] == '=')) // skip =
513 // find the terminating char
514 if(position < length && input[position] == '\"')
516 terminating_char = '\"'; // use quotes to terminate
517 if(position < length) position++; // don't store the quote itself
520 terminating_char = ' '; // use space to terminate
522 // read until the terminating char
526 input[position] != right_delimiter &&
527 input[position] != '\n' &&
528 input[position] != terminating_char;
531 string[j] = input[position];
535 // store the value in a property array
536 tag_property_values[total_properties] = new char[strlen(string) + 1];
537 strcpy(tag_property_values[total_properties], string);
539 // advance property if one was just loaded
540 if(tag_properties[total_properties][0] != 0) total_properties++;
542 // get the terminating char
543 if(position < length && input[position] != right_delimiter) position++;
547 if(position < length && input[position] == right_delimiter) position++;
549 if(total_properties || tag_title[0])
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 //printf("XMLTag::get_property %s %s\n", tag_properties[i], tag_property_values[i]);
595 char *tv = tag_property_values[i];
596 while (j < strlen(tag_property_values[i])) {
597 if (!strncmp(tv + j,""",6)) {
601 value[k++] = tv[j++];
611 char* XMLTag::get_property_text(int number)
613 if(number < total_properties)
614 return tag_properties[number];
619 int XMLTag::get_property_int(int number)
621 if(number < total_properties)
622 return atol(tag_properties[number]);
627 float XMLTag::get_property_float(int number)
629 if(number < total_properties)
630 return atof(tag_properties[number]);
635 char* XMLTag::get_property(char *property)
638 for(i = 0, result = 0; i < total_properties && !result; i++)
640 if(!strcasecmp(tag_properties[i], property))
642 return tag_property_values[i];
649 int32_t XMLTag::get_property(char *property, int32_t default_)
652 get_property(property, temp_string);
653 if(temp_string[0] == 0)
656 return atol(temp_string);
659 int64_t XMLTag::get_property(char *property, int64_t default_)
663 get_property(property, temp_string);
664 if(temp_string[0] == 0)
668 sscanf(temp_string, "%lld", &result);
673 // int XMLTag::get_property(char *property, int default_)
675 // temp_string[0] = 0;
676 // get_property(property, temp_string);
677 // if(temp_string[0] == 0) return default_;
678 // else return atol(temp_string);
681 float XMLTag::get_property(char *property, float default_)
684 get_property(property, temp_string);
685 if(temp_string[0] == 0)
688 return atof(temp_string);
691 double XMLTag::get_property(char *property, double default_)
694 get_property(property, temp_string);
695 if(temp_string[0] == 0)
698 return atof(temp_string);
701 int XMLTag::set_title(char *text) // set the title field
703 strcpy(tag_title, text);
707 int XMLTag::set_property(char *text, int32_t value)
709 sprintf(temp_string, "%ld", value);
710 set_property(text, temp_string);
714 int XMLTag::set_property(char *text, int64_t value)
716 sprintf(temp_string, "%lld", value);
717 set_property(text, temp_string);
721 int XMLTag::set_property(char *text, float value)
723 if (value - (float)((int64_t)value) == 0)
724 sprintf(temp_string, "%lld", (int64_t)value);
726 sprintf(temp_string, "%.6e", value);
727 set_property(text, temp_string);
731 int XMLTag::set_property(char *text, double value)
733 if (value - (double)((int64_t)value) == 0)
734 sprintf(temp_string, "%lld", (int64_t)value);
736 sprintf(temp_string, "%.16e", value);
737 set_property(text, temp_string);
741 int XMLTag::set_property(char *text, char *value)
743 tag_properties[total_properties] = new char[strlen(text) + 1];
744 strcpy(tag_properties[total_properties], text);
748 for (int i = strlen(value)-1; i >= 0; i--)
752 // Allocate space, and replace quotes with "
753 tag_property_values[total_properties] = new char[strlen(value) + qcount*5 + 1];
755 for (int i = 0; i < strlen(value); i++) {
758 tag_property_values[total_properties][j] = 0;
759 strcat(tag_property_values[total_properties],""");
763 tag_property_values[total_properties][j++] = value[i];
766 tag_property_values[total_properties][j] = 0;