home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1989 / 02 / extra / dystr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-25  |  7.6 KB  |  333 lines

  1.  
  2. #define TEST 1     /* create main program if TEST = 1. */
  3. #include "CINFO.H" /* compiler selection               */
  4. /*******************************************************/
  5. /*                                                     */
  6. /*              DYSTR.C                                */
  7. /*                                                     */
  8. /* Some routines for the manipulation of strings       */
  9. /* in dynamic storage                                  */
  10. /* storage.                                            */
  11. /*                                                     */
  12. /* (C) 1988 Dr.-Ing. Dietmar Schroeder & TOOLBOX       */
  13. /*               V1.7    06.10.88                      */
  14. /*                                                     */
  15. /*******************************************************/
  16.  
  17. /* Some definitions. */
  18. #ifdef VAXC
  19. #include STDIO.H
  20. #else
  21. #include "STDIO.H"
  22. #endif
  23.  
  24. #ifdef TURBOC
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. char *strsave(char *);
  29. char *strsave(Str) char *Str;
  30.  
  31. {
  32.  char *p;
  33.  p = (char *)calloc(strlen(Str)+1,1);
  34.  p = strncpy(p,Str,strlen(Str)+1);
  35.  return p;
  36. }
  37.  
  38. #define cfree free
  39. int index(char *, char *);
  40. int index(Str1, Str2) char *Str1, *Str2;
  41. {
  42.  char * Res;
  43.  
  44.  Res = strstr(Str1,Str2);
  45.  if (Res == NULL)
  46.    return -1;
  47.  else
  48.    return ((int)(Res-Str1));
  49. }
  50.  
  51. /* function prototypes */
  52. char *DelAt(unsigned, unsigned, char *);
  53. char *InsAt(unsigned, char *, char *);
  54. char *DyApp(char *, char *);
  55. char *DyCut(char *, char *);
  56. char *SubstAll(char *, char *, char*);
  57. void DyPrint(char *);
  58. void DyWrite(FILE *, char *);
  59. #endif
  60.  
  61. #ifndef TURBOC
  62. extern strcpy(), strncpy(), strcat(), strlen(), cfree();
  63. extern char *strsave(), *calloc();
  64. extern index(), fprintf();
  65. #endif
  66.  
  67. /*------------- InsAt -----------------------------------*/
  68.  
  69. char *InsAt(pos, ssins, dstr)
  70.       unsigned pos; char *ssins, *dstr;
  71.  
  72. /* Inserts the string ssins at position pos into string
  73.    dstr. String dstr must be allocated by calloc and will
  74.    be released on return. The return string is allocated
  75.    by calloc. In case of error, NULL is returned.
  76. */
  77.  
  78. {
  79. char *zstr;
  80. unsigned slen;
  81.  
  82. /* Check dstr. */
  83. if (dstr == NULL) return NULL;
  84.  
  85. /* Check if pos inbetween of dstr. */
  86. if (  (slen= strlen(dstr) ) < pos  ) pos = slen;
  87.  
  88.  
  89. /* Allocate and copy. */
  90.  
  91. if ((zstr=(char *)calloc(strlen(ssins)+slen+1,1)) != NULL){
  92.   zstr = strncpy(zstr, dstr, pos); *(zstr + pos) = '\0';
  93.   zstr = strcat(zstr, ssins);
  94.   zstr = strcat(zstr, dstr + pos);
  95.   cfree(dstr);
  96. }
  97. return zstr;
  98. }
  99.  
  100.  
  101. /*------------- DelAt -----------------------------------*/
  102.  
  103. char *DelAt(pos, n, dstr)   unsigned pos,n; char *dstr;
  104.  
  105. /* Deletes at position pos in string dstr n characters.
  106.    String dstr must be allocated by calloc and will be
  107.    released on return. The return string is allocated by
  108.    calloc. In case of error, NULL is returned.
  109. */
  110.  
  111. {
  112. char *zstr;
  113. unsigned slen;
  114.  
  115. /* Check dstr. */
  116. if (dstr == NULL) return NULL;
  117.  
  118. /* Check if pos and n correct. */
  119. if ((slen=strlen(dstr)) < pos) pos = slen;
  120. if ((slen-pos) < n) n   = slen - pos;
  121.  
  122.  
  123. /* Allocate and copy. */
  124.  
  125. if ((zstr=(char *)calloc(slen - n + 1, 1)) != NULL ) {
  126.   zstr = strncpy(zstr,(char *)dstr, pos);
  127.   *(zstr + pos) = '\0';
  128.   zstr = strcat(zstr, dstr + (pos + n) );
  129.  
  130.   cfree(dstr);
  131. }
  132. return zstr;
  133. }
  134.  
  135.  
  136. /*------------- DyApp -----------------------------------*/
  137.  
  138. char *DyApp(dstr1, dstr2)   char *dstr1, *dstr2;
  139.  
  140. /* DyApp returns a string containing dstr2 appended to
  141.    dstr1. Strings dstr1 and dstr2 must be allocated by
  142.    calloc; they will be deallocated on return. The return
  143.    string is allocated by calloc. In case of error,
  144.    NULL is returned.
  145. */
  146.  
  147. {
  148. char *zstr;
  149.  
  150. if ( (dstr1 == NULL) || (dstr2 == NULL) ) return NULL;
  151. /* Allocate and copy. */
  152.  
  153. if ((zstr=calloc(strlen(dstr1)+strlen(dstr2)+1,1))!= NULL){
  154.   zstr = strcpy(zstr, dstr1);
  155.   zstr = strcat(zstr, dstr2);
  156.  
  157.   cfree(dstr1);
  158.   cfree(dstr2);
  159. }
  160.  
  161. return zstr;
  162. }
  163.  
  164.  
  165. /*------------- DyCut -----------------------------------*/
  166.  
  167. char *DyCut(ssbeg, ssend)   char *ssbeg, *ssend;
  168.  
  169. /* Returns a string which is located between pointers ssbeg
  170.    and ssend, not including *ssend. The return string is
  171.    allocated by calloc. In case of error is NULL, and in
  172.    case of ssend < ssbeg an empty string is returned.
  173. */
  174.  
  175. {
  176. char *zstr;
  177. int n;
  178.  
  179. /* Check pointers. */
  180. if ( (n= ssend-ssbeg) <= 0 )  return strsave("");
  181.  
  182. /* Allocate and copy. */
  183. if ((zstr = (char *)calloc(n + 1, 1)) != NULL ) {
  184.   zstr = strncpy(zstr, ssbeg, n);
  185.   *(zstr + n) = '\0';
  186. }
  187. return zstr;
  188. }
  189.  
  190.  
  191.  
  192. /*------------- SubstAll --------------------------------*/
  193.  
  194. char *SubstAll(ssub1, ssub2, sstr)
  195.            char *ssub1, *ssub2, *sstr;
  196.  
  197. /* Substitutes all the dummies %P1% and %P2% in string
  198.    sstr with the actual values of ssub1 and ssub2. The
  199.    return string is allocated by calloc. In case of error,
  200.    NULL is returned.
  201. */
  202.  
  203. {
  204. char *zstr;
  205. int locp;          /* location of parameter in sstr. */
  206.  
  207. /* Allocate and copy string. */
  208. zstr = strsave(sstr);
  209.  
  210. /* Substitute %P1%. */
  211. while ((zstr != NULL) && ((locp=index(zstr,"%P1%")) >= 0))
  212.      {zstr = DelAt(locp, (unsigned) 4, zstr);
  213.       zstr = InsAt(locp, ssub1, zstr);
  214.      }
  215.  
  216. /* Substitute %P2%. */
  217. while ((zstr != NULL) && ((locp=index(zstr,"%P2%")) >= 0))
  218.      {zstr = DelAt(locp, (unsigned) 4, zstr);
  219.       zstr = InsAt(locp, ssub2, zstr);
  220.      }
  221.  
  222. return zstr;
  223. }
  224.  
  225.  
  226. /*------------- DyPrint ---------------------------------*/
  227.  
  228. char *NoMem = "\nNot enough dynamic memory.\n";
  229.  
  230.  
  231. #ifdef TURBOC
  232. void DyPrint(str)   char *str;
  233. #else
  234. DyPrint(str)   char *str;
  235. #endif
  236.  
  237. /* Prints the string str on the terminal. If str is NULL
  238.    an error message is printed.
  239. */
  240.  
  241. {unsigned i;
  242.  
  243. if (str != NULL)
  244.   {
  245.    for (i = 0; str[i]; i++) printf("%c", str[i]);
  246.   }
  247.  
  248. else
  249.    printf("\7%s\7", NoMem);
  250. }
  251.  
  252.  
  253. /*------------- DyWrite ---------------------------------*/
  254.  
  255. #ifdef TURBOC
  256. void DyWrite(fp,str)   FILE *fp; char *str;
  257. #else
  258. DyWrite(fp,str)   FILE *fp; char *str;
  259. #endif
  260.  
  261. /* Prints the string str to file *fp. If str is NULL
  262.    an error message is printed.
  263. */
  264.  
  265. {unsigned i;
  266.  
  267. if (str != NULL)
  268.   {
  269.    for(i  = 0;str[i]; i++) fprintf(fp, "%c", str[i]);
  270.   }
  271. else
  272.    { fprintf(fp, "%s", NoMem);
  273.      printf("\7%s\7", NoMem);
  274.    }
  275. }
  276.  
  277.  
  278. #if TEST
  279. /*--------- MAIN TEST PROGRAM ---------------------------*/
  280.  
  281. main()
  282. {
  283. char
  284. *line,
  285. *lineIns, *lineDel, *lineApp1, *lineApp2,
  286. *lineCut ="Die bloedsinnige Funktion DyCut ist in \
  287. Ordnung, hast du gedacht.\n",
  288. *lineSubst = "\nFunktion SubstAll ist %P1% in %P2%.\n",
  289. *line1, *line2, *line3;
  290.  
  291.  
  292. lineIns = strsave("Funktion InsAt ist fehlerbehaftet.\n");
  293. lineDel = strsave("Funktion DelAt ist nicht in Ordnung.\n");
  294. lineApp1 = strsave("Funktion DyApp ");
  295. lineApp2 = strsave("ist in Ordnung.\n");
  296.  
  297. lineIns = InsAt(19, "nicht ", lineIns);
  298. DyPrint(lineIns);
  299. lineDel = DelAt(19, 6, lineDel);
  300. DyPrint(lineDel);
  301. lineApp1 = DyApp(lineApp1, lineApp2);
  302. DyPrint(lineApp1);
  303. line = DyCut(lineCut+17, lineCut+46);
  304. DyPrint(line);
  305. printf(".\n");
  306. cfree(line);
  307.  
  308. line = DyCut(lineCut+46, lineCut+17);
  309. DyPrint(line);
  310. cfree(line);
  311.  
  312. line = SubstAll("ziemlich", "Ordnung", lineSubst);
  313. DyPrint(line);
  314. cfree(line);
  315.  
  316. line1 = strsave("Es koennen auch ");
  317. line2 = strsave("mehrere Funktionen ");
  318. DyPrint( DyApp(line1, DyApp(line2,
  319.                 SubstAll("inein", "schacht",
  320.             "%P1%ander ge%P2%elt werden.\n"))));
  321.  
  322. line3 = strsave(
  323.        "Die Ausgangsvariable kann auch mit einer ");
  324. line3 = DyApp(line3,
  325.     strsave("Eingangsvariablen identisch sein.\n"));
  326. DyPrint(line3);
  327.  
  328. printf("\n\nJetzt muss noch eine Fehlermeldung kommen:");
  329. DyPrint(NULL);
  330.  
  331. }
  332. #endif
  333.