sreplace.c
author viric@llimona
Wed, 04 Jul 2007 21:40:18 +0200
changeset 9 7f47f2295c44
parent 7 fcde17ef6af6
child 8 4ecd557ebebf
child 11 0ef0d9c52f82
permissions -rw-r--r--
Removed the test based on nonrespository files.
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
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    13
enum {
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    14
  BUFFER_SIZE = 2048
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    15
};
0
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
static FILE * input, * output;
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
struct CmpState
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 String *str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    22
  int matched;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    23
};
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 old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    26
struct String new_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    27
struct CmpState cmp_state;
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
/* Buffer:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    30
   [F] - read
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    31
   [F] - read_cmp
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    32
   [F]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    33
   [F] - filled
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    34
   [ ]
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    35
   [ ]
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
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    38
   read <= read_cmp < filled
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
struct Buffer
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    41
{
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
    42
  unsigned char *base;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    43
  int filled;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    44
  int read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    45
  int read_cmp;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    46
  int size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    47
};
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    48
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    49
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
    50
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    51
  int space;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    52
  space = end - start + b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    53
  if (space > b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    54
    space -= b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    55
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    56
  return space;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    59
static int min(int a, int b)
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
  if (a < b)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    62
    return a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    63
  return 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    66
static int one_before(struct Buffer *b, int pos)
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
  if (pos == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    69
    return b->size - 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    70
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    71
  return pos - 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    74
/* This should be called without any exceeding size */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    75
static int file_to_buffer(struct Buffer *b, int size)
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
  int blocksize;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    78
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    79
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    80
  blocksize = min(b->size - b->filled, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    81
  if (blocksize > 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    82
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    83
    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
    84
    /*res = read(0, b->base + b->filled, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    85
    /*fprintf(stderr, "fread1 %i bytes (should %i).\n", res, blocksize);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    86
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    87
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    88
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    89
    /* Check EOF */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    90
    if (res == 0)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    91
      return 0;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    94
  if (size > 0 && res == blocksize)
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
    blocksize = min(b->read - 1, size);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    97
    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
    98
    /*res = read(0, b->base, blocksize);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
    99
    /*fprintf(stderr, "fread2 %i bytes.\n", res);*/
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   100
    size -= res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   101
    b->filled = (b->filled + res) % b->size;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   102
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   103
  return 1;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   106
static int file_read_more(struct Buffer *b)
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
  int can_read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   109
  int res;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   110
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   111
  can_read = get_buffer_space(b, b->filled, b->read - 1);
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
  /*fprintf(stderr, "file_to_buffer %i\n", can_read);*/
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
  return file_to_buffer(b, 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   118
/* pos will be read or read_cmp, depending on
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   119
   what to read */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   120
static int can_read_from_buffer(struct Buffer *b, int pos)
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
  /*
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   123
   In the circular sense:
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   124
   read <= read_cmp < filled
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
  if (pos != b->filled)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   127
    return 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   128
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   129
    return 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   130
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   131
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   132
static int getc_real(struct Buffer *b)
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
  int a;
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
  if (!can_read_from_buffer(b, b->read))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   137
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   138
  else
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
    a = *(b->base + b->read);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   141
    b->read += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   142
    if (b->read == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   143
      b->read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   144
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   145
  /*fprintf(stderr, "getc_real %x\n", a);*/
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
  return 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   150
static int getc_cmp(struct Buffer *b)
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
  int a;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   153
  if (!can_read_from_buffer(b, b->read_cmp))
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   154
    a = EOF;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   155
  else
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   156
  {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   157
    a = *(b->base + b->read_cmp);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   158
    b->read_cmp += 1;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   159
    if (b->read_cmp == b->size)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   160
      b->read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   161
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   162
  /*fprintf(stderr, "getc_cmp %x\n", a);*/
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
  return 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   167
/* This will not be moved farer than read_cmp */
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   168
static void read_advance(struct Buffer *b, int adv)
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
  int i;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   171
  for (i=0; i < adv; ++i)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   172
    getc_real(b);
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   175
static void process_buffer(struct Buffer *b)
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
  int c;
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
  while (1)
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
    c = getc_cmp(b);
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
    if (c == EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   184
      break;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   185
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   186
    if (cmp_state.str->ptr[cmp_state.matched] == (char) c)
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
      cmp_state.matched++;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   189
      if (cmp_state.matched == cmp_state.str->length)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   190
      {
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   191
        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
   192
        /*write(1, new_str.ptr, new_str.length);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   193
        read_advance(b, cmp_state.matched);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   194
        cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   195
      }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   196
    } else
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
      int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   199
      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
   200
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   201
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   202
      cmp_state.matched = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   203
      b->read_cmp = b->read;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   204
    }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   205
  }
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
static void end_buffer(struct Buffer *b)
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
  int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   211
  while((c = getc_real(b)) != EOF)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   212
      fputc(c, output);
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   213
      /*write(1, &c, 1);*/
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   214
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   215
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   216
static void loop()
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
  struct Buffer b;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   219
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   220
  b.base = (unsigned char *) malloc(BUFFER_SIZE);
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   221
  b.read = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   222
  b.read_cmp = 0;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   223
  b.filled = 0;
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   224
  b.size = BUFFER_SIZE;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   225
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   226
  while(file_read_more(&b) > 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
    int c;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   229
    process_buffer(&b);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   230
  }
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
  end_buffer(&b);;
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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   235
static void show_usage(const char *pname)
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
  printf("usage: %s OLD_STR NEW_STR\n", 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
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   240
static void process_parameters(int argc, char **argv)
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
  if (argc != 3)
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
    show_usage(argv[0]);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   245
    exit(1);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   246
  }
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   247
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   248
  old_str.ptr = argv[1];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   249
  new_str.ptr = argv[2];
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   250
7
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   251
  old_str.length = parse_backslashes(old_str.ptr);
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   252
  fprintf(stderr, "OLD: ");
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   253
  print_hex(stderr, &old_str);
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   254
  fprintf(stderr, "\n");
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   255
7
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   256
  new_str.length = parse_backslashes(new_str.ptr);
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   257
  fprintf(stderr, "NEW: ");
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   258
  print_hex(stderr, &new_str);
fcde17ef6af6 Separated backslash C parser library.
viric@llimona
parents: 5
diff changeset
   259
  fprintf(stderr, "\n");
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   260
}
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   261
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   262
int main(int argc, char ** argv)
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   263
{
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   264
  process_parameters(argc, argv);
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   265
  
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   266
  cmp_state.str = &old_str;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   267
  cmp_state.matched = 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
  input = stdin;
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   270
  output = stdout;
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
  loop();
3
edae5c16989e Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents: 0
diff changeset
   273
  return 0;
0
31bc1c76aa85 Initial. Seems to work.
lbatlle@npdl268.bpo.hp.com
parents:
diff changeset
   274
}