home *** CD-ROM | disk | FTP | other *** search
-
- #define TEST 1 /* create main program if TEST = 1. */
- #include "CINFO.H" /* compiler selection */
- /*******************************************************/
- /* */
- /* DYSTR.C */
- /* */
- /* Some routines for the manipulation of strings */
- /* in dynamic storage */
- /* storage. */
- /* */
- /* (C) 1988 Dr.-Ing. Dietmar Schroeder & TOOLBOX */
- /* V1.7 06.10.88 */
- /* */
- /*******************************************************/
-
- /* Some definitions. */
- #ifdef VAXC
- #include STDIO.H
- #else
- #include "STDIO.H"
- #endif
-
- #ifdef TURBOC
- #include <stdlib.h>
- #include <string.h>
-
- char *strsave(char *);
- char *strsave(Str) char *Str;
-
- {
- char *p;
- p = (char *)calloc(strlen(Str)+1,1);
- p = strncpy(p,Str,strlen(Str)+1);
- return p;
- }
-
- #define cfree free
- int index(char *, char *);
- int index(Str1, Str2) char *Str1, *Str2;
- {
- char * Res;
-
- Res = strstr(Str1,Str2);
- if (Res == NULL)
- return -1;
- else
- return ((int)(Res-Str1));
- }
-
- /* function prototypes */
- char *DelAt(unsigned, unsigned, char *);
- char *InsAt(unsigned, char *, char *);
- char *DyApp(char *, char *);
- char *DyCut(char *, char *);
- char *SubstAll(char *, char *, char*);
- void DyPrint(char *);
- void DyWrite(FILE *, char *);
- #endif
-
- #ifndef TURBOC
- extern strcpy(), strncpy(), strcat(), strlen(), cfree();
- extern char *strsave(), *calloc();
- extern index(), fprintf();
- #endif
-
- /*------------- InsAt -----------------------------------*/
-
- char *InsAt(pos, ssins, dstr)
- unsigned pos; char *ssins, *dstr;
-
- /* Inserts the string ssins at position pos into string
- dstr. String dstr must be allocated by calloc and will
- be released on return. The return string is allocated
- by calloc. In case of error, NULL is returned.
- */
-
- {
- char *zstr;
- unsigned slen;
-
- /* Check dstr. */
- if (dstr == NULL) return NULL;
-
- /* Check if pos inbetween of dstr. */
- if ( (slen= strlen(dstr) ) < pos ) pos = slen;
-
-
- /* Allocate and copy. */
-
- if ((zstr=(char *)calloc(strlen(ssins)+slen+1,1)) != NULL){
- zstr = strncpy(zstr, dstr, pos); *(zstr + pos) = '\0';
- zstr = strcat(zstr, ssins);
- zstr = strcat(zstr, dstr + pos);
- cfree(dstr);
- }
- return zstr;
- }
-
-
- /*------------- DelAt -----------------------------------*/
-
- char *DelAt(pos, n, dstr) unsigned pos,n; char *dstr;
-
- /* Deletes at position pos in string dstr n characters.
- String dstr must be allocated by calloc and will be
- released on return. The return string is allocated by
- calloc. In case of error, NULL is returned.
- */
-
- {
- char *zstr;
- unsigned slen;
-
- /* Check dstr. */
- if (dstr == NULL) return NULL;
-
- /* Check if pos and n correct. */
- if ((slen=strlen(dstr)) < pos) pos = slen;
- if ((slen-pos) < n) n = slen - pos;
-
-
- /* Allocate and copy. */
-
- if ((zstr=(char *)calloc(slen - n + 1, 1)) != NULL ) {
- zstr = strncpy(zstr,(char *)dstr, pos);
- *(zstr + pos) = '\0';
- zstr = strcat(zstr, dstr + (pos + n) );
-
- cfree(dstr);
- }
- return zstr;
- }
-
-
- /*------------- DyApp -----------------------------------*/
-
- char *DyApp(dstr1, dstr2) char *dstr1, *dstr2;
-
- /* DyApp returns a string containing dstr2 appended to
- dstr1. Strings dstr1 and dstr2 must be allocated by
- calloc; they will be deallocated on return. The return
- string is allocated by calloc. In case of error,
- NULL is returned.
- */
-
- {
- char *zstr;
-
- if ( (dstr1 == NULL) || (dstr2 == NULL) ) return NULL;
- /* Allocate and copy. */
-
- if ((zstr=calloc(strlen(dstr1)+strlen(dstr2)+1,1))!= NULL){
- zstr = strcpy(zstr, dstr1);
- zstr = strcat(zstr, dstr2);
-
- cfree(dstr1);
- cfree(dstr2);
- }
-
- return zstr;
- }
-
-
- /*------------- DyCut -----------------------------------*/
-
- char *DyCut(ssbeg, ssend) char *ssbeg, *ssend;
-
- /* Returns a string which is located between pointers ssbeg
- and ssend, not including *ssend. The return string is
- allocated by calloc. In case of error is NULL, and in
- case of ssend < ssbeg an empty string is returned.
- */
-
- {
- char *zstr;
- int n;
-
- /* Check pointers. */
- if ( (n= ssend-ssbeg) <= 0 ) return strsave("");
-
- /* Allocate and copy. */
- if ((zstr = (char *)calloc(n + 1, 1)) != NULL ) {
- zstr = strncpy(zstr, ssbeg, n);
- *(zstr + n) = '\0';
- }
- return zstr;
- }
-
-
-
- /*------------- SubstAll --------------------------------*/
-
- char *SubstAll(ssub1, ssub2, sstr)
- char *ssub1, *ssub2, *sstr;
-
- /* Substitutes all the dummies %P1% and %P2% in string
- sstr with the actual values of ssub1 and ssub2. The
- return string is allocated by calloc. In case of error,
- NULL is returned.
- */
-
- {
- char *zstr;
- int locp; /* location of parameter in sstr. */
-
- /* Allocate and copy string. */
- zstr = strsave(sstr);
-
- /* Substitute %P1%. */
- while ((zstr != NULL) && ((locp=index(zstr,"%P1%")) >= 0))
- {zstr = DelAt(locp, (unsigned) 4, zstr);
- zstr = InsAt(locp, ssub1, zstr);
- }
-
- /* Substitute %P2%. */
- while ((zstr != NULL) && ((locp=index(zstr,"%P2%")) >= 0))
- {zstr = DelAt(locp, (unsigned) 4, zstr);
- zstr = InsAt(locp, ssub2, zstr);
- }
-
- return zstr;
- }
-
-
- /*------------- DyPrint ---------------------------------*/
-
- char *NoMem = "\nNot enough dynamic memory.\n";
-
-
- #ifdef TURBOC
- void DyPrint(str) char *str;
- #else
- DyPrint(str) char *str;
- #endif
-
- /* Prints the string str on the terminal. If str is NULL
- an error message is printed.
- */
-
- {unsigned i;
-
- if (str != NULL)
- {
- for (i = 0; str[i]; i++) printf("%c", str[i]);
- }
-
- else
- printf("\7%s\7", NoMem);
- }
-
-
- /*------------- DyWrite ---------------------------------*/
-
- #ifdef TURBOC
- void DyWrite(fp,str) FILE *fp; char *str;
- #else
- DyWrite(fp,str) FILE *fp; char *str;
- #endif
-
- /* Prints the string str to file *fp. If str is NULL
- an error message is printed.
- */
-
- {unsigned i;
-
- if (str != NULL)
- {
- for(i = 0;str[i]; i++) fprintf(fp, "%c", str[i]);
- }
- else
- { fprintf(fp, "%s", NoMem);
- printf("\7%s\7", NoMem);
- }
- }
-
-
- #if TEST
- /*--------- MAIN TEST PROGRAM ---------------------------*/
-
- main()
- {
- char
- *line,
- *lineIns, *lineDel, *lineApp1, *lineApp2,
- *lineCut ="Die bloedsinnige Funktion DyCut ist in \
- Ordnung, hast du gedacht.\n",
- *lineSubst = "\nFunktion SubstAll ist %P1% in %P2%.\n",
- *line1, *line2, *line3;
-
-
- lineIns = strsave("Funktion InsAt ist fehlerbehaftet.\n");
- lineDel = strsave("Funktion DelAt ist nicht in Ordnung.\n");
- lineApp1 = strsave("Funktion DyApp ");
- lineApp2 = strsave("ist in Ordnung.\n");
-
- lineIns = InsAt(19, "nicht ", lineIns);
- DyPrint(lineIns);
- lineDel = DelAt(19, 6, lineDel);
- DyPrint(lineDel);
- lineApp1 = DyApp(lineApp1, lineApp2);
- DyPrint(lineApp1);
- line = DyCut(lineCut+17, lineCut+46);
- DyPrint(line);
- printf(".\n");
- cfree(line);
-
- line = DyCut(lineCut+46, lineCut+17);
- DyPrint(line);
- cfree(line);
-
- line = SubstAll("ziemlich", "Ordnung", lineSubst);
- DyPrint(line);
- cfree(line);
-
- line1 = strsave("Es koennen auch ");
- line2 = strsave("mehrere Funktionen ");
- DyPrint( DyApp(line1, DyApp(line2,
- SubstAll("inein", "schacht",
- "%P1%ander ge%P2%elt werden.\n"))));
-
- line3 = strsave(
- "Die Ausgangsvariable kann auch mit einer ");
- line3 = DyApp(line3,
- strsave("Eingangsvariablen identisch sein.\n"));
- DyPrint(line3);
-
- printf("\n\nJetzt muss noch eine Fehlermeldung kommen:");
- DyPrint(NULL);
-
- }
- #endif