diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-04-16 18:28:14 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-04-16 18:28:14 +0000 |
commit | 6c8866fbc243e59d1b3a6d58525a449cbeb9e974 (patch) | |
tree | b7084ce647471f0da512c135fe8b3ec0e01f8a46 /src | |
parent | 9d4509e7626805853a7f469c8c7f5b30dfb67d60 (diff) |
assplode() routine to help with parsing
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@4 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r-- | src/model.c | 21 | ||||
-rw-r--r-- | src/utils.c | 73 | ||||
-rw-r--r-- | src/utils.h | 1 |
3 files changed, 93 insertions, 2 deletions
diff --git a/src/model.c b/src/model.c index d50159d..40ecde8 100644 --- a/src/model.c +++ b/src/model.c @@ -213,13 +213,30 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re } if ( line[s] == 'f' ) { - + char **bits; + int i, n; + n = assplode(line, " \t\r\n", &bits); + printf("The %i bits are:\n", n); + for ( i=0; i<n; i++ ) { + char **sp; + int np, ip; + printf("%3i: '%s'\n", i, bits[i]); + np = assplode(bits[i], "/", &sp); + for ( ip=0; ip<np; ip++ ) { + printf("Subbit %i: '%s'\n", ip, sp[ip]); + } + if ( np < 3 ) { + continue; + } + free(bits[i]); + } + free(bits); } } fclose(fh); - + return model_add(ctx, model); } diff --git a/src/utils.c b/src/utils.c index fad8edb..a407416 100644 --- a/src/utils.c +++ b/src/utils.c @@ -14,6 +14,7 @@ #endif #include <string.h> +#include <stdlib.h> void chomp(char *a) { @@ -28,3 +29,75 @@ void chomp(char *a) { } +/* Return non-zero if c is in delims */ +static int assplode_isdelim(const char c, const char *delims) { + size_t i; + for ( i=0; i<strlen(delims); i++ ) { + if ( c == delims[i] ) return 1; + } + return 0; +} + +static int assplode_extract(char ***pbits, int n, size_t n_captured, size_t start, const char *a) { + char **bits = *pbits; + bits = realloc(bits, sizeof(char *)*(n+1)); + bits[n] = malloc(n_captured+1); + memcpy(bits[n], a+start, n_captured); + bits[n][n_captured] = '\0'; + n++; + *pbits = bits; + return n; +} + +/* Split the string 'a' using 'delims' as a zero-terminated list of deliminators. + * Store each segment in bits[0...n] where n is the number of segments and is the + * return value. pbits = &bits + * Each segment needs to be freed with free() when finished with. + * The array of bits also needs to be freed with free() when finished with, unless + * n=0 in which case bits==NULL + */ +int assplode(const char *a, const char *delims, char ***pbits) { + + size_t i, start, n_captured; + int n; + char **bits; + + n = 0; + i = 0; + n_captured = 0; + start = 0; + bits = NULL; + while ( i < strlen(a) ) { + + if ( assplode_isdelim(a[i], delims) ) { + + if ( n_captured > 0 ) { + /* This is a deliminator after a sequence of non-deliminator chars */ + n = assplode_extract(&bits, n, n_captured, start, a); + } + + n_captured = 0; + + } else { + + if ( n_captured == 0 ) { + /* No characters currently found, so this is the start */ + start = i; + } + n_captured++; + + } + + i++; + + } + /* Left over characters at the end? */ + if ( n_captured > 0 ) { + n = assplode_extract(&bits, n, n_captured, start, a); + } + + *pbits = bits; + return n; + +} + diff --git a/src/utils.h b/src/utils.h index cfbf6ad..ff34a5f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -22,6 +22,7 @@ #define deg2rad(a) ((a)*M_PI/180) extern void chomp(char *a); +extern int assplode(const char *a, const char *delims, char ***pbits); #endif /* UTILS_H */ |