/* text.c - Routines to handle replacements in text files (C) Richard K. Lloyd 2001-2004 */ #define Extern extern #include "replace.h" static int linereps,maxreplineswarn; /* Replacement counts/flags */ static size_t newlinepos; /* Where we've written to in the new line buffer */ static char *ostr,*nstr; /* Old and new strings */ static size_t osl,nsl; /* Old and new string sizes */ static size_t oldlen; /* Old string size */ static int asked; /* Asked about replacement for this string yet ? */ #ifdef __STDC__ static void copy_char(char thechar) #else static void copy_char(thechar) char thechar; #endif { /* Copy a character into the new line buffer, allocating extra memory if necessary */ if (newlinepos+1>=newlinesize) newline=alloc_mem(newline,&newlinesize,newlinesize*2+1); newline[newlinepos++]=thechar; } #ifdef __STDC__ static int display_replace(char *oldline,size_t linecol) #else static int display_replace(oldline,linecol) char *oldline; size_t linecol; #endif { /* Show replacement if in verbose mode level 2. Return 0 if to continue replacement or 1 if to abort */ if (verbose==2) { char tmpold[BUFSIZ],tmpnew[BUFSIZ]; (void)strncpy(tmpold,&oldline[linecol],osl); tmpold[osl]='\0'; (void)strncpy(tmpnew,&newline[newlinepos-nsl],nsl); tmpnew[nsl]='\0'; if (!asked) { char nicemess[BUFSIZ]; (void)snprintf(nicemess,BUFSIZ,"\"%s\" with \"%s\" in line %d",ostr,nstr,linenum); (void)fprintf(stderr,"Line %d: %s\n",linenum,oldline); if (!ask_user(2,nicemess)) { size_t retline; for (retline=0;retline<=oldlen;retline++) newline[retline]=oldline[retline]; return(1); } asked=1; } (void)fprintf(stderr,"\"%s\" -> \"%s\" (line %d, column %d)\n", tmpold,tmpnew,linenum,(int)(linecol+1)); } repcount++; if (!linereps) linecount++; linereps++; return(0); } #ifdef __STDC__ static char *replace_line(char *oldline,size_t strnum) #else static char *replace_line(oldline,strnum) char *oldline; size_t strnum; #endif { /* Replace strings in "oldline" using old/new strings number "strnum" */ size_t oldmax,a=0,comppos,c; char newchar; int maxtimeswarn=0; asked=(prompt!=2); ostr=oldstr[strnum],nstr=newstr[strnum]; newlinepos=0; osl=oldstrlen[strnum]; nsl=newstrlen[strnum]; oldlen=strlen(oldline); oldmax=oldlen-osl; if (linenum>=startline && osl<=oldlen) { if (oldlen && osl) { newline=alloc_mem(newline,&newlinesize,oldlen); while (a=osl-1) comppos--; } copy_char(newchar); } if (display_replace(oldline,a)) return(newline); a+=osl; } } } else /* If old string is "" and the line is also "", then copy the new string in. Otherwise, do nothing to line if it's "" */ if (!oldlen && !osl) { for (c=0;c