File: parsefile.c

package info (click to toggle)
p3scan 2:2.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,568 kB
  • ctags: 2,371
  • sloc: ansic: 12,072; makefile: 253; sh: 234
file content (221 lines) | stat: -rw-r--r-- 6,465 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 *
 * parsefile.c
 *
 * (C) 2002 by Folke Ashberg <folke@ashberg.de>
 *
 * $Id: parsefile.c,v 1.1.1.1 2003/01/23 12:41:40 vtoroman Exp $
 *
 * parsefile.c provides functions for parsing text with replacing keywords.
 * it uses my getline.c for string handling
 *
 * This stuff provides functions for linehandlin on file descriptors,
 * especially using netork sockets, because the getline function is
 * non blocking (the write stuff not!)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#include <sys/stat.h>
#include "parsefile.h"
#include "getline.h"

#define NONULL(x) ( x==NULL ? "" : x) /* this is nice, found in the mutt code */

struct paramlist * paramlist_init(void){
   struct paramlist *pl;
   pl=malloc(sizeof(struct paramlist));
   pl->name=NULL;
   pl->value=NULL;
   pl->next=NULL;
   return pl;
}

void paramlist_uninit(struct paramlist ** p){
   struct paramlist * next;
   while (*p){
      next=(*p)->next;
      if ((*p)->name) free((*p)->name);
      if ((*p)->value) free((*p)->value);
      free(*p);
      *p=next;
   }
}

int paramlist_set(struct paramlist * p, char * name, char * value){
   /* TODO: deleteting (value==NULL) does nothing */
   struct paramlist *last=NULL;
   if (!name) return 1;
   while (p && (p->name) && (strcasecmp(p->name, name))){
      last=p;
      p=p->next;
   }
   if (!p){
      if (!value) return 0;
      p=paramlist_init();
      if (last) last->next=p;
   }
   if (p->name) free(p->name);
   if (p->value) free(p->value);
   p->name=strdup(name);
   p->value= value ? strdup(value) : NULL;
   return 0;
};

char * paramlist_get(struct paramlist * params, char * name){
   if (!name) return NULL;
   while (params){
      if (params->name && !strcasecmp(params->name, name)) return params->value;
      params=params->next;
   }
   return NULL;
}

int paramlist_strreplace(char ** dst, char * in, struct paramlist * params){
   typedef struct replacelist {
      int pos;    /* position in in where to replace      */
      int newlen; /* length of new text (params->value)   */
      int oldlen; /* length of old text (params->name)    */
      char * new; /* char to replace with */
      struct replacelist * next;
   } replacelist;

   int len, pos, l;
   int replacecount=0;
   char * occ;
   char * in_inc;
   int lastreplacepos;
   int incremented;
   struct replacelist * rl_first = NULL, * rl = NULL, * rl_last, *rl_next;
   struct paramlist * p;
   if (!in) return -1;
   if (!params){ /* nothing to replace */
      *dst=in;
      return 0;
   }
   *dst=NULL;
   len=strlen(in);
   p=params;
   /* look for replacements and save them, count size of new string */
   while (p){
      if (p->name){
         in_inc=in;
         incremented=0;
         while ((occ=strstr(in_inc, p->name))!=NULL){
            pos=occ - in;
            /* add entry to replacelist, which has to be sorted by pos,
            * we have to look up our entry point */
            rl=rl_first;
            rl_last=NULL;
            while (rl){
               if (rl->pos > pos) break;
               rl_last=rl;
               rl=rl->next;
            }
            rl_next=rl;
            rl=malloc(sizeof(struct replacelist));
            if (rl_last) rl_last->next=rl;
            else rl_first=rl;
            rl->next=rl_next;
            rl->pos=pos;
            rl->newlen=strlen(NONULL(p->value));
            rl->new=p->value;
            rl->oldlen=strlen(p->name);
            len+=rl->newlen - rl->oldlen;
            replacecount++;
            /* override old string to ensure no further shorter
            * string will match */
            memset(occ, 1, strlen(p->name));
            /* we loop strstr until we cannot find this p->name */
            in_inc+=rl->oldlen;
            incremented+=pos;
         }
      }
      p=p->next;
   }
   if (replacecount>0){
      *dst=malloc(len+1);
      (*dst)[len]='\0';
      rl=rl_first;
      pos=0;
      lastreplacepos=0;
      while (rl){
         /* copy unreplaced text into buf */
         l=rl->pos - lastreplacepos;
         memcpy(&(*dst)[pos], &in[lastreplacepos], l);
         lastreplacepos=rl->pos + rl->oldlen;
         pos+=l;
         if (rl->newlen > 0){
            /* copy replaced text */
            memcpy(&(*dst)[pos], rl->new, rl->newlen);
            pos+=rl->newlen;
         }
         rl=rl->next;
      }
      /* copy the rest of the line */
      if ((l=len-pos)>0) memcpy(&(*dst)[pos], &in[lastreplacepos], len-pos);
   } else *dst=in;
   rl=rl_first;
   while (rl){
      rl_last=rl;
      rl=rl->next;
      free(rl_last);
   }
   return replacecount;
}

int parsefile(char * infile, char * outfile , struct paramlist * params, int leading){
   int in, out, ret;
   if ( (in=open(infile, O_RDONLY))<0) return 1;
   /** if file already exists just send that file and don't do any parsing
     * prim vt */
   if ( (out=open(outfile,O_RDONLY))>=0){
      close(in);
      close(out);
      return 0;
   }

   if ( (out=open(outfile, O_WRONLY | O_CREAT | O_TRUNC,  S_IRUSR | S_IWUSR ))<0) return 1;
   ret=parsefds(in, out, params, leading);
   close(in);
   close(out);
   return ret;
}

int parsefds(int in, int out , struct paramlist * params, int leading){
   int res, replacements;
   char * replaced;
   struct linebuf *l;
   if ((l=linebuf_init(4096))==NULL) return 1;
   while ( (res=getline(in, l))>=0){
      if (l->linelen >=0 ){
         replacements=paramlist_strreplace(&replaced, l->line, params);
         if (writeline(out, leading, replaced)){
            //error
         }
         if (replacements>0) free(replaced);
      }
   }
   linebuf_uninit(l);
   if (res!=GETLINE_EOF) return 1;
   return 0;
}