vorbis bld msgs, ffmpeg one frame/frame flush bug, ffmpeg audio history fixes
[goodguy/history.git] / cinelerra-5.0 / guicast / stringfile.C
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 #include "stringfile.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 StringFile::StringFile(size_t length)
28 {
29         pointer = 0;
30         if(length == 0)
31         {
32                 this->length = 100000;
33         }
34         else
35         {
36                 this->length = length;
37         }
38         string = new char[this->length + 1];
39         available = this->length;
40 }
41
42 StringFile::StringFile(const char *filename)
43 {
44         FILE *in = fopen(filename, "rb");
45         if( in )
46         {
47                 fseek(in, 0, SEEK_END);
48                 length = ftell(in);
49                 available = length;
50                 fseek(in, 0, SEEK_SET);
51                 string = new char[length + 5];
52
53                 (void)fread(string, length, 1, in);
54                 for(int i = 0; i < 5; i++) string[length + i] = 0;
55                 fclose(in);
56         }
57         else
58         {
59                 //printf("File not found: %s\n", filename);
60                 length = 0;
61                 available = 1;
62                 string = new char[1];
63                 string[0] = 0;
64         }
65
66         pointer = 0;
67 }
68
69 StringFile::~StringFile()
70 {
71         delete [] string;
72 }
73
74 int StringFile::write_to_file(const char *filename)
75 {
76         FILE *out = fopen(filename, "wb");
77         if( out )
78         {
79                 fwrite(string, pointer, 1, out);
80         }
81         else
82         {
83 //              printf("Couldn't open %s for writing.\n", filename);
84                 return 1;
85         }
86         fclose(out);
87         return 0;
88 }
89
90 int StringFile::read_from_string(const char *string)
91 {
92         int i;
93
94         delete [] this->string;
95         length = strlen(string);
96         available = length;
97         this->string = new char[length + 5];
98         strcpy(this->string, string);
99         for(i = 0; i < 5; i++) this->string[length + i] = 0;
100         return 0;
101 }
102
103 size_t StringFile::get_length()
104 {
105         return strlen(string);
106 }
107
108 size_t StringFile::get_pointer()
109 {
110         return pointer;
111 }
112
113 int StringFile::readline(char *arg2)
114 {
115         readline(string1, arg2);
116         return 0;
117 }
118
119 int StringFile::readline()
120 {
121         readline(string1, string1);
122         return 0;
123 }
124
125 int StringFile::readline(float *arg2)
126 {
127         readline(string1, arg2);
128         return 0;
129 }
130
131 int StringFile::readline(int *arg2)
132 {
133         readline(string1, arg2);
134         return 0;
135 }
136
137 int StringFile::readline(long *arg2)
138 {
139         readline(string1, arg2);
140         return 0;
141 }
142
143 int StringFile::readline(Freq *arg2)
144 {
145         readline(string1, &(arg2->freq));
146         return 0;
147 }
148
149 int StringFile::readline(char *arg1, char *arg2)
150 {
151         int i, len, max;
152         len = 0; max = 1024;
153
154         while(string[pointer] == ' ') pointer++; // skip indent
155         arg1[0] = 0;    arg2[0] = 0;
156
157         for(i = 0; string[pointer] != ' ' && string[pointer] != '\n' && len < max; i++, pointer++)
158         {     // get title
159                 arg1[i] = string[pointer];
160                 len++;
161         }
162         arg1[i] = 0;
163
164         if(string[pointer] != '\n')
165         {       // get value
166                 pointer++;      // skip space
167                 for(i = 0; string[pointer] != '\n' && len < max; i++, pointer++)
168                 {
169                         arg2[i] = string[pointer];
170                         len++;
171                 }
172                 arg2[i] = 0;
173         }
174         pointer++;      // skip eoln
175         return 0;
176 }
177
178 int StringFile::backupline()
179 {
180         while(string[pointer] != 10 && pointer > 0)
181         {
182                 pointer--;     // first eoln
183         }
184         if(string[pointer] == 10) pointer--;        // skip eoln
185
186         while(string[pointer] != 10 && pointer > 0)
187         {
188                 pointer--;     // second eoln
189         }
190
191         if(string[pointer] == 10) pointer++;      // skip eoln
192         return 0;
193 }
194
195 int StringFile::readline(char *arg1, long *arg2)
196 {
197         readline(arg1, string1);
198         *arg2 = atol(string1);
199         return 0;
200 }
201
202 int StringFile::readline(char *arg1, int *arg2)
203 {
204         long arg;
205         readline(arg1, &arg);
206         *arg2 = (int)arg;
207         return 0;
208 }
209
210 int StringFile::readline(char *arg1, float *arg2)
211 {
212         readline(arg1, string1);
213         *arg2 = atof(string1);
214         return 0;
215 }
216
217 int StringFile::writeline(char *arg1, int indent)
218 {
219 // reallocate the string
220         int len = strlen(arg1);
221         if( (len + indent) > (int)(available - pointer) ) {
222                 char *newstring = new char[available * 2];
223                 strcpy(newstring, string);
224                 delete string;
225                 available *= 2;
226                 length *= 2;
227                 string = newstring;
228         }
229
230         for(int i = 0; i < indent; i++) string[pointer++] = ' ';
231         strcpy(&string[pointer], arg1);
232         pointer += len;
233         return 0;
234 }
235
236 int StringFile::writeline(char *arg1, char *arg2, int indent)
237 {
238         sprintf(string1, "%s %s\n", arg1, arg2);
239         writeline(string1, indent);
240         return 0;
241 }
242
243 int StringFile::writeline(char *arg1, long arg2, int indent)
244 {
245         sprintf(string1, "%s %ld\n", arg1, arg2);
246         writeline(string1, indent);
247         return 0;
248 }
249
250 int StringFile::writeline(char *arg1, int arg2, int indent)
251 {
252         sprintf(string1, "%s %d\n", arg1, arg2);
253         writeline(string1, indent);
254         return 0;
255 }
256
257 int StringFile::writeline(char *arg1, float arg2, int indent)
258 {
259         sprintf(string1, "%s %f\n", arg1, arg2);
260         writeline(string1, indent);
261         return 0;
262 }
263
264 int StringFile::writeline(char *arg1, Freq arg2, int indent)
265 {
266         sprintf(string1, "%s %d\n", arg1, arg2.freq);
267         writeline(string1, indent);
268         return 0;
269 }