File: substr.c

package info (click to toggle)
atlas 3.10.3-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 38,308 kB
  • sloc: ansic: 486,789; fortran: 66,209; asm: 7,267; makefile: 1,466; sh: 604
file content (131 lines) | stat: -rw-r--r-- 2,822 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

static char LINE0[2048], LINE1[2048];

void PrintUsage(char *nam)
{
   fprintf(stderr,
  "\nUSAGE: %s -i <infile> -o <outfile> -s \"<find str>\" \"<rep string>\"\n\n",
           nam);
   exit(-1);
}
void GetFlags(int nargs, char **args, FILE **fpin, FILE **fpout,
              int *nsub, char ***finds, char ***reps)
{
   int i, j, n;
   char **nfind, **nrep;
   *nsub = 0;
   *fpin = stdin;
   *fpout = stdout;
   for (i=1; i < nargs; i++)
   {
      if (args[i][0] != '-') PrintUsage(args[0]);
      switch(args[i][1])
      {
      case 'o':
         i++;
         if (!strcmp(args[i], "stderr")) *fpout = stderr;
         else if (!strcmp(args[i], "stdout")) *fpout = stdout;
         else *fpout = fopen(args[i], "w");
         assert(*fpout);
         break;
      case 'i':
         i++;
         if (!strcmp(args[i], "stdin")) *fpout = stdin;
         else *fpin = fopen(args[i], "r");
         assert(*fpin);
         break;
      case 's':
         n = *nsub + 1;
         nfind = malloc(sizeof(char *)*n);
         nrep = malloc(sizeof(char *)*n);
         assert(nfind && nrep);
         for (j=0; j < *nsub; j++)
         {
            nfind[j] = (*finds)[j];
            nrep[j] = (*reps)[j];
         }
         nfind[j] = args[++i];
         nrep[j] = args[++i];
         if (*nsub > 0)
         {
            free(*finds);
            free(*reps);
         }
         *finds = nfind;
         *reps = nrep;
         *nsub = n;
         break;
      default :
         PrintUsage(args[0]);
      }
   }
}

void MakeSub(char *ln, char *find, char *replace)
{
   char *ln2=LINE1;
   int i, j;
   char *sp;

   sp = strstr(ln, find);
   if (sp)
   {
      i = strlen(find);
      j = strlen(replace);
      strcpy(ln2, sp+i);
      strcpy(sp, replace);
      strcpy(sp+j, ln2);
      MakeSub(sp+j, find, replace);
   }
}

void FirstCharSub(char *ln, char *find, char *replace)
{
   char *ln2=LINE1;
   int i, j;

   i = strlen(find);
   if (!strncmp(ln, find, i))
   {
      j = strlen(replace);
      strcpy(ln2, ln+i);
      strcpy(ln, replace);
      strcpy(ln+j, ln2);
   }
}

void GoToTown(FILE *fpin, FILE *fpout, int nsub, char **finds, char **reps)
{
   char *ln=LINE0;
   int i;

   while (fgets(ln, 1024, fpin))
   {
      for (i=0; i < nsub; i++)
      {
         if (finds[i][0] == '^') FirstCharSub(ln, finds[i]+1, reps[i]);
         else MakeSub(ln, finds[i], reps[i]);
      }
      fputs(ln, fpout);
   }
}

int main(int nargs, char **args)
{
   FILE *fpin, *fpout;
   int nsub;
   char **finds, **reps;
   GetFlags(nargs, args, &fpin, &fpout, &nsub, &finds, &reps);
   GoToTown(fpin, fpout, nsub, finds, reps);
   if (nsub > 0)
   {
      free(finds);
      free(reps);
   }
   return(0);
}