home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / PFC / SRC / STRINS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  3.7 KB  |  151 lines

  1. /* (c) 1985, Phoenix Computer Products Corp. and Novum Organum, Inc. */
  2. /***
  3. * name :    strinsc - insert a character into a string
  4. *
  5. * synopsis:    char    *strinsc ( str, chr )
  6. *        char    *str;         string to work on 
  7. *        int    chr;         character to insert 
  8. *
  9. * description:    Inserts a character at the beginning of a string.
  10. *
  11. * returns:    An echo of the input string address.
  12. *        
  13. *
  14. * (C) novum organum, inc. 1985
  15. *
  16. ***/
  17. #include "cptpdf.h"
  18.  
  19. char    *strinsc (str,chr)
  20.     char    *str;
  21.     int    chr;
  22. {
  23.     mempp ( str + 1, str, strlen(str) + 1); /* make room */
  24.     *str = chr;            /* insert character */
  25.     return    (str);
  26. }
  27.  
  28.  
  29. #ifdef use
  30.  
  31. /***
  32. * name :    strninsc - insert a char, truncate to maximum buffer length
  33. *
  34. * synopsis:    char    *strinsc ( str, chr, maxlen )
  35. *        char    *str;        string to work on 
  36. *        int    chr;        character to insert 
  37. *        int    maxlen;        maximum output buffer length 
  38. *
  39. * description:    Inserts a character at the beginning of a string. If the new
  40. *        length would exceed the buffer size (stralen()+1), the last
  41. *        character is deleted before insertion.
  42. *
  43. * returns:    An echo of the input string address.
  44. *        
  45. *
  46. * (C) novum organum, inc. 1985
  47. *
  48. ***/
  49.  
  50. char    *strninsc ( str, chr, maxlen )
  51.     char    *str;
  52.     int    chr, maxlen;
  53. {
  54.     char    *strinsc();
  55.  
  56.     maxlen -= 2;            /* maximum offset to EOS */
  57.  
  58.     if    (maxlen >= 0)        /* if STR will hold even 1 char */
  59.         {
  60.         if    (strlen(str) > maxlen)/* if STR is too long */
  61.             *(str+maxlen) = EOS;    /* truncate it */
  62.         strinsc (str, chr);    /* insert the character */
  63.         }
  64.     return    ( str );
  65. }
  66.  
  67.  
  68. /***
  69. * name :    strins - insert a string
  70. *
  71. * synopsis:    char    *strinsc ( str, insert_str )
  72. *        char    *str;        string to work on 
  73. *        char    *insert_str;    string to insert 
  74. *
  75. * description:    Inserts a sub-string at the beginning of a string. 
  76. *
  77. * returns:    An echo of the original string address (STR).
  78. *        
  79. *
  80. * (C) novum organum, inc. 1985
  81. *
  82. ***/
  83.  
  84. char    *strins (str, insert_str)
  85.     char    *str, *insert_str;
  86. {
  87.     int    inslen;
  88.  
  89.     inslen = stralen (insert_str);    /* length of insert string */
  90.  
  91.     if    (inslen > 0) 
  92.         {
  93.         mempp(str+inslen,str,(stralen(str)+1));/* move original */
  94.         /* moved an extra byte to make sure 3rd arg non-zero */
  95.         mempp(str, insert_str, inslen); /* move the insert */
  96.         }
  97.     return    (str);
  98. }
  99.  
  100. /***
  101. * name :    strnins - insert a string, truncate to maximum buffer length
  102. *
  103. * synopsis:    char    *strinsc ( str, insert_str, maxlen )
  104. *        char    *str;        string to work on 
  105. *        char    *insert_str;    string to insert 
  106. *        int    maxlen;        maximum output buffer length 
  107. *
  108. * description:    Inserts a sub-string at the beginning of a string. If the
  109. *        new length would exceed the buffer size (stralen()+1), the
  110. *        string is truncated before insertion.
  111. *
  112. * returns:    An echo of the original string address (STR).
  113. *        
  114. *
  115. * (C) novum organum, inc. 1985
  116. *
  117. ***/
  118.  
  119. char    *strnins (str, insert_str, maxlen)
  120.     char    *str, *insert_str;
  121.     int    maxlen;
  122. {
  123.     int    inslen, curlen;
  124.  
  125.     maxlen--;            /* exclude final end of string */
  126.     inslen = stralen (insert_str);    /* length of insert string */
  127.  
  128.     if    (maxlen <= 0 || inslen <= 0) /* nothing to be done */
  129.         goto end;
  130.  
  131.     if    (inslen >= maxlen)    /* only the insert fits */
  132.         inslen = maxlen;    /* limit size of insert */
  133.     else                /* some of the original STR fits */
  134.         {
  135.         curlen = stralen (str);  /* size of original */
  136.  
  137.         if    ( inslen+curlen > maxlen )/* if not room for both */
  138.             curlen = maxlen-inslen;   /* limit size of original */
  139.         else                  /* there is room for both */
  140.             maxlen = inslen+curlen;   /* mark where EOS goes */
  141.         mempp(str+inslen,str,(curlen+1));/* move original */
  142.         /* moved an extra byte to make sure 3rd arg non-zero */
  143.         }
  144.     mempp(str, insert_str, inslen); /* move the insert */
  145.     *(str+maxlen) = EOS;        /* put in final end of string */
  146. end:
  147.     return    (str);
  148. }
  149.  
  150. #endif
  151.