l10n: Updated Slovak (sk) translation to 100%
[lxde/lxinput.git] / src / xml-purge.c
CommitLineData
49161c14
HJYP
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <unistd.h>
9
10#define IS_BLANK(ch) strchr(" \t\n\r", ch)
11
12static void purge_file( const char* file )
13{
14 struct stat statbuf;
15 int fd;
16 char* buf, *pbuf;
17 int in_tag = 0, in_quote = 0;
18 FILE* fo;
19
20 fd = open( file, O_RDONLY );
21 if( fd == -1 )
22 return;
23
24 if( fstat( fd, &statbuf) == -1 )
25 return;
26
27 if( buf = (char*)malloc( statbuf.st_size + 1 ) )
28 {
29 if( read( fd, buf, statbuf.st_size) == -1 )
30 {
31 free( buf );
32 return;
33 }
34 buf[ statbuf.st_size ] = '\0';
35 }
36 close( fd );
37
38 fo = fopen( file, "w" );
39 if( ! fo )
40 goto error;
41
42 for( pbuf = buf; *pbuf; ++pbuf )
43 {
44 if( in_tag > 0 )
45 {
46 if( in_quote )
47 {
48 if( *pbuf == '\"' )
49 in_quote = 0;
50 }
51 else
52 {
53 if( *pbuf == '\"' )
54 ++in_quote;
55 if( ! in_quote && IS_BLANK(*pbuf) ) /* skip unnecessary blanks */
56 {
57 do{
58 ++pbuf;
59 }while( IS_BLANK( *pbuf ) );
60
61 if( *pbuf != '>' )
62 fputc( ' ', fo );
63 --pbuf;
64 continue;
65 }
66 }
67 if( *pbuf == '>' )
68 --in_tag;
69 fputc( *pbuf, fo );
70 }
71 else
72 {
73 if( *pbuf == '<' )
74 {
75 if( 0 == strncmp( pbuf, "<!--", 4 ) ) /* skip comments */
76 {
77 pbuf = strstr( pbuf, "-->" );
78 if( ! pbuf )
79 goto error;
80 pbuf += 2;
81 continue;
82 }
83 ++in_tag;
84 fputc( '<', fo );
85 }
86 else
87 {
88 char* tmp = pbuf;
89 while( *tmp && IS_BLANK( *tmp ) && *tmp != '<' )
90 ++tmp;
91 if( *tmp == '<' ) /* all cdata are blank characters */
92 pbuf = tmp - 1;
93 else /* not blank, keep the cdata */
94 {
95 if( tmp == pbuf )
96 fputc( *pbuf, fo );
97 else
98 {
99 fwrite( pbuf, 1, tmp - pbuf, fo );
100 pbuf = tmp - 1;
101 }
102 }
103 }
104 }
105 }
106
107 fclose( fo );
108
109error:
110 free( buf );
111}
112
113int main( int argc, char** argv )
114{
115 int i;
116 if( argc < 2 )
117 return 1;
118
119 for( i = 1; i < argc; ++i )
120 purge_file( argv[ i] );
121
122 return 0;
123}