PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/brlcad/branches/STABLE/src/librt/columnparse.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 166 lines | 102 code | 29 blank | 35 comment | 5 complexity | 0000c3c6e6dce2040ea30288f77b9411 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /* C O L U M N P A R S E . C
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 2008-2012 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. #include "common.h"
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <regex.h>
  30. #include "bio.h"
  31. #include "bn.h"
  32. #include "db.h"
  33. #include "raytrace.h"
  34. #include "bu.h"
  35. struct col_properties {
  36. int col_cnt;
  37. int *col_sizes;
  38. char **col_attrnames;
  39. };
  40. static void
  41. parse_line(struct bu_vls *line, struct col_properties *cp)
  42. {
  43. int currentposstart = 0;
  44. int currentposend = 0;
  45. int currentcol = -1;
  46. struct bu_vls workingstring = BU_VLS_INIT_ZERO;
  47. while (currentcol < cp->col_cnt) {
  48. currentcol++;
  49. currentposstart = currentposend;
  50. currentposend += cp->col_sizes[currentcol];
  51. bu_vls_trunc(&workingstring, 0);
  52. bu_vls_strncpy(&workingstring, bu_vls_addr(line)+currentposstart, currentposend - currentposstart);
  53. bu_vls_trimspace(&workingstring);
  54. bu_log("column %d contents: %s\n", currentcol, bu_vls_addr(&workingstring));
  55. }
  56. }
  57. static void
  58. find_columns(char *name, struct col_properties *cp)
  59. {
  60. regex_t compiled_regex;
  61. regmatch_t *result_locations;
  62. int ret, components;
  63. struct bu_vls modelregex = BU_VLS_INIT_ZERO;
  64. struct bu_vls attrregex = BU_VLS_INIT_ZERO;
  65. struct bu_vls workingstring1 = BU_VLS_INIT_ZERO;
  66. struct bu_vls workingstring2 = BU_VLS_INIT_ZERO;
  67. struct bu_vls testresult = BU_VLS_INIT_ZERO;
  68. bu_vls_sprintf(&modelregex, "([ ]*Model Name[ ]*)(.*)");
  69. bu_vls_sprintf(&attrregex, "([a-zA-Z0-9]*[ ]*)([a-zA-Z0-9].*$)?");
  70. bu_vls_sprintf(&workingstring1, "%s", name);
  71. ret=regcomp(&compiled_regex, bu_vls_addr(&modelregex), REG_EXTENDED);
  72. components = 2;
  73. result_locations = (regmatch_t *)bu_calloc(components + 1, sizeof(regmatch_t), "array to hold answers from regex");
  74. ret=regexec(&compiled_regex, bu_vls_addr(&workingstring1), components+1, result_locations, 0);
  75. bu_vls_trunc(&testresult, 0);
  76. bu_vls_strncpy(&testresult, bu_vls_addr(&workingstring1)+result_locations[1].rm_so, result_locations[1].rm_eo - result_locations[1].rm_so);
  77. cp->col_sizes[0] = bu_vls_strlen(&testresult);
  78. bu_log("stringlength:%d\n", cp->col_sizes[0]);
  79. bu_vls_trimspace(&testresult);
  80. cp->col_attrnames[0] = bu_vls_addr(&testresult);
  81. bu_log("trimmed name:%s\n", cp->col_attrnames[0]);
  82. bu_vls_trunc(&workingstring2, 0);
  83. bu_vls_strncpy(&workingstring2, bu_vls_addr(&workingstring1)+result_locations[2].rm_so, result_locations[2].rm_eo - result_locations[2].rm_so);
  84. while ((0 < bu_vls_strlen(&workingstring2)) && (ret != REG_NOMATCH)) {
  85. bu_vls_sprintf(&workingstring1, "%s", bu_vls_addr(&workingstring2));
  86. ret=regcomp(&compiled_regex, bu_vls_addr(&attrregex), REG_EXTENDED);
  87. ret=regexec(&compiled_regex, bu_vls_addr(&workingstring1), components+1, result_locations, 0);
  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. bu_log("\n%s\n", bu_vls_addr(&testresult));
  91. bu_vls_trunc(&workingstring2, 0);
  92. bu_vls_strncpy(&workingstring2, bu_vls_addr(&workingstring1)+result_locations[2].rm_so, result_locations[2].rm_eo - result_locations[2].rm_so);
  93. cp->col_cnt = cp->col_cnt + 1;
  94. cp->col_sizes[cp->col_cnt] = bu_vls_strlen(&testresult);
  95. bu_vls_trimspace(&testresult);
  96. cp->col_attrnames[cp->col_cnt] = bu_vls_addr(&testresult);
  97. bu_log("stringlength:%d\n", cp->col_sizes[cp->col_cnt]);
  98. bu_log("trimmed name:%s\n", cp->col_attrnames[cp->col_cnt]);
  99. }
  100. bu_log("columns found: %d\n", cp->col_cnt);
  101. bu_log("\n");
  102. bu_free(result_locations, "free regex results");
  103. }
  104. int
  105. main()
  106. {
  107. FILE *fp;
  108. struct col_properties *cp;
  109. struct bu_vls currentline = BU_VLS_INIT_ZERO;
  110. BU_GET(cp, struct col_properties);
  111. cp->col_sizes = (int *)bu_malloc(sizeof(int) * 10, "initial array of column sizes");
  112. cp->col_attrnames = (char **)bu_malloc(sizeof(char *) * 11, "initial array of attribute names");
  113. cp->col_cnt = 0;
  114. fp = fopen("./test.txt", "r");
  115. bu_vls_gets(&currentline, fp);
  116. find_columns(bu_vls_addr(&currentline), cp);
  117. /* header separator is a throwaway */
  118. bu_vls_gets(&currentline, fp);
  119. bu_vls_trunc(&currentline, 0);
  120. while (!(bu_vls_gets(&currentline, fp) < 0)) {
  121. /*printf("line: %s\n\n", bu_vls_addr(&currentline));*/
  122. parse_line(&currentline, cp);
  123. bu_vls_trunc(&currentline, 0);
  124. }
  125. fclose(fp);
  126. return 1;
  127. }
  128. /** @} */
  129. /*
  130. * Local Variables:
  131. * tab-width: 8
  132. * mode: C
  133. * indent-tabs-mode: t
  134. * c-file-style: "stroustrup"
  135. * End:
  136. * ex: shiftwidth=4 tabstop=8
  137. */