home *** CD-ROM | disk | FTP | other *** search
- /*
- * midstr.c - These functions were designed to acquire the same results
- * as the BASIC equivalents.
- *
- * Copyright 1987, Jeff French - Lattice 'C' Version 3.20
- *
- * The function set includes: mid(), right(), left() & instr().
- *
- * WARNING: The three functions mid(),right() and left() use
- * the ANSI compatible function malloc() to allocate
- * storage for the 'substring' returned. Before the
- * substring is returned, the storage is released
- * with the ANSI compatible function free(). Again,
- * in order to keep the 'substring' and still CALL
- * one of the above functions again, the substring
- * previously returned MUST be stored elsewhere. Use
- * of a function such as strcpy() would satisfy this
- * requirement. i.e. 'strcpy(hold,mid(str,sub,4,7))'
- * where 'hold' is defined as 'char hold[8]' noting
- * that 'hold' is 1 byte larger than the maximum size
- * of 'sub' since mid(),left() or right() all return
- * a pointer to a NULL terminated string. This is
- * NOT required for instr() since this function only
- * returns an integer value and can be stored in a
- * standard 'int' variable. Please also note that
- * the mid(), left() and right() use error.h or errno
- * to define error messages which you may desire to
- * change or remove, if an does occur the function
- * returns an empty string, however, the calling
- * program will continue to execute. Again, this may
- * not be desireable.
- *
- * NOTE: These routines will NOT work correctly with the
- * TURBOC or MICROSOFT (4.0) as they currently are
- * due to the way these compilers handle the free()
- * function. TURBOC will work if you remove the
- * free(substring) from the mid(), right() and left()
- * functions and change error.h to errno.h in the
- * include files, however you must remember to free
- * the space (especially for larger programs) after
- * you have finished using the string (or storing it)
- * and BEFORE you call the function again. I am yet
- * to get mid(), right() or left() to work correctly
- * with MICROSOFT!
- *
- */
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "error.h"
-
- char *mid(), *left(), *right();
- int instr();
- /**/
- /*
- * This function returns the 'length_desired' number of characters from the
- * 'fullstring' starting at the 'startposition' in 'fullstring', the results
- * are placed in 'substring' (returned to the calling routine).
- *
- * SYNOPSIS: substring = mid(fullstring,startposition,length_desired);
- *
- * char *substring;
- * char *fullstring;
- * int startposition;
- * int length_desired;
- *
- * PLEASE NOTE: If this function cannot return the entire 'substring'
- * desired, it will return the largest 'substring' possible
- * (or a NULL string).
- */
-
- char *mid(fullstring,startposition,length_desired)
- char *fullstring;
- int startposition;
- int length_desired;
- {
- int actual_length = 0;
- char *substring;
-
- if (startposition < 1 || length_desired < 1) {
- errno = EINVAL; /* Invalid Argument */
- return("");
- }
- if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
- errno = ENOMEM; /* No Memory Available */
- return("");
- }
- errno = 0; /* No Error Occurred */
- startposition--;
- if (strlen(fullstring) - 1 >= startposition) {
- fullstring += startposition;
- while(actual_length < length_desired && *(fullstring + actual_length))
- *(substring + actual_length) = *(fullstring + actual_length++);
- }
- *(substring + actual_length) = NULL;
- free(substring);
- return(substring);
- }
- /**/
- /*
- * This function returns the 'length_desired' number of characters starting
- * with the first character in 'fullstring'. Results are put in 'substring'
- * (returned to the calling routine).
- *
- * SYNOPSIS: substring = left(fullstring,length_desired);
- *
- * char *substring;
- * char *fullstring;
- * int length_desired;
- *
- * PLEASE NOTE: If this function cannot return the entire 'substring'
- * desired, it will return the largest 'substring' possible
- * (or a NULL string).
- */
-
- char *left(fullstring,length_desired)
- char *fullstring;
- int length_desired;
- {
- int actual_length = 0;
- char *substring;
-
- if (length_desired < 1) {
- errno = EINVAL; /* Invalid Argument */
- return("");
- }
- if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
- errno = ENOMEM; /* No Memory Available */
- return("");
- }
- errno = 0; /* No Error Occurred */
- if (strlen(fullstring) - 1 >= 0)
- while(actual_length < length_desired && *(fullstring + actual_length))
- *(substring + actual_length) = *(fullstring + actual_length++);
- *(substring + actual_length) = NULL;
- free(substring);
- return(substring);
- }
- /**/
- /*
- * This function returns the 'length_desired' number of characters starting
- * with the last character in the 'fullstring'. The final string is placed
- * in 'substring' (returned to the calling routine).
- *
- * SYNOPSIS: substring = right(fullstring,length_desired);
- *
- * char *substring;
- * char *fullstring;
- * int length_desired;
- *
- * PLEASE NOTE: If this function cannot return the entire 'substring'
- * desired, it will return the largest 'substring' possible
- * (or a NULL string).
- */
-
- char *right(fullstring,length_desired)
- char *fullstring;
- int length_desired;
- {
- int startposition, actual_length = 0;
- char *substring;
-
- if (length_desired < 1) {
- errno = EINVAL; /* Invalid Argument */
- return("");
- }
- if ((substring = malloc((unsigned) (length_desired + 1))) == NULL) {
- errno = ENOMEM; /* No Memory Available */
- return("");
- }
- errno = 0; /* No error Occurred */
- startposition = strlen(fullstring) - length_desired;
- if (startposition < 0)
- startposition = 0;
- if (strlen(fullstring) - 1 >= startposition) {
- fullstring += startposition;
- while(actual_length < length_desired && *(fullstring + actual_length))
- *(substring + actual_length) = *(fullstring + actual_length++);
- }
- *(substring + actual_length) = NULL;
- free(substring);
- return(substring);
- }
- /**/
- /*
- * This function returns the 'position' of the 'substring' in the 'fullstring'
- * If the entire 'substring' is not found, the function returns 0.
- *
- * SYNOPSIS: position = instr(fullstring,substring);
- *
- * int position;
- * char *fullstring;
- * char *substring;
- */
- int instr(string,substring)
- char *string, *substring;
- {
- int check_length = 0;
- int location_of_substring = 1;
-
- while (*string) {
- check_length = 0;
- while (*(substring + check_length) == *(string + check_length)
- && *(substring + check_length))
- check_length++;
- if (check_length == strlen(substring))
- return(location_of_substring);
- location_of_substring++;
- string++;
- }
- return(0);
- }
- with MICROSOFT!
- *
- */
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "error.h"
-
-