sreplace.c
author viric@llimona
Wed, 04 Jul 2007 21:44:29 +0200
changeset 11 0ef0d9c52f82
parent 7 fcde17ef6af6
child 12 f81dd70a9b0b
permissions -rw-r--r--
Added compile time flag for verbose
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
7
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
    11
#include "sreplace.h"
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
    12
11
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
    13
const int verbose = 0;
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
    14
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    15
enum {
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    16
  BUFFER_SIZE = 2048
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    17
};
0
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
static FILE * input, * output;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    20
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    21
struct CmpState
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 String *str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    24
  int matched;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    25
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    26
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    27
struct String old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    28
struct String new_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    29
struct CmpState cmp_state;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    30
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    31
/* Buffer:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    32
   [F] - read
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    33
   [F] - read_cmp
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    34
   [F]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    35
   [F] - filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    36
   [ ]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    37
   [ ]
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
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    40
   read <= read_cmp < filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    41
*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    42
struct Buffer
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    43
{
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    44
  unsigned char *base;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    45
  int filled;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    46
  int read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    47
  int read_cmp;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    48
  int size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    49
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    50
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    51
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
    52
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    53
  int space;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    54
  space = end - start + b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    55
  if (space > b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    56
    space -= b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    57
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    58
  return space;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    61
static int min(int a, int b)
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
  if (a < b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    64
    return a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    65
  return b;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    66
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    67
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    68
static int one_before(struct Buffer *b, int pos)
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
  if (pos == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    71
    return b->size - 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    72
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    73
  return pos - 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    76
/* This should be called without any exceeding size */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    77
static int file_to_buffer(struct Buffer *b, int size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    78
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    79
  int blocksize;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    80
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    81
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    82
  blocksize = min(b->size - b->filled, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    83
  if (blocksize > 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    84
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    85
    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
    86
    /*res = read(0, b->base + b->filled, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    87
    /*fprintf(stderr, "fread1 %i bytes (should %i).\n", res, blocksize);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    88
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    89
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    90
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    91
    /* Check EOF */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    92
    if (res == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    93
      return 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    94
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    95
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    96
  if (size > 0 && res == blocksize)
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
    blocksize = min(b->read - 1, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    99
    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
   100
    /*res = read(0, b->base, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   101
    /*fprintf(stderr, "fread2 %i bytes.\n", res);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   102
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   103
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   104
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   105
  return 1;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   108
static int file_read_more(struct Buffer *b)
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
  int can_read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   111
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   112
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   113
  can_read = get_buffer_space(b, b->filled, b->read - 1);
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
  /*fprintf(stderr, "file_to_buffer %i\n", can_read);*/
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
  return file_to_buffer(b, 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   120
/* pos will be read or read_cmp, depending on
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   121
   what to read */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   122
static int can_read_from_buffer(struct Buffer *b, int pos)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   123
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   124
  /*
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   125
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   126
   read <= read_cmp < filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   127
   */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   128
  if (pos != b->filled)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   129
    return 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   130
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   131
    return 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   132
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   133
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   134
static int getc_real(struct Buffer *b)
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
  int a;
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
  if (!can_read_from_buffer(b, b->read))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   139
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   140
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   141
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   142
    a = *(b->base + b->read);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   143
    b->read += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   144
    if (b->read == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   145
      b->read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   146
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   147
  /*fprintf(stderr, "getc_real %x\n", a);*/
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
  return 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   152
static int getc_cmp(struct Buffer *b)
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
  int a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   155
  if (!can_read_from_buffer(b, b->read_cmp))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   156
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   157
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   158
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   159
    a = *(b->base + b->read_cmp);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   160
    b->read_cmp += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   161
    if (b->read_cmp == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   162
      b->read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   163
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   164
  /*fprintf(stderr, "getc_cmp %x\n", a);*/
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
  return 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   169
/* This will not be moved farer than read_cmp */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   170
static void read_advance(struct Buffer *b, int adv)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   171
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   172
  int i;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   173
  for (i=0; i < adv; ++i)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   174
    getc_real(b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   175
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   176
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   177
static void process_buffer(struct Buffer *b)
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
  int c;
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
  while (1)
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
    c = getc_cmp(b);
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
    if (c == EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   186
      break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   187
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   188
    if (cmp_state.str->ptr[cmp_state.matched] == (char) c)
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
      cmp_state.matched++;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   191
      if (cmp_state.matched == cmp_state.str->length)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   192
      {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   193
        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
   194
        /*write(1, new_str.ptr, new_str.length);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   195
        read_advance(b, cmp_state.matched);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   196
        cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   197
      }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   198
    } else
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
      int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   201
      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
   202
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   203
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   204
      cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   205
      b->read_cmp = b->read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   206
    }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   207
  }
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
static void end_buffer(struct Buffer *b)
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
  int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   213
  while((c = getc_real(b)) != EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   214
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   215
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   216
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   217
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   218
static void loop()
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
  struct Buffer b;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   221
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   222
  b.base = (unsigned char *) malloc(BUFFER_SIZE);
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   223
  b.read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   224
  b.read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   225
  b.filled = 0;
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   226
  b.size = BUFFER_SIZE;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   227
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   228
  while(file_read_more(&b) > 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
    int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   231
    process_buffer(&b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   232
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   233
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   234
  end_buffer(&b);;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   237
static void show_usage(const char *pname)
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
  printf("usage: %s OLD_STR NEW_STR\n", 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   242
static void process_parameters(int argc, char **argv)
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
  if (argc != 3)
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
    show_usage(argv[0]);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   247
    exit(1);
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   250
  old_str.ptr = argv[1];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   251
  new_str.ptr = argv[2];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   252
7
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   253
  old_str.length = parse_backslashes(old_str.ptr);
11
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   254
  if (verbose)
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   255
  {
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   256
      fprintf(stderr, "OLD: ");
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   257
      print_hex(stderr, &old_str);
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   258
      fprintf(stderr, "\n");
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   259
  }
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   260
7
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   261
  new_str.length = parse_backslashes(new_str.ptr);
11
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   262
  if (verbose)
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   263
  {
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   264
      fprintf(stderr, "NEW: ");
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   265
      print_hex(stderr, &new_str);
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   266
      fprintf(stderr, "\n");
0ef0d9c52f82 Added compile time flag for verbose
viric@llimona
parents: 7
diff changeset
   267
  }
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   268
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   269
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   270
int main(int argc, char ** argv)
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
  process_parameters(argc, argv);
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
  cmp_state.str = &old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   275
  cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   276
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   277
  input = stdin;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   278
  output = stdout;
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
  loop();
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   281
  return 0;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   282
}