2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 printf("Clear text from lib.ru for Pocket PC, Version 0.2.\n");
38 printf("Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>\n");
39 printf(" All rights reserved.\n");
40 printf("Usage:\n clrlibru [-i INPUTFILE] [-o OUTPUT] [-l NUMSPACE]\n");
54 is_rtag(char *tag, int len) {
59 if ( strcmp(tag, *ptr) == 0 )
67 static char *optarg = NULL;
71 mgetopt(int argn, char* argv[], char *option) {
74 if ( current >= argn ) return -1;
76 key = *(argv[current]+1);
77 if ( *(argv[current]) == '-' || *(argv[current]) == '/' ) {
78 char *ptr = strchr( option, key );
80 printf( "Unknown option: %s\n", argv[current]);
84 if ( *(ptr+1) == ':' ) {
89 printf("No value for -%c\n" ,key);
95 printf("Unknown option: %s\n", argv[current]);
105 #define INDROPINTAG 4
107 #define INDROPINCLSTAG 6
111 main(int argn, char *argv[]) {
113 FILE *in=stdin, *out=stdout;
116 int lenbuf=0,closelen=0;
119 while((ch = mgetopt(argn, argv, "l:i:o:h?"))!=-1) {
122 if ( (in=fopen(optarg, "r"))==NULL) {
123 printf("Can't open file %s\n", optarg);
128 if ( (out=fopen(optarg, "w"))==NULL) {
129 printf("Can't open file %s\n",optarg);
134 spacelen = atoi(optarg);
135 if ( spacelen < 0 ) {
136 printf("-l should be >= 0\n");
147 while( (ch=getc(in)) != EOF ) {
148 if ( state==INTXT ) {
152 } else if ( ch == '\n' ) {
156 } else if ( ch != '\r' )
158 } else if ( state==INHEADTAG ) {
160 buf[ lenbuf ] = tolower(ch);
162 } else if ( ch == '>' ) {
163 if ( is_rtag(buf,lenbuf) ) {
170 } else if ( lenbuf == 0 && ch != '/' ) {
171 fputc('<',out); fputc(ch,out);
174 if ( is_rtag(buf,lenbuf) ) {
182 } else if ( state==INTAG ) {
187 } else if ( state == INDROPTAG ) {
192 } else if ( state == INDROPINTAG ) {
194 state=INDROPINCLSTAG;
197 } else if ( state == INDROPINCLSTAG ) {
199 if ( closelen < lenbuf && tolower(ch) == buf[closelen] ) {
201 if ( closelen==lenbuf )
207 } else if ( state==FINDEND ) {
211 if ( lenbuf > spacelen ) {
212 fwrite(buf, sizeof(char), lenbuf, out);
215 } else if ( ch=='\n' ) {
218 fwrite(buf, sizeof(char), lenbuf, out);
220 } else if ( ch !='\r' ) {
226 printf("Unknown state: %d\n", state);