Revision: 75402
http://sourceforge.net/p/brlcad/code/75402
Author: starseeker
Date: 2020-04-14 19:05:21 +0000 (Tue, 14 Apr 2020)
Log Message:
-----------
Break regex using part of vls out into a C++ file (uuid.h isn't happy in C++,
so can't just shift vls.c to C++)
Modified Paths:
--------------
brlcad/trunk/src/libbu/CMakeLists.txt
brlcad/trunk/src/libbu/vls.c
Added Paths:
-----------
brlcad/trunk/src/libbu/vls_incr.cpp
Modified: brlcad/trunk/src/libbu/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbu/CMakeLists.txt 2020-04-14 18:20:42 UTC (rev
75401)
+++ brlcad/trunk/src/libbu/CMakeLists.txt 2020-04-14 19:05:21 UTC (rev
75402)
@@ -128,6 +128,7 @@
vfont.c
vlb.c
vls.c
+ vls_incr.cpp
vls_vprintf.c
whereami.c
whereis.c
Modified: brlcad/trunk/src/libbu/vls.c
===================================================================
--- brlcad/trunk/src/libbu/vls.c 2020-04-14 18:20:42 UTC (rev 75401)
+++ brlcad/trunk/src/libbu/vls.c 2020-04-14 19:05:21 UTC (rev 75402)
@@ -24,7 +24,6 @@
#include <ctype.h>
#include <errno.h> /* for errno */
#include <limits.h> /* for LONG_MAX */
-#include <regex.h>
#include <stdarg.h>
#include <stdlib.h> /* for strtol */
#include <string.h>
@@ -907,208 +906,6 @@
return ret;
}
-
-HIDDEN int
-vls_incr_next(struct bu_vls *next_incr, const char *incr_state, const char
*inc_specifier)
-{
- int i = 0;
- long ret = 0;
- /*char bsep = '\0';
- char esep = '\0';*/
- long vals[4] = {0}; /* 0 = width, 1 = min/init, 2 = max, 3 = increment */
- long state_val = 0;
- const char *wstr = inc_specifier;
- char *endptr;
- int spacer_cnt = 1;
- long spacer_val;
-
- if (!inc_specifier || !next_incr || !incr_state) return 0;
-
- errno = 0;
- state_val = strtol(incr_state, &endptr, 10);
- if (errno == ERANGE) {
- bu_log("ERANGE error reading current value\n");
- return -1;
- }
-
- for(i = 0; i < 4; i++) {
- errno = 0;
- if (!wstr) {
- bu_log("Invalid incrementation specifier: %s\n", inc_specifier);
- return -1;
- }
- vals[i] = strtol(wstr, &endptr, 10);
- if (errno == ERANGE) {
- bu_log("ERANGE error reading name generation specifier\n");
- return -1;
- }
- wstr = (strchr(wstr, ':')) ? strchr(wstr, ':') + 1 : NULL;
- }
-
- /* increment */
- state_val = (vals[3]) ? state_val + vals[3] : state_val + 1;
-
- /* if we're below the minimum specified range, clamp to minimum */
- if (vals[1] && state_val < vals[1]) state_val = vals[1];
-
- /* if we're above the maximum specified range, roll over */
- if (vals[2] && state_val > vals[2]) {
- state_val = vals[1];
- ret = -2;
- }
-
- /* find out if we need padding zeros */
- if (vals[0]) {
- spacer_val = state_val;
- while ((spacer_val = spacer_val / 10)) spacer_cnt++;
- }
-
- if (wstr) {
- bu_vls_printf(next_incr, "%c", wstr[0]);
- if (vals[0]) {
- for (i = 0; i < vals[0]-spacer_cnt; i++) {
- bu_vls_printf(next_incr, "%d", 0);
- }
- bu_vls_printf(next_incr, "%ld", state_val);
- } else {
- bu_vls_printf(next_incr, "%ld", state_val);
- }
- wstr = (strchr(wstr, ':')) ? strchr(wstr, ':') + 1 : NULL;
- if (wstr) bu_vls_printf(next_incr, "%c", wstr[0]);
- } else {
- if (vals[0]) {
- for (i = 0; i < vals[0]-spacer_cnt; i++) {
- bu_vls_printf(next_incr, "%d", 0);
- }
- bu_vls_printf(next_incr, "%ld", state_val);
- } else {
- bu_vls_printf(next_incr, "%ld", state_val);
- }
- }
- return ret;
-}
-
-int
-bu_vls_incr(struct bu_vls *name, const char *regex_str, const char *incr_spec,
bu_vls_uniq_t ut, void *data)
-{
- int ret = 0;
- size_t i = 0;
- size_t j = 0;
- int offset = 0;
- regmatch_t *incr_substrs;
- regmatch_t *num_substrs;
- struct bu_vls new_name = BU_VLS_INIT_ZERO;
- struct bu_vls curr_incr = BU_VLS_INIT_ZERO;
- struct bu_vls ispec = BU_VLS_INIT_ZERO;
- const char *num_regex = "([0-9]+)";
- const char *last_regex = "([0-9]+)[^0-9]*$";
- const char *rs = NULL;
- struct bu_vls num_str = BU_VLS_INIT_ZERO;
- int success = 0;
-
- if (!name) return -1;
-
- rs = (regex_str) ? regex_str : last_regex;
-
- while (!success) {
- /* Find incrementer. */
- {
- regex_t compiled_regex;
- ret = regcomp(&compiled_regex, rs, REG_EXTENDED);
- if (ret != 0) {
- regfree(&compiled_regex);
- return -1;
- }
- incr_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(name) + 1,
sizeof(regmatch_t), "regex results");
- ret = regexec(&compiled_regex, bu_vls_addr(name),
bu_vls_strlen(name) + 1, incr_substrs, 0);
- if (ret == REG_NOMATCH) {
- bu_vls_printf(name, "0");
- bu_free(incr_substrs, "free regex results");
- incr_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(name) + 1,
sizeof(regmatch_t), "regex results");
- ret = regexec(&compiled_regex, bu_vls_addr(name),
bu_vls_strlen(name) + 1, incr_substrs, 0);
- if (ret == REG_NOMATCH) {
- bu_free(incr_substrs, "free regex results");
- regfree(&compiled_regex);
- return -1;
- }
- }
- regfree(&compiled_regex);
- }
- i = bu_vls_strlen(name);
- while(incr_substrs[i].rm_so == -1 || incr_substrs[i].rm_eo == -1)
- i--;
-
- if (i != 1)
- return -1;
-
- /* Now we know where the incrementer is - process, find the number, and
assemble the new string */
- bu_vls_trunc(&new_name, 0);
- bu_vls_substr(&curr_incr, name, incr_substrs[1].rm_so,
incr_substrs[1].rm_eo - incr_substrs[1].rm_so);
- bu_vls_strncpy(&new_name, bu_vls_addr(name)+offset,
incr_substrs[j].rm_so - offset);
-
- /* Find number. */
- {
- regex_t compiled_regex;
- ret = regcomp(&compiled_regex, num_regex, REG_EXTENDED);
- if (ret != 0) {
- regfree(&compiled_regex);
- return -1;
- }
- num_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(&curr_incr) +
1, sizeof(regmatch_t), "regex results");
- ret = regexec(&compiled_regex, bu_vls_addr(&curr_incr),
bu_vls_strlen(&curr_incr) + 1, num_substrs, 0);
- if (ret == REG_NOMATCH) {
- bu_vls_free(&new_name);
- bu_vls_free(&ispec);
- bu_vls_free(&curr_incr);
- bu_free(num_substrs, "free regex results");
- regfree(&compiled_regex);
- return -1;
- }
- bu_vls_substr(&num_str, &curr_incr, num_substrs[1].rm_so,
num_substrs[1].rm_eo - num_substrs[1].rm_so);
- regfree(&compiled_regex);
- }
-
- /* Either used the supplied incrementing specification or initialize
with the default */
- if (!incr_spec) {
- bu_vls_sprintf(&ispec, "%lu:%d:%d:%d",
strlen(bu_vls_addr(&num_str)), 0, 0, 1);
- } else {
- bu_vls_sprintf(&ispec, "%s", incr_spec);
- }
-
- /* Do incrementation */
- ret = vls_incr_next(&new_name, bu_vls_addr(&num_str),
bu_vls_addr(&ispec));
- bu_vls_printf(&new_name, "%s", bu_vls_addr(name)+incr_substrs[1].rm_eo);
- bu_vls_sprintf(name, "%s", bu_vls_addr(&new_name));
-
- if (ret < 0) {
- bu_vls_free(&new_name);
- bu_vls_free(&ispec);
- bu_vls_free(&curr_incr);
- bu_free(num_substrs, "free regex results");
- return ret;
- }
-
- /* If we need to, test for uniqueness */
- if (ut) {
- success = (*ut)(name,data);
- } else {
- success = 1;
- }
-
- bu_free(num_substrs, "free regex results");
- }
-
- bu_vls_free(&num_str);
- bu_vls_free(&new_name);
- bu_vls_free(&ispec);
- bu_vls_free(&curr_incr);
- bu_free(incr_substrs, "free regex results");
-
- return ret;
-}
-
-
-
/*
* Local Variables:
* mode: C
Added: brlcad/trunk/src/libbu/vls_incr.cpp
===================================================================
--- brlcad/trunk/src/libbu/vls_incr.cpp (rev 0)
+++ brlcad/trunk/src/libbu/vls_incr.cpp 2020-04-14 19:05:21 UTC (rev 75402)
@@ -0,0 +1,241 @@
+/* V L S _ I N C R . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2004-2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+
+#include "common.h"
+
+#include <stdlib.h> /* for strtol */
+#include <string.h>
+
+#include <cerrno>
+
+#include "regex.h"
+
+#include "bu/log.h"
+#include "bu/malloc.h"
+#include "bu/vls.h"
+
+static int
+vls_incr_next(struct bu_vls *next_incr, const char *incr_state, const char
*inc_specifier)
+{
+ int i = 0;
+ long ret = 0;
+ /*char bsep = '\0';
+ char esep = '\0';*/
+ long vals[4] = {0}; /* 0 = width, 1 = min/init, 2 = max, 3 = increment */
+ long state_val = 0;
+ const char *wstr = inc_specifier;
+ char *endptr;
+ int spacer_cnt = 1;
+ long spacer_val;
+
+ if (!inc_specifier || !next_incr || !incr_state) return 0;
+
+ errno = 0;
+ state_val = strtol(incr_state, &endptr, 10);
+ if (errno == ERANGE) {
+ bu_log("ERANGE error reading current value\n");
+ return -1;
+ }
+
+ for(i = 0; i < 4; i++) {
+ errno = 0;
+ if (!wstr) {
+ bu_log("Invalid incrementation specifier: %s\n", inc_specifier);
+ return -1;
+ }
+ vals[i] = strtol(wstr, &endptr, 10);
+ if (errno == ERANGE) {
+ bu_log("ERANGE error reading name generation specifier\n");
+ return -1;
+ }
+ wstr = (strchr(wstr, ':')) ? strchr(wstr, ':') + 1 : NULL;
+ }
+
+ /* increment */
+ state_val = (vals[3]) ? state_val + vals[3] : state_val + 1;
+
+ /* if we're below the minimum specified range, clamp to minimum */
+ if (vals[1] && state_val < vals[1]) state_val = vals[1];
+
+ /* if we're above the maximum specified range, roll over */
+ if (vals[2] && state_val > vals[2]) {
+ state_val = vals[1];
+ ret = -2;
+ }
+
+ /* find out if we need padding zeros */
+ if (vals[0]) {
+ spacer_val = state_val;
+ while ((spacer_val = spacer_val / 10)) spacer_cnt++;
+ }
+
+ if (wstr) {
+ bu_vls_printf(next_incr, "%c", wstr[0]);
+ if (vals[0]) {
+ for (i = 0; i < vals[0]-spacer_cnt; i++) {
+ bu_vls_printf(next_incr, "%d", 0);
+ }
+ bu_vls_printf(next_incr, "%ld", state_val);
+ } else {
+ bu_vls_printf(next_incr, "%ld", state_val);
+ }
+ wstr = (strchr(wstr, ':')) ? strchr(wstr, ':') + 1 : NULL;
+ if (wstr) bu_vls_printf(next_incr, "%c", wstr[0]);
+ } else {
+ if (vals[0]) {
+ for (i = 0; i < vals[0]-spacer_cnt; i++) {
+ bu_vls_printf(next_incr, "%d", 0);
+ }
+ bu_vls_printf(next_incr, "%ld", state_val);
+ } else {
+ bu_vls_printf(next_incr, "%ld", state_val);
+ }
+ }
+ return ret;
+}
+
+ int
+bu_vls_incr(struct bu_vls *name, const char *regex_str, const char *incr_spec,
bu_vls_uniq_t ut, void *data)
+{
+ int ret = 0;
+ size_t i = 0;
+ size_t j = 0;
+ int offset = 0;
+ regmatch_t *incr_substrs;
+ regmatch_t *num_substrs;
+ struct bu_vls new_name = BU_VLS_INIT_ZERO;
+ struct bu_vls curr_incr = BU_VLS_INIT_ZERO;
+ struct bu_vls ispec = BU_VLS_INIT_ZERO;
+ const char *num_regex = "([0-9]+)";
+ const char *last_regex = "([0-9]+)[^0-9]*$";
+ const char *rs = NULL;
+ struct bu_vls num_str = BU_VLS_INIT_ZERO;
+ int success = 0;
+
+ if (!name) return -1;
+
+ rs = (regex_str) ? regex_str : last_regex;
+
+ while (!success) {
+ /* Find incrementer. */
+ {
+ regex_t compiled_regex;
+ ret = regcomp(&compiled_regex, rs, REG_EXTENDED);
+ if (ret != 0) {
+ regfree(&compiled_regex);
+ return -1;
+ }
+ incr_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(name) + 1,
sizeof(regmatch_t), "regex results");
+ ret = regexec(&compiled_regex, bu_vls_addr(name),
bu_vls_strlen(name) + 1, incr_substrs, 0);
+ if (ret == REG_NOMATCH) {
+ bu_vls_printf(name, "0");
+ bu_free(incr_substrs, "free regex results");
+ incr_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(name) + 1,
sizeof(regmatch_t), "regex results");
+ ret = regexec(&compiled_regex, bu_vls_addr(name),
bu_vls_strlen(name) + 1, incr_substrs, 0);
+ if (ret == REG_NOMATCH) {
+ bu_free(incr_substrs, "free regex results");
+ regfree(&compiled_regex);
+ return -1;
+ }
+ }
+ regfree(&compiled_regex);
+ }
+ i = bu_vls_strlen(name);
+ while(incr_substrs[i].rm_so == -1 || incr_substrs[i].rm_eo == -1)
+ i--;
+
+ if (i != 1)
+ return -1;
+
+ /* Now we know where the incrementer is - process, find the number, and
assemble the new string */
+ bu_vls_trunc(&new_name, 0);
+ bu_vls_substr(&curr_incr, name, incr_substrs[1].rm_so,
incr_substrs[1].rm_eo - incr_substrs[1].rm_so);
+ bu_vls_strncpy(&new_name, bu_vls_addr(name)+offset,
incr_substrs[j].rm_so - offset);
+
+ /* Find number. */
+ {
+ regex_t compiled_regex;
+ ret = regcomp(&compiled_regex, num_regex, REG_EXTENDED);
+ if (ret != 0) {
+ regfree(&compiled_regex);
+ return -1;
+ }
+ num_substrs = (regmatch_t *)bu_calloc(bu_vls_strlen(&curr_incr) +
1, sizeof(regmatch_t), "regex results");
+ ret = regexec(&compiled_regex, bu_vls_addr(&curr_incr),
bu_vls_strlen(&curr_incr) + 1, num_substrs, 0);
+ if (ret == REG_NOMATCH) {
+ bu_vls_free(&new_name);
+ bu_vls_free(&ispec);
+ bu_vls_free(&curr_incr);
+ bu_free(num_substrs, "free regex results");
+ regfree(&compiled_regex);
+ return -1;
+ }
+ bu_vls_substr(&num_str, &curr_incr, num_substrs[1].rm_so,
num_substrs[1].rm_eo - num_substrs[1].rm_so);
+ regfree(&compiled_regex);
+ }
+
+ /* Either used the supplied incrementing specification or initialize
with the default */
+ if (!incr_spec) {
+ bu_vls_sprintf(&ispec, "%lu:%d:%d:%d",
strlen(bu_vls_addr(&num_str)), 0, 0, 1);
+ } else {
+ bu_vls_sprintf(&ispec, "%s", incr_spec);
+ }
+
+ /* Do incrementation */
+ ret = vls_incr_next(&new_name, bu_vls_addr(&num_str),
bu_vls_addr(&ispec));
+ bu_vls_printf(&new_name, "%s", bu_vls_addr(name)+incr_substrs[1].rm_eo);
+ bu_vls_sprintf(name, "%s", bu_vls_addr(&new_name));
+
+ if (ret < 0) {
+ bu_vls_free(&new_name);
+ bu_vls_free(&ispec);
+ bu_vls_free(&curr_incr);
+ bu_free(num_substrs, "free regex results");
+ return ret;
+ }
+
+ /* If we need to, test for uniqueness */
+ if (ut) {
+ success = (*ut)(name,data);
+ } else {
+ success = 1;
+ }
+
+ bu_free(num_substrs, "free regex results");
+ }
+
+ bu_vls_free(&num_str);
+ bu_vls_free(&new_name);
+ bu_vls_free(&ispec);
+ bu_vls_free(&curr_incr);
+ bu_free(incr_substrs, "free regex results");
+
+ return ret;
+}
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: brlcad/trunk/src/libbu/vls_incr.cpp
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits