Guest User

Untitled

a guest
Apr 26th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <stdarg.h>
  2. #include "../util/util.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int searchToken(int *tokenstart, int *tokenlength, int *fulllength, struct buffer *buffer_temp, va_list ap) {
  7.     /* the return value: 1 if a separator was found, else 0 */
  8.     int return_value = 0;
  9.  
  10.     /* initialize *fulllength */
  11.     *fulllength = 0;
  12.  
  13.     /* initialize *tokenlength */
  14.     *tokenlength = 0;
  15.  
  16.     /* define and initialize separator_found for later use in the for-loops *
  17.      * it is used to tell the outer loop if a separator was found in the    *
  18.      * inner loop                               */
  19.     int separator_found = 0;
  20.  
  21.     /* define an array of strings for the separators and a total number of separators */
  22.     char **separator_array;
  23.     int separator_number_total;
  24.     /* allocate memory for <= 20 separators */
  25.     separator_array = malloc(20*sizeof(char*));
  26.  
  27.     /* fill separator_array and set separator_number_total correctly */
  28.     for(int k = 0; k < 20; ++k) {
  29.         /* no further separators are found (the va_list _must_ be terminated by NULL) */
  30.         if((*separator_array[k] = va_arg(ap, char *) == NULL)) {
  31.             separator_number_total = k;
  32.             break;
  33.         }
  34.         if(strlen(separator_array[k]) > 0) {
  35.             continue;
  36.         } else {
  37.             return -1;
  38.         }
  39.  
  40.     }
  41.  
  42.     /* search for leading separators and set *tokenstart    *
  43.      * correctly; if no leading separators are found,   *
  44.      * set *tokenstart to 0                 */
  45.     for(int i = 0; i < buffer_temp->buflen; ++i) {
  46.         /* test for ALL the separators */
  47.         for(int m = 0; m < separator_number_total; ++m) {
  48.             /* separator is found */
  49.             if(strcmp(&buffer_temp->buf[i], &separator_array[m]) == 0) {        // assuming seperators are terminated by '\0'
  50.                 return_value = 1;
  51.                 *tokenstart += strlen(separator_array[m]);
  52.                 /* skip the next strlen(separator_array[m]) chars */
  53.                 i += strlen(separator_array[m]);
  54.                 /* skip the testing of the rest of the separators */
  55.                 break;
  56.             }
  57.         }
  58.     }
  59.     *fulllength = *tokenstart;
  60.     /* overwritten later if a separator was found */
  61.     *tokenlength = buffer_temp->buflen - *tokenstart;
  62.  
  63.  
  64.     /* find the next separator and set *tokenlength */
  65.     for(int j = *tokenstart; j < buffer_temp->buflen; ++j) {
  66.         for(int n = 0; n < separator_number_total; ++n) {
  67.             /* separator is found */
  68.             if(strcmp(&buffer_temp->buf[j], &separator_array[n]) == 0) {
  69.                 return_value = 1;
  70.                 separator_found = 1;
  71.                 /* add the length of the found seperator to fulllength */
  72.                 *fulllength += strlen(separator_array[n]);
  73.                 *tokenlength = j - *tokenstart;
  74.                 /* let the outer loop exit */
  75.                 j = buffer_temp->buflen;
  76.                 /* let the inner loop exit */
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.     *fulllength += *tokenlength;
  82.  
  83.  
  84.     /* in case no separator was found */
  85.     if(return_value == 0) {
  86.         *fulllength = buffer_temp->buflen;
  87.     }
  88.  
  89.     return return_value;
  90. }
  91.  
  92.  
  93. /* extracts a token from a given buffer */
  94. int extractToken(struct buffer *buffer_temp, struct buffer *token, int tokenstart, int tokenlength, int fulllength) {
  95.     /* errors that may occur */
  96.     if(tokenlength > token->bufmax - 1 || fulllength > buffer_temp->buflen || tokenlength + tokenstart > fulllength || tokenstart > buffer_temp->buflen) {
  97.         return -1;
  98.     }
  99.     /* copy the token from *buffer_temp to *token */
  100.     memcpy(token->buf, &buffer_temp->buf[tokenstart], tokenlength);
  101.     token->buflen = tokenlength;
  102.     /* the token is null-terminated */
  103.     token->buf[tokenlength] = 0;
  104.     /* move the values in buffer_temp so the next token can be searched for */
  105.     memmove(buffer_temp->buf, &buffer_temp->buf[fulllength], buffer_temp->buflen - fulllength);
  106.     /* set the length of the buffer lower (fulllength chars were taken away) */
  107.     buffer_temp->buflen -= fulllength;
  108.  
  109.     return 1;
  110. }
Add Comment
Please, Sign In to add comment