initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / filexml.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #ifndef FILEXML_H
23 #define FILEXML_H
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <limits.h>
28
29 #include "arraylist.h"
30 #include "filexml.inc"
31 #include "sizes.h"
32
33 #define MAX_TITLE 256
34
35 class XMLBuffer
36 {
37         long bsz, isz;
38         unsigned char *inp, *outp, *bfr, *lmt;
39         int destroy;
40
41         unsigned char *&demand(long len);
42 public:
43         XMLBuffer(long buf_size=0x1000, long max_size=LONG_MAX, int del=1);
44         XMLBuffer(long buf_size, const char *buf, int del=0); // writing
45         XMLBuffer(const char *buf, long buf_size, int del=0); // reading
46         ~XMLBuffer();
47
48         long otell() { return inp - bfr; }
49         long itell() { return outp - bfr; }
50         void oseek(long pos) { inp = bfr + pos; }
51         void iseek(long pos) { outp = bfr + pos; }
52         unsigned char *pos(long ofs=0) { return bfr+ofs; }
53         int read(char *bp, int n);
54         int write(const char *bp, int n);
55
56         int cur()  { return outp>=inp ? -1 : *outp; }
57         int next() { return outp>=inp ? -1 : *outp++; }
58         int next(int ch) {
59                 demand(otell()+1);
60                 return *inp++ = ch;
61         }
62
63         static char *decode_data(char *bp, const char *sp, int n=-1);
64         static char *encode_data(char *bp, const char *sp, int n=-1);
65         static char *copy_data(char *bp, const char *sp, int n=-1);
66         static long encoded_length(const char *sp, int n=-1);
67         static long copy_length(const char *sp, int n=-1);
68 };
69
70 class XMLTag
71 {
72         XMLBuffer *buffer;
73         class Property {
74         public:
75                 const char *prop;
76                 const char *value;
77
78                 Property(const char *pp, const char *vp);
79                 ~Property();
80         };
81         bool ws(char ch) { return ch==' ' || ch=='\n'; }
82
83 public:
84         XMLTag();
85         ~XMLTag();
86
87         int reset_tag();
88
89         int title_is(const char *title);
90         char *get_title();
91         int get_title(char *value);
92         int test_property(char *property, char *value);
93         const char *get_property_text(int number);
94         int get_property_int(int number);
95         float get_property_float(int number);
96         const char *get_property(const char *property);
97         const char* get_property(const char *property, char *value);
98         int32_t get_property(const char *property, int32_t default_);
99         int64_t get_property(const char *property, int64_t default_);
100         float get_property(const char *property, float default_);
101         double get_property(const char *property, double default_);
102
103         int set_title(const char *text);
104         int set_property(const char *text, const char *value);
105         int set_property(const char *text, int32_t value);
106         int set_property(const char *text, int64_t value);
107         int set_property(const char *text, float value);
108         int set_property(const char *text, double value);
109
110         int write_tag(FileXML *xml);
111         int read_tag(FileXML *xml);
112
113         char title[MAX_TITLE];
114         ArrayList<Property*> properties;
115         char *string;
116         long used, avail;
117 };
118
119
120 class FileXML
121 {
122 public:
123         FileXML(int coded=1);
124         ~FileXML();
125
126         int terminate_string();
127         int append_newline();
128         int append_tag();
129         int append_text(const char *text);
130         int append_data(const char *text, long len);
131         int append_text(const char *text, long len);
132
133         char* read_text();
134         int read_data_until(const char *tag_end, char *out, int len);
135         int read_text_until(const char *tag_end, char *out, int len);
136         int read_tag();
137         int write_to_file(const char *filename);
138         int write_to_file(FILE *file);
139         int read_from_file(const char *filename, int ignore_error = 0);
140         int read_from_string(char *string);
141         char *(*decode)(char *bp, const char *sp, int n);
142         char *(*encode)(char *bp, const char *sp, int n);
143         long (*coded_length)(const char *sp, int n);
144
145         int set_shared_input(char *shared_string, long avail, int coded=1);
146         int set_shared_output(char *shared_string, long avail, int coded=1);
147         void set_coding(int coding);
148         int get_coding();
149         int rewind();
150         char *get_data();
151         char *string();
152
153         XMLBuffer *buffer;
154         int coded;
155
156         XMLTag tag;
157         long output_length;
158         char *output;
159         char left_delimiter, right_delimiter;
160         char filename[MAX_TITLE];
161 };
162
163 #endif