rllib  1
rlsvgcat.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlsvgcat.cpp - description
3  -------------------
4  begin : Tue Apr 09 2006
5  copyright : (C) 2006 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlsvgcat.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 
22 {
23  fin = fout = 0;
24  s = -1;
25 }
26 
28 {
29  close();
30 }
31 
33 {
34  if(s == -1)
35  {
36  if(fout != 0 && fout != stdout) fclose((FILE *) fout);
37  }
38  else
39  {
40  fflush((FILE *) fout);
41  }
42  if(fin != 0) fclose((FILE *) fin);
43  fin = fout = 0;
44 }
45 
46 int rlSvgCat::open(const char *infile, const char *outfile)
47 {
48  fin = fout = NULL;
49  fin = (void *) fopen(infile,"r");
50  if(fin == 0)
51  {
52  printf("could not open %s\n",infile);
53  return -1;
54  }
55  if(outfile != 0)
56  {
57  fout = (void *) fopen(outfile,"w");
58  if(fout == 0)
59  {
60  printf("could not write %s\n",outfile);
61  return -1;
62  }
63  }
64  else
65  {
66  fout = (void *) stdout;
67  }
68  return 0;
69 }
70 
71 int rlSvgCat::reopenSocket(const char *infile, int socket)
72 {
73  s = socket;
74  if(s < 0)
75  {
76  s = -1;
77  return -1;
78  }
79  fin = fout = NULL;
80  fin = (void *) fopen(infile,"r");
81  if(fin == 0)
82  {
83  printf("could not open %s\n",infile);
84  return -1;
85  }
86  fout = fdopen(s,"w");
87  if(fout == NULL)
88  {
89  s = -1;
90  return -1;
91  }
92  return 0;
93 }
94 
96 {
97  while(fgets(line,sizeof(line)-1,(FILE *) fin) != 0)
98  {
99  catline();
100  }
101 }
102 
103 int rlSvgCat::outUntil(int i, const char *tag)
104 {
105  FILE *out = (FILE *) fout;
106  int len = strlen(tag);
107  while(line[i] != '\0' && line[i] != '\n')
108  {
109  if(strncmp(&line[i],tag,len) == 0)
110  {
111  for(int it=0; it<len; it++)
112  {
113  fputc(line[i++],out);
114  }
115  if(line[i] == '<') i--;
116  break;
117  }
118  else
119  {
120  fputc(line[i++],out);
121  }
122  }
123  return i;
124 }
125 
127 {
128  FILE *out = (FILE *) fout;
129  while(line[i] != '\0' && line[i] != '\n')
130  {
131  if(line[i] == '>')
132  {
133  //xlehrig fputc(line[i++],out);
134  fputc(line[i],out);
135  break;
136  }
137  if(line[i] == ' ' || line[i] == '\t')
138  {
139  break;
140  }
141  else
142  {
143  fputc(line[i++],out);
144  }
145  }
146  fputc('\n',out);
147  return i;
148 }
149 
151 {
152  //printf("outValue=%s",&line[i]);
153  FILE *out = (FILE *) fout;
154  while(line[i] != '\0' && line[i] != '\n')
155  {
156  if(line[i] == '<')
157  {
158  i--;
159  break;
160  }
161  else if(line[i] == '>')
162  {
163  fputc('\n',out);
164  fputc(line[i],out);
165  break;
166  }
167  else if(line[i] == '/' && line[i+1] == '>')
168  {
169  fputc('\n',out);
170  fputc(line[i++],out);
171  fputc(line[i],out);
172  break;
173  }
174  else
175  {
176  fputc(line[i++],out);
177  }
178  }
179  return i;
180 }
181 
183 {
184  int i = 0;
185  FILE *out = (FILE *) fout;
186 
187  while(line[i] != '\0' && line[i] != '\n')
188  {
189  while(line[i] == ' ' || line[i] == '\t' ) i++; // eliminate leading spaces
190  // compare tags
191  if (strncmp(&line[i],"<?",2) == 0)
192  {
193  fputc(line[i++],out);
194  fputc(line[i++],out);
195  i = outUntil(i,"?>");
196  fputc('\n',out);
197  }
198  else if(strncmp(&line[i],"<!",2) == 0) // comment
199  {
200  fprintf(out,"%s",&line[i]);
201  if(strstr(&line[i],">") != NULL) return;
202  while(fgets(line,sizeof(line)-1,(FILE *) fin) != 0)
203  {
204  fprintf(out,"%s",line);
205  if(strstr(line,">") != NULL) return;
206  }
207  return;
208  }
209  else if(strncmp(&line[i],"<desc",5) == 0)
210  {
211  fprintf(out,"%s",&line[i]);
212  return;
213  }
214  else if(strncmp(&line[i],"</",2) == 0)
215  {
216  if(strncmp(&line[i],"</svg>",6) == 0)
217  {
218  fprintf(out,"</svg>");
219  return;
220  }
221  if(strncmp(&line[i],"</SVG>",6) == 0)
222  {
223  fprintf(out,"</SVG>");
224  return;
225  }
226  fputc(line[i++],out);
227  fputc(line[i++],out);
228  i = outUntil(i,">");
229  fputc('\n',out);
230  }
231  else if(strncmp(&line[i],"<",1) == 0)
232  {
233  fputc(line[i++],out);
234  i = outUntilEnd(i);
235  }
236  else if(strncmp(&line[i],"/>",2) == 0)
237  {
238  fputc(line[i++],out);
239  fputc(line[i++],out);
240  fputc('\n',out);
241  }
242  else if(strncmp(&line[i],">",1) == 0)
243  {
244  fputc(line[i++],out);
245  fputc('\n',out);
246  }
247  else
248  {
249  i = outValue(i);
250  fputc('\n',out);
251  }
252  i++;
253  }
254 }
255 
void * fin
Definition: rlsvgcat.h:40
int outUntilEnd(int i)
Definition: rlsvgcat.cpp:126
int open(const char *infile, const char *outfile=0)
Definition: rlsvgcat.cpp:46
rlSvgCat()
Definition: rlsvgcat.cpp:21
virtual ~rlSvgCat()
Definition: rlsvgcat.cpp:27
int s
Definition: rlsvgcat.h:41
int outUntil(int i, const char *tag)
Definition: rlsvgcat.cpp:103
char line[256 *256]
Definition: rlsvgcat.h:42
void catline()
Definition: rlsvgcat.cpp:182
void * fout
Definition: rlsvgcat.h:40
void cat()
Definition: rlsvgcat.cpp:95
void close()
Definition: rlsvgcat.cpp:32
int outValue(int i)
Definition: rlsvgcat.cpp:150
int reopenSocket(const char *infile, int s)
Definition: rlsvgcat.cpp:71