strutil.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  secwatch - Copyright (C) 2006 Nic Stevens -- See COPYING for license details
00003 ------------------------------------------------------------------------------
00004   strutil.c -- string utility functions.
00005   These functions provide some higher-level string functions useful in dealing
00006   with text files.
00007 -----------------------------------------------------------------------------*/
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <ctype.h>
00012 
00017 char ** split(char *sep, char *string) {
00018      char **array = NULL;
00019      int ap, cnt = 0;
00020      char *p, *s;
00021      
00022      if((s = strdup(string)) == NULL)
00023           return NULL;
00024      
00025      p = strtok(s,sep);
00026 
00027      while(p != NULL) {
00028           ap = cnt++;
00029           if((array = realloc(array,((sizeof(char *))*cnt+1))) == NULL) {
00030                free(s);
00031                return NULL;
00032           }
00033           array[ap] = p;
00034           array[cnt] = NULL;
00035           p = strtok(NULL,sep);
00036      }
00037      return array;
00038 }
00039 
00044 char *ltrim(char *s) {
00045      char *p = s;
00046      if(s == NULL || *s == '\0')
00047           return s;
00048      while(isspace(*p))
00049           p++;
00050      return p;
00051 }
00055 char *rtrim(char *s) {
00056      char *p;
00057      if(s == NULL || *s == '\0')
00058           return s;
00059      p = s;
00060      p = ((char *)&s[strlen(s)-1]);
00061      while(isspace(*p))
00062           p--;
00063      *++p = '\0';
00064      return s;
00065 }
00070 char *trim(char *s) {
00071      return rtrim(ltrim(s));
00072 }
00076 int count(char **a) {
00077      int i;
00078      for(i = 0; a[i] != NULL; i++)
00079           ;
00080      return i;
00081 }
00085 int strpos(char *haystack, char *needle) {
00086      int i;
00087      int nlen = strlen(needle);
00088      for(i = 0; i < strlen(haystack); i++) 
00089           if(strncmp(&haystack[i],needle,nlen) == 0)
00090                return i;
00091      return -1;
00092 }

Generated on Tue Oct 31 10:17:23 2006 for secwatch by  doxygen 1.4.6