The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sub.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * sub.c - This little utility substitutes all occurances of a string
4  * with another string. Is this useful? Maybe not. But it's
5  * been here since r2 so it seems a shame to remove it :)
6  * Currently not built by build system
7  *
8  * $Id: sub.c 2833 2013-06-29 06:40:28Z chrislit $
9  *
10  * Copyright 1997-2009 CrossWire Bible Society (http://www.crosswire.org)
11  * CrossWire Bible Society
12  * P. O. Box 2528
13  * Tempe, AZ 85280-2528
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the
17  * Free Software Foundation version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * General Public License for more details.
23  *
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 main(int argc, char **argv)
30 {
31  FILE *fp;
32  char *buf;
33  int size;
34 
35  if ((argc < 3) || (argc > 4)) {
36  fprintf(stderr, "usage: %s <string> <substitute string> [filename]\n", *argv);
37  exit(-1);
38  }
39 
40  if (argc > 3)
41  fp = fopen(argv[3], "r");
42  else fp = stdin;
43 
44  size = strlen(argv[1]);
45  buf = (char *)calloc(size + 1, 1);
46 
47  while ((buf[size - 1] = fgetc(fp)) != EOF) {
48  if (!strcmp(buf, argv[1])) {
49  printf("\n%s", argv[2]);
50  memset(buf, 0, size);
51  continue;
52  }
53  if (*buf) {
54  printf("%c", *buf);
55  }
56  memmove(buf, &buf[1], size);
57  }
58  buf[size - 1] = 0;
59  printf("%s", buf);
60 }
int main(int argc, char **argv)
Definition: addcomment.cpp:32
int fp
Definition: gbfidx.cpp:49
int size
Definition: regex.c:5043