BRL-CAD
columnparse.c
Go to the documentation of this file.
1 /* C O L U M N P A R S E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup librt */
21 /** @{ */
22 /** @file librt/columnparse.c
23  */
24 
25 #include "common.h"
26 
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <regex.h>
32 #include "bio.h"
33 
34 #include "bn.h"
35 #include "db.h"
36 #include "raytrace.h"
37 
38 
40  int col_cnt;
41  int *col_sizes;
42  char **col_attrnames;
43 };
44 
45 
46 static void
47 parse_line(struct bu_vls *line, struct col_properties *cp)
48 {
49  int currentposstart = 0;
50  int currentposend = 0;
51  int currentcol = -1;
52  struct bu_vls workingstring = BU_VLS_INIT_ZERO;
53 
54  while (currentcol < cp->col_cnt) {
55  currentcol++;
56  currentposstart = currentposend;
57  currentposend += cp->col_sizes[currentcol];
58  bu_vls_trunc(&workingstring, 0);
59  bu_vls_strncpy(&workingstring, bu_vls_addr(line)+currentposstart, currentposend - currentposstart);
60  bu_vls_trimspace(&workingstring);
61  bu_log("column %d contents: %s\n", currentcol, bu_vls_addr(&workingstring));
62  }
63 }
64 
65 
66 static void
67 find_columns(char *name, struct col_properties *cp)
68 {
69  regex_t compiled_regex;
70  regmatch_t *result_locations;
71  int ret, components;
72  struct bu_vls modelregex = BU_VLS_INIT_ZERO;
73  struct bu_vls attrregex = BU_VLS_INIT_ZERO;
74  struct bu_vls workingstring1 = BU_VLS_INIT_ZERO;
75  struct bu_vls workingstring2 = BU_VLS_INIT_ZERO;
76  struct bu_vls testresult = BU_VLS_INIT_ZERO;
77 
78  bu_vls_sprintf(&modelregex, "([ ]*Model Name[ ]*)(.*)");
79  bu_vls_sprintf(&attrregex, "([a-zA-Z0-9]*[ ]*)([a-zA-Z0-9].*$)?");
80  bu_vls_sprintf(&workingstring1, "%s", name);
81 
82  ret=regcomp(&compiled_regex, bu_vls_addr(&modelregex), REG_EXTENDED);
83  components = 2;
84  result_locations = (regmatch_t *)bu_calloc(components + 1, sizeof(regmatch_t), "array to hold answers from regex");
85 
86  ret=regexec(&compiled_regex, bu_vls_addr(&workingstring1), components+1, result_locations, 0);
87 
88  bu_vls_trunc(&testresult, 0);
89  bu_vls_strncpy(&testresult, bu_vls_addr(&workingstring1)+result_locations[1].rm_so, result_locations[1].rm_eo - result_locations[1].rm_so);
90  cp->col_sizes[0] = bu_vls_strlen(&testresult);
91  bu_log("stringlength:%d\n", cp->col_sizes[0]);
92  bu_vls_trimspace(&testresult);
93  cp->col_attrnames[0] = bu_vls_addr(&testresult);
94  bu_log("trimmed name:%s\n", cp->col_attrnames[0]);
95 
96  bu_vls_trunc(&workingstring2, 0);
97  bu_vls_strncpy(&workingstring2, bu_vls_addr(&workingstring1)+result_locations[2].rm_so, result_locations[2].rm_eo - result_locations[2].rm_so);
98 
99  while ((0 < bu_vls_strlen(&workingstring2)) && (ret != REG_NOMATCH)) {
100  bu_vls_sprintf(&workingstring1, "%s", bu_vls_addr(&workingstring2));
101  ret=regcomp(&compiled_regex, bu_vls_addr(&attrregex), REG_EXTENDED);
102  ret=regexec(&compiled_regex, bu_vls_addr(&workingstring1), components+1, result_locations, 0);
103  bu_vls_trunc(&testresult, 0);
104  bu_vls_strncpy(&testresult, bu_vls_addr(&workingstring1)+result_locations[1].rm_so, result_locations[1].rm_eo - result_locations[1].rm_so);
105  bu_log("\n%s\n", bu_vls_addr(&testresult));
106 
107  bu_vls_trunc(&workingstring2, 0);
108  bu_vls_strncpy(&workingstring2, bu_vls_addr(&workingstring1)+result_locations[2].rm_so, result_locations[2].rm_eo - result_locations[2].rm_so);
109 
110  cp->col_cnt = cp->col_cnt + 1;
111  cp->col_sizes[cp->col_cnt] = bu_vls_strlen(&testresult);
112  bu_vls_trimspace(&testresult);
113  cp->col_attrnames[cp->col_cnt] = bu_vls_addr(&testresult);
114  bu_log("stringlength:%d\n", cp->col_sizes[cp->col_cnt]);
115  bu_log("trimmed name:%s\n", cp->col_attrnames[cp->col_cnt]);
116  }
117 
118  bu_log("columns found: %d\n", cp->col_cnt);
119 
120  bu_log("\n");
121 
122  bu_free(result_locations, "free regex results");
123 }
124 
125 
126 int
128 {
129  FILE *fp;
130  struct col_properties cp;
131  struct bu_vls currentline = BU_VLS_INIT_ZERO;
132 
133 
134  cp.col_cnt = 0;
135  cp.col_sizes = (int *)bu_malloc(sizeof(int) * 10, "initial array of column sizes");
136  cp.col_attrnames = (char **)bu_malloc(sizeof(char *) * 11, "initial array of attribute names");
137 
138  fp = fopen("./test.txt", "r");
139  bu_vls_gets(&currentline, fp);
140  find_columns(bu_vls_addr(&currentline), &cp);
141 
142  /* header separator is a throwaway */
143  bu_vls_gets(&currentline, fp);
144  bu_vls_trunc(&currentline, 0);
145 
146  while (!(bu_vls_gets(&currentline, fp) < 0)) {
147  /*printf("line: %s\n\n", bu_vls_addr(&currentline));*/
148  parse_line(&currentline, &cp);
149  bu_vls_trunc(&currentline, 0);
150  }
151 
152  fclose(fp);
153  return 1;
154 }
155 
156 
157 /** @} */
158 /*
159  * Local Variables:
160  * tab-width: 8
161  * mode: C
162  * indent-tabs-mode: t
163  * c-file-style: "stroustrup"
164  * End:
165  * ex: shiftwidth=4 tabstop=8
166  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Definition: clone.c:90
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
void bu_vls_strncpy(struct bu_vls *vp, const char *s, size_t n)
Definition: vls.c:339
Header file for the BRL-CAD common definitions.
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
void bu_vls_sprintf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:707
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
char ** col_attrnames
Definition: columnparse.c:42
int main()
Definition: columnparse.c:127
int bu_vls_gets(struct bu_vls *vp, FILE *fp)
Definition: vls.c:621
void bu_vls_trimspace(struct bu_vls *vp)
Definition: vls.c:678
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56