r969: Render format selector: Do not change the path name when the format is changed.
[cinelerra_cv/ct.git] / guicast / stringfile.C
blob5f604d4925985909d16b195411236451a190212d
1 #include "stringfile.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 StringFile::StringFile(long length)
8         pointer = 0;
9         if(length == 0)
10         {
11                 this->length = 100000;
12         }
13         else
14         {
15                 this->length = length;
16         }
17         string = new char[this->length + 1];
18         available = this->length;
21 StringFile::StringFile(char *filename)
23         FILE *in;
24         if(in = fopen(filename, "rb"))
25         {
26                 fseek(in, 0, SEEK_END);
27                 length = ftell(in);
28                 available = length;
29                 fseek(in, 0, SEEK_SET);
30                 string = new char[length + 5];
32                 fread(string, length, 1, in);
33                 for(int i = 0; i < 5; i++) string[length + i] = 0;
34                 fclose(in);
35         }
36         else
37         {
38                 //printf("File not found: %s\n", filename);
39                 length = 0;
40                 available = 1;
41                 string = new char[1];
42                 string[0] = 0;
43         }
44         
45         pointer = 0;
48 StringFile::~StringFile()
50         delete [] string;
53 int StringFile::write_to_file(char *filename)
55         FILE *out;
56         if(out = fopen(filename, "wb"))
57         {
58                 fwrite(string, pointer, 1, out);
59         }
60         else
61         {
62 //              printf("Couldn't open %s for writing.\n", filename);
63                 return 1;
64         }
65         fclose(out);
66         return 0;
69 int StringFile::read_from_string(char *string)
71         int i;
72         
73         delete [] this->string;
74         length = strlen(string);
75         available = length;
76         this->string = new char[length + 5];
77         strcpy(this->string, string);
78         for(i = 0; i < 5; i++) this->string[length + i] = 0;
79         return 0;
82 long StringFile::get_length()
84         return strlen(string);
87 long StringFile::get_pointer()
89         return pointer;
92 int StringFile::readline(char *arg2)
93 {       
94         readline(string1, arg2);
95         return 0;
98 int StringFile::readline()
99 {       
100         readline(string1, string1);
101         return 0;
104 int StringFile::readline(float *arg2)
106         readline(string1, arg2);
107         return 0;
110 int StringFile::readline(int *arg2)
112         readline(string1, arg2);
113         return 0;
116 int StringFile::readline(long *arg2)
118         readline(string1, arg2);
119         return 0;
122 int StringFile::readline(Freq *arg2)
124         readline(string1, &(arg2->freq));
125         return 0;
128 int StringFile::readline(char *arg1, char *arg2)
130         int i, len, max;
131         len = 0; max = 1024;
132         
133         while(string[pointer] == ' ') pointer++; // skip indent
134         arg1[0] = 0;    arg2[0] = 0;
135         
136         for(i = 0; string[pointer] != ' ' && string[pointer] != '\n' && len < max; i++, pointer++)
137         {     // get title
138                 arg1[i] = string[pointer];
139                 len++;
140         }
141         arg1[i] = 0;
143         if(string[pointer] != '\n')
144         {       // get value
145                 pointer++;      // skip space
146                 for(i = 0; string[pointer] != '\n' && len < max; i++, pointer++)
147                 {
148                         arg2[i] = string[pointer];
149                         len++;
150                 }
151                 arg2[i] = 0;
152         }
153         pointer++;      // skip eoln
154         return 0;
157 int StringFile::backupline()
159         while(string[pointer] != 10 && pointer > 0)
160         {
161                 pointer--;     // first eoln
162         }
163         if(string[pointer] == 10) pointer--;        // skip eoln
164         
165         while(string[pointer] != 10 && pointer > 0)
166         {
167                 pointer--;     // second eoln
168         }
169         
170         if(string[pointer] == 10) pointer++;      // skip eoln
171         return 0;
174 int StringFile::readline(char *arg1, long *arg2)
176         readline(arg1, string1);
177         *arg2 = atol(string1);
178         return 0;
181 int StringFile::readline(char *arg1, int *arg2)
183         long arg;
184         readline(arg1, &arg);
185         *arg2 = (int)arg;
186         return 0;
189 int StringFile::readline(char *arg1, float *arg2)
191         readline(arg1, string1);
192         *arg2 = atof(string1);
193         return 0;
196 int StringFile::writeline(char *arg1, int indent)
198         int i;
199         
200 // reallocate the string
201         if(pointer + strlen(arg1) > available)
202         {
203                 char *newstring = new char[available * 2];
204                 strcpy(newstring, string);
205                 delete string;
206                 available *= 2;
207                 length *= 2;
208                 string = newstring;
209         }
210         
211         for(i = 0; i < indent; i++, pointer++) string[pointer] = ' ';
212         sprintf(&string[pointer], arg1);
213         pointer += strlen(arg1);
214         return 0;
217 int StringFile::writeline(char *arg1, char *arg2, int indent)
219         int i;
220         
221         sprintf(string1, "%s %s\n", arg1, arg2);
222         writeline(string1, indent);
223         return 0;
226 int StringFile::writeline(char *arg1, long arg2, int indent)
228         sprintf(string1, "%s %ld\n", arg1, arg2);
229         writeline(string1, indent);
230         return 0;
233 int StringFile::writeline(char *arg1, int arg2, int indent)
235         sprintf(string1, "%s %d\n", arg1, arg2);
236         writeline(string1, indent);
237         return 0;
240 int StringFile::writeline(char *arg1, float arg2, int indent)
242         sprintf(string1, "%s %f\n", arg1, arg2);
243         writeline(string1, indent);
244         return 0;
247 int StringFile::writeline(char *arg1, Freq arg2, int indent)
248 {       
249         sprintf(string1, "%s %d\n", arg1, arg2.freq);
250         writeline(string1, indent);
251         return 0;