Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdarg.h>
- #include "../util/util.h"
- #include <string.h>
- #include <stdlib.h>
- int searchToken(int *tokenstart, int *tokenlength, int *fulllength, struct buffer *buffer_temp, va_list ap) {
- /* the return value: 1 if a separator was found, else 0 */
- int return_value = 0;
- /* initialize *fulllength */
- *fulllength = 0;
- /* initialize *tokenlength */
- *tokenlength = 0;
- /* define and initialize separator_found for later use in the for-loops *
- * it is used to tell the outer loop if a separator was found in the *
- * inner loop */
- int separator_found = 0;
- /* define an array of strings for the separators and a total number of separators */
- char **separator_array;
- int separator_number_total;
- /* allocate memory for <= 20 separators */
- separator_array = malloc(20*sizeof(char*));
- /* fill separator_array and set separator_number_total correctly */
- for(int k = 0; k < 20; ++k) {
- /* no further separators are found (the va_list _must_ be terminated by NULL) */
- if((*separator_array[k] = va_arg(ap, char *) == NULL)) {
- separator_number_total = k;
- break;
- }
- if(strlen(separator_array[k]) > 0) {
- continue;
- } else {
- return -1;
- }
- }
- /* search for leading separators and set *tokenstart *
- * correctly; if no leading separators are found, *
- * set *tokenstart to 0 */
- for(int i = 0; i < buffer_temp->buflen; ++i) {
- /* test for ALL the separators */
- for(int m = 0; m < separator_number_total; ++m) {
- /* separator is found */
- if(strcmp(&buffer_temp->buf[i], &separator_array[m]) == 0) { // assuming seperators are terminated by '\0'
- return_value = 1;
- *tokenstart += strlen(separator_array[m]);
- /* skip the next strlen(separator_array[m]) chars */
- i += strlen(separator_array[m]);
- /* skip the testing of the rest of the separators */
- break;
- }
- }
- }
- *fulllength = *tokenstart;
- /* overwritten later if a separator was found */
- *tokenlength = buffer_temp->buflen - *tokenstart;
- /* find the next separator and set *tokenlength */
- for(int j = *tokenstart; j < buffer_temp->buflen; ++j) {
- for(int n = 0; n < separator_number_total; ++n) {
- /* separator is found */
- if(strcmp(&buffer_temp->buf[j], &separator_array[n]) == 0) {
- return_value = 1;
- separator_found = 1;
- /* add the length of the found seperator to fulllength */
- *fulllength += strlen(separator_array[n]);
- *tokenlength = j - *tokenstart;
- /* let the outer loop exit */
- j = buffer_temp->buflen;
- /* let the inner loop exit */
- break;
- }
- }
- }
- *fulllength += *tokenlength;
- /* in case no separator was found */
- if(return_value == 0) {
- *fulllength = buffer_temp->buflen;
- }
- return return_value;
- }
- /* extracts a token from a given buffer */
- int extractToken(struct buffer *buffer_temp, struct buffer *token, int tokenstart, int tokenlength, int fulllength) {
- /* errors that may occur */
- if(tokenlength > token->bufmax - 1 || fulllength > buffer_temp->buflen || tokenlength + tokenstart > fulllength || tokenstart > buffer_temp->buflen) {
- return -1;
- }
- /* copy the token from *buffer_temp to *token */
- memcpy(token->buf, &buffer_temp->buf[tokenstart], tokenlength);
- token->buflen = tokenlength;
- /* the token is null-terminated */
- token->buf[tokenlength] = 0;
- /* move the values in buffer_temp so the next token can be searched for */
- memmove(buffer_temp->buf, &buffer_temp->buf[fulllength], buffer_temp->buflen - fulllength);
- /* set the length of the buffer lower (fulllength chars were taken away) */
- buffer_temp->buflen -= fulllength;
- return 1;
- }
Add Comment
Please, Sign In to add comment