File: subst.c

package info (click to toggle)
ftjam 2.3.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 800 kB
  • ctags: 1,022
  • sloc: ansic: 7,951; yacc: 365; sh: 95; makefile: 57
file content (170 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "regexp.h"

#define  MAX_SUBST_STRING  4095

typedef struct subst_string
{
  char    s[MAX_SUBST_STRING+1];
  int     len;
  
} subst_string;


static void
subst_string_init( subst_string*  string,
                   const char*    source )
{
  int  len = 0;
  
  if (source)
  {
    len = strlen(source);
    if (len > MAX_SUBST_STRING)
      len = MAX_SUBST_STRING;

    if (len > 0)
      memcpy( string->s, source, len );
  }
  string->s[len] = 0;
  string->len    = len;
}                   


static void
subst_string_add( subst_string*  string,
                  const char*    source,
                  int            src_len )
{
  int  delta;

  delta = string->len + src_len - MAX_SUBST_STRING;
  if (delta > 0)
    src_len -= delta;  

  if ( src_len > 0 )  
  {
    memcpy( string->s + string->len, source, src_len );
    string->len += src_len;
    string->s[ string->len ] = 0;
  }
}


static void
subst_string_add_char( subst_string*  string,
                       const char     c )
{
  int  len = string->len;
  if ( len < MAX_SUBST_STRING )
  {
    string->s[len++] = c;
    string->s[len]   = 0;
    string->len = len;
  }
}                       


static void
subst_string_setsub( subst_string*  string,
                     int            offset,
                     int            len,
                     const char*    replacement )
{
  subst_string  temp;
  int           end;
  
  subst_string_init( &temp, 0 );
  
  subst_string_add( &temp, string->s, offset );
  
  if (replacement)
    subst_string_add( &temp, replacement, strlen(replacement) );

  offset += len;    
  subst_string_add( &temp, string->s + offset, string->len - offset );
  
  *string = temp;
}



void substitute( const char*  source,
                 const char*  pattern,
                 const char*  replacement,
                 char*        target )
{
  regexp*  repat;

  target[0] = '\0';
  
  repat = regcomp( (char*)pattern );
  if (repat)
  {
    if ( regexec( repat, (char*)source ) )
    {
      subst_string  targ;
      
      /* a match was found */
      if ( !strchr( replacement, '$' ) )
      {
        /* fast, simple replacement */
        subst_string_init( &targ, source );
        subst_string_setsub( &targ, repat->startp[0] - source,
                             repat->endp[0] - repat->startp[0],
                             replacement );
      }
      else
      {
        /* perform sub-expression replacement */
        subst_string  temp;
        const char*   src = replacement;
        char          c;
        
        subst_string_init( &targ, 0 );
        subst_string_init( &temp, 0 );

        for (;;)
        {
          c = *src++;
          if (!c) break;
          
          if ( c == '$' )
          {
            if ( *src == '&' || (*src - '0') <= 9 )
            {
              int  n = (*src++ - '0');
              
              if ( (unsigned int)n > 9)
                n = 0;

              subst_string_add( &temp, repat->startp[n],
                                repat->endp[n] - repat->startp[n] );

              continue;
            }
          }
          
          /* ordinary character */
          if ( c == '\\' && ( *src == '\\' || *src == '$' ) )
            c = *src++;
            
          subst_string_add_char( &temp, c );
        }
        
        subst_string_setsub( &targ, repat->startp[0] - source,
                             repat->endp[0] - repat->startp[0],
                             temp.s );
      }
      
      /* now copy the content of "targ" to the target */
      strcpy( target, targ.s );
    }
    else
    {
      /* no match, simply copy source into target */
      strcpy( target, source );
    }
    free( repat );
  }
}