sreplace.c
author lbatlle@npdl268.bpo.hp.com
Tue, 29 May 2007 18:50:35 +0200
changeset 5 18c4f565e6d8
parent 4 576167dd9de2
child 7 fcde17ef6af6
permissions -rw-r--r--
Fixed the license names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
971e9ed05e1a Added license
viric@mandarina
parents: 0
diff changeset
     1
/*
5
18c4f565e6d8 Fixed the license names
lbatlle@npdl268.bpo.hp.com
parents: 4
diff changeset
     2
    Stream Replace - replaces sequences of bytes in FILE streams
2
971e9ed05e1a Added license
viric@mandarina
parents: 0
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
971e9ed05e1a Added license
viric@mandarina
parents: 0
diff changeset
     4
971e9ed05e1a Added license
viric@mandarina
parents: 0
diff changeset
     5
    Please find the license in the provided COPYING file.
971e9ed05e1a Added license
viric@mandarina
parents: 0
diff changeset
     6
*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
     7
#include <stdio.h>
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
     8
#include <assert.h>
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
     9
#include <string.h>
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    10
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    11
enum {
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    12
  BUFFER_SIZE = 2048
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    13
};
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    14
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    15
static FILE * input, * output;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    16
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    17
struct String
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    18
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    19
  char *ptr;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    20
  int length;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    21
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    22
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    23
struct CmpState
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    24
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    25
  struct String *str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    26
  int matched;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    27
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    28
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    29
struct String old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    30
struct String new_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    31
struct CmpState cmp_state;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    32
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    33
/* Buffer:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    34
   [F] - read
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    35
   [F] - read_cmp
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    36
   [F]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    37
   [F] - filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    38
   [ ]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    39
   [ ]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    40
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    41
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    42
   read <= read_cmp < filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    43
*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    44
struct Buffer
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    45
{
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    46
  unsigned char *base;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    47
  int filled;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    48
  int read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    49
  int read_cmp;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    50
  int size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    51
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    52
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    53
static int get_buffer_space(struct Buffer *b, int start, int end)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    54
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    55
  int space;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    56
  space = end - start + b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    57
  if (space > b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    58
    space -= b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    59
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    60
  return space;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    61
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    62
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    63
static int min(int a, int b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    64
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    65
  if (a < b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    66
    return a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    67
  return b;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    68
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    69
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    70
static int one_before(struct Buffer *b, int pos)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    71
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    72
  if (pos == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    73
    return b->size - 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    74
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    75
  return pos - 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    76
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    77
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    78
/* This should be called without any exceeding size */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    79
static int file_to_buffer(struct Buffer *b, int size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    80
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    81
  int blocksize;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    82
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    83
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    84
  blocksize = min(b->size - b->filled, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    85
  if (blocksize > 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    86
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    87
    res = fread(b->base + b->filled, 1, blocksize, input);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    88
    /*res = read(0, b->base + b->filled, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    89
    /*fprintf(stderr, "fread1 %i bytes (should %i).\n", res, blocksize);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    90
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    91
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    92
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    93
    /* Check EOF */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    94
    if (res == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    95
      return 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    96
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    97
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    98
  if (size > 0 && res == blocksize)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    99
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   100
    blocksize = min(b->read - 1, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   101
    res = fread(b->base, 1, blocksize, input);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   102
    /*res = read(0, b->base, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   103
    /*fprintf(stderr, "fread2 %i bytes.\n", res);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   104
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   105
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   106
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   107
  return 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   108
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   109
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   110
static int file_read_more(struct Buffer *b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   111
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   112
  int can_read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   113
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   114
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   115
  can_read = get_buffer_space(b, b->filled, b->read - 1);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   116
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   117
  /*fprintf(stderr, "file_to_buffer %i\n", can_read);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   118
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   119
  return file_to_buffer(b, can_read);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   120
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   121
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   122
/* pos will be read or read_cmp, depending on
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   123
   what to read */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   124
static int can_read_from_buffer(struct Buffer *b, int pos)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   125
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   126
  /*
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   127
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   128
   read <= read_cmp < filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   129
   */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   130
  if (pos != b->filled)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   131
    return 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   132
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   133
    return 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   134
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   135
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   136
static int getc_real(struct Buffer *b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   137
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   138
  int a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   139
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   140
  if (!can_read_from_buffer(b, b->read))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   141
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   142
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   143
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   144
    a = *(b->base + b->read);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   145
    b->read += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   146
    if (b->read == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   147
      b->read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   148
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   149
  /*fprintf(stderr, "getc_real %x\n", a);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   150
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   151
  return a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   152
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   153
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   154
static int getc_cmp(struct Buffer *b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   155
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   156
  int a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   157
  if (!can_read_from_buffer(b, b->read_cmp))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   158
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   159
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   160
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   161
    a = *(b->base + b->read_cmp);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   162
    b->read_cmp += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   163
    if (b->read_cmp == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   164
      b->read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   165
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   166
  /*fprintf(stderr, "getc_cmp %x\n", a);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   167
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   168
  return a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   169
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   170
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   171
/* This will not be moved farer than read_cmp */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   172
static void read_advance(struct Buffer *b, int adv)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   173
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   174
  int i;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   175
  for (i=0; i < adv; ++i)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   176
    getc_real(b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   177
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   178
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   179
static void process_buffer(struct Buffer *b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   180
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   181
  int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   182
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   183
  while (1)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   184
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   185
    c = getc_cmp(b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   186
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   187
    if (c == EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   188
      break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   189
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   190
    if (cmp_state.str->ptr[cmp_state.matched] == (char) c)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   191
    {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   192
      cmp_state.matched++;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   193
      if (cmp_state.matched == cmp_state.str->length)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   194
      {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   195
        fwrite(new_str.ptr, 1, new_str.length, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   196
        /*write(1, new_str.ptr, new_str.length);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   197
        read_advance(b, cmp_state.matched);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   198
        cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   199
      }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   200
    } else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   201
    {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   202
      int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   203
      c = getc_real(b); /* Will not be EOF. read_cmp is forwarder. */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   204
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   205
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   206
      cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   207
      b->read_cmp = b->read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   208
    }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   209
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   210
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   211
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   212
static void end_buffer(struct Buffer *b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   213
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   214
  int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   215
  while((c = getc_real(b)) != EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   216
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   217
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   218
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   219
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   220
static void loop()
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   221
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   222
  struct Buffer b;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   223
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   224
  b.base = (unsigned char *) malloc(BUFFER_SIZE);
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   225
  b.read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   226
  b.read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   227
  b.filled = 0;
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   228
  b.size = BUFFER_SIZE;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   229
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   230
  while(file_read_more(&b) > 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   231
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   232
    int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   233
    process_buffer(&b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   234
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   235
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   236
  end_buffer(&b);;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   237
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   238
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   239
static void show_usage(const char *pname)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   240
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   241
  printf("usage: %s OLD_STR NEW_STR\n", pname);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   242
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   243
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   244
static void parse_backslashes(char *str)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   245
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   246
  int was_backslash = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   247
  char *write_str = str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   248
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   249
  while (*str != 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   250
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   251
    if (*str == '\\')
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   252
      was_backslash = 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   253
    else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   254
    {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   255
      if (was_backslash)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   256
      {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   257
        switch(*str)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   258
        {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   259
          case '\\':
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   260
            *(write_str++) = '\\';
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   261
            break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   262
          case 'n':
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   263
            *(write_str++) = '\n';
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   264
            break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   265
          case 't':
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   266
            *(write_str++) = '\t';
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   267
            break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   268
          default:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   269
            *(write_str++) = *str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   270
        }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   271
      }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   272
      else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   273
      {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   274
        *(write_str++) = *str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   275
      }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   276
      was_backslash = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   277
    }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   278
    ++str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   279
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   280
  *(write_str++) = '\0';
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   281
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   282
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   283
static void process_parameters(int argc, char **argv)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   284
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   285
  if (argc != 3)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   286
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   287
    show_usage(argv[0]);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   288
    exit(1);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   289
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   290
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   291
  old_str.ptr = argv[1];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   292
  new_str.ptr = argv[2];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   293
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   294
  parse_backslashes(old_str.ptr);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   295
  parse_backslashes(new_str.ptr);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   296
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   297
  old_str.length = strlen(old_str.ptr);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   298
  new_str.length = strlen(new_str.ptr);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   299
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   300
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   301
int main(int argc, char ** argv)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   302
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   303
  process_parameters(argc, argv);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   304
  
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   305
  cmp_state.str = &old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   306
  cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   307
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   308
  input = stdin;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   309
  output = stdout;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   310
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   311
  loop();
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   312
  return 0;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   313
}