home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / strings / midstr / midstr.c
Encoding:
C/C++ Source or Header  |  1987-12-11  |  7.9 KB  |  221 lines

  1. /*
  2.  *   midstr.c - These functions were designed to acquire the same results
  3.  *              as the BASIC equivalents.
  4.  *
  5.  *              Copyright 1987, Jeff French - Lattice 'C' Version 3.20
  6.  *
  7.  *              The function set includes: mid(), right(), left() & instr().
  8.  *
  9.  *              WARNING: The three functions mid(),right() and left() use
  10.  *                       the ANSI compatible function malloc() to allocate
  11.  *                       storage for the 'substring' returned.  Before the
  12.  *                       substring is returned, the storage is released
  13.  *                       with the ANSI compatible function free().  Again,
  14.  *                       in order to keep the 'substring' and still CALL
  15.  *                       one of the above functions again, the substring
  16.  *                       previously returned MUST be stored elsewhere. Use
  17.  *                       of a function such as strcpy() would satisfy this
  18.  *                       requirement. i.e. 'strcpy(hold,mid(str,sub,4,7))'
  19.  *                       where 'hold' is defined as 'char hold[8]' noting
  20.  *                       that 'hold' is 1 byte larger than the maximum size
  21.  *                       of 'sub' since mid(),left() or right() all return
  22.  *                       a pointer to a NULL terminated string.  This is
  23.  *                       NOT required for instr() since this function only
  24.  *                       returns an integer value and can be stored in a
  25.  *                       standard 'int' variable.  Please also note that
  26.  *                       the mid(), left() and right() use error.h or errno
  27.  *                       to define error messages which you may desire to
  28.  *                       change or remove, if an does occur the function
  29.  *                       returns an empty string, however, the calling
  30.  *                       program will continue to execute. Again, this may
  31.  *                       not be desireable.
  32.  *
  33.  *              NOTE:    These routines will NOT work correctly with the
  34.  *                       TURBOC or MICROSOFT (4.0) as they currently are
  35.  *                       due to the way these compilers handle the free()
  36.  *                       function.  TURBOC will work if you remove the
  37.  *                       free(substring) from the mid(), right() and left()
  38.  *                       functions and change error.h to errno.h in the
  39.  *                       include files, however you must remember to free
  40.  *                       the space (especially for larger programs) after
  41.  *                       you have finished using the string (or storing it)
  42.  *                       and BEFORE you call the function again.  I am yet
  43.  *                       to get mid(), right() or left() to work correctly
  44.  *                       with MICROSOFT!
  45.  *
  46.  */
  47. #include "stdio.h"
  48. #include "stdlib.h"
  49. #include "string.h"
  50. #include "error.h"
  51.  
  52. char *mid(), *left(), *right();
  53. int  instr();
  54. /**/
  55. /*
  56.  * This function returns the 'length_desired' number of characters from the
  57.  * 'fullstring' starting at the 'startposition' in 'fullstring', the results
  58.  * are placed in 'substring' (returned to the calling routine).
  59.  *
  60.  * SYNOPSIS:   substring = mid(fullstring,startposition,length_desired);
  61.  *
  62.  *             char *substring;
  63.  *             char *fullstring;
  64.  *             int  startposition;
  65.  *             int  length_desired;
  66.  *
  67.  * PLEASE NOTE: If this function cannot return the entire 'substring'
  68.  *              desired, it will return the largest 'substring' possible
  69.  *              (or a NULL string).
  70.  */
  71.  
  72. char *mid(fullstring,startposition,length_desired)
  73. char *fullstring;
  74. int startposition;
  75. int length_desired;
  76. {
  77.     int actual_length = 0;
  78.     char *substring;
  79.  
  80.     if (startposition < 1 || length_desired < 1) {
  81.     errno = EINVAL;     /* Invalid Argument */
  82.     return("");
  83.     }
  84.     if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
  85.     errno = ENOMEM;     /* No Memory Available */
  86.     return("");
  87.     }
  88.     errno = 0;              /* No Error Occurred */
  89.     startposition--;
  90.     if (strlen(fullstring) - 1 >= startposition) {
  91.     fullstring += startposition;
  92.     while(actual_length < length_desired && *(fullstring + actual_length))
  93.         *(substring + actual_length) = *(fullstring + actual_length++);
  94.     }
  95.     *(substring + actual_length) = NULL;
  96.     free(substring);
  97.     return(substring);
  98. }
  99. /**/
  100. /*
  101.  * This function returns the 'length_desired' number of characters starting
  102.  * with the first character in 'fullstring'.  Results are put in 'substring'
  103.  * (returned to the calling routine).
  104.  *
  105.  * SYNOPSIS:   substring = left(fullstring,length_desired);
  106.  *
  107.  *             char *substring;
  108.  *             char *fullstring;
  109.  *             int  length_desired;
  110.  *
  111.  * PLEASE NOTE: If this function cannot return the entire 'substring'
  112.  *              desired, it will return the largest 'substring' possible
  113.  *              (or a NULL string).
  114.  */
  115.  
  116. char *left(fullstring,length_desired)
  117. char *fullstring;
  118. int length_desired;
  119. {
  120.     int actual_length = 0;
  121.     char *substring;
  122.  
  123.     if (length_desired < 1) {
  124.     errno = EINVAL;     /* Invalid Argument */
  125.     return("");
  126.     }
  127.     if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
  128.     errno = ENOMEM;     /* No Memory Available */
  129.     return("");
  130.     }
  131.     errno = 0;              /* No Error Occurred */
  132.     if (strlen(fullstring) - 1 >= 0)
  133.     while(actual_length < length_desired && *(fullstring + actual_length))
  134.         *(substring + actual_length) = *(fullstring + actual_length++);
  135.     *(substring + actual_length) = NULL;
  136.     free(substring);
  137.     return(substring);
  138. }
  139. /**/
  140. /*
  141.  * This function returns the 'length_desired' number of characters starting
  142.  * with the last character in the 'fullstring'.  The final string is placed
  143.  * in 'substring' (returned to the calling routine).
  144.  *
  145.  * SYNOPSIS:   substring = right(fullstring,length_desired);
  146.  *
  147.  *             char *substring;
  148.  *             char *fullstring;
  149.  *             int  length_desired;
  150.  *
  151.  * PLEASE NOTE: If this function cannot return the entire 'substring'
  152.  *              desired, it will return the largest 'substring' possible
  153.  *              (or a NULL string).
  154.  */
  155.  
  156. char *right(fullstring,length_desired)
  157. char *fullstring;
  158. int length_desired;
  159. {
  160.     int startposition, actual_length = 0;
  161.     char *substring;
  162.  
  163.     if (length_desired < 1) {
  164.     errno = EINVAL;     /* Invalid Argument */
  165.     return("");
  166.     }
  167.     if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
  168.     errno = ENOMEM;     /* No Memory Available */
  169.     return("");
  170.     }
  171.     errno = 0;              /* No error Occurred */
  172.     startposition = strlen(fullstring) - length_desired;
  173.     if (startposition < 0)
  174.     startposition = 0;
  175.     if (strlen(fullstring) - 1 >= startposition) {
  176.     fullstring += startposition;
  177.     while(actual_length < length_desired && *(fullstring + actual_length))
  178.         *(substring + actual_length) = *(fullstring + actual_length++);
  179.     }
  180.     *(substring + actual_length) = NULL;
  181.     free(substring);
  182.     return(substring);
  183. }
  184. /**/
  185. /*
  186.  * This function returns the 'position' of the 'substring' in the 'fullstring'
  187.  * If the entire 'substring' is not found, the function returns 0.
  188.  *
  189.  * SYNOPSIS:   position = instr(fullstring,substring);
  190.  *
  191.  *             int position;
  192.  *             char *fullstring;
  193.  *             char *substring;
  194.  */
  195. int instr(string,substring)
  196. char *string, *substring;
  197. {
  198.     int check_length = 0;
  199.     int location_of_substring = 1;
  200.  
  201.     while (*string) {
  202.     check_length = 0;
  203.     while (*(substring + check_length) == *(string + check_length)
  204.         && *(substring + check_length))
  205.             check_length++;
  206.     if (check_length == strlen(substring))
  207.         return(location_of_substring);
  208.     location_of_substring++;
  209.     string++;
  210.     }
  211.     return(0);
  212. }
  213.      with MICROSOFT!
  214.  *
  215.  */
  216. #include "stdio.h"
  217. #include "stdlib.h"
  218. #include "string.h"
  219. #include "error.h"
  220.  
  221.