|
From: <kin...@us...> - 2025-09-23 14:21:28
|
Revision: 7477
http://sourceforge.net/p/teem/code/7477
Author: kindlmann
Date: 2025-09-23 14:21:24 +0000 (Tue, 23 Sep 2025)
Log Message:
-----------
changing hestArgVec from array of hestArg structs to array of pointers to hestArg structs
Modified Paths:
--------------
teem/trunk/src/hest/argvHest.c
teem/trunk/src/hest/hest.h
teem/trunk/src/hest/parsest.c
Modified: teem/trunk/src/hest/argvHest.c
===================================================================
--- teem/trunk/src/hest/argvHest.c 2025-09-23 11:04:43 UTC (rev 7476)
+++ teem/trunk/src/hest/argvHest.c 2025-09-23 14:21:24 UTC (rev 7477)
@@ -22,14 +22,6 @@
#define INCR 32
-/* to avoid strict aliasing warnings */
-typedef union {
- hestArg **harg;
- hestArgVec **havec;
- hestInput **hin;
- void **v;
-} hestPtrPtrUnion;
-
/* -------------------------- hestArg = harg = hestArg = harg ---------------------- */
/* dereferences as char *, sets to '\0' */
@@ -40,46 +32,45 @@
return;
}
-static void
-hargInit(void *_harg) {
- hestArg *harg = (hestArg *)_harg;
+hestArg *
+hestArgNew(void) {
+ hestArg *harg = AIR_CALLOC(1, hestArg);
+ assert(harg);
harg->str = NULL;
harg->len = 0;
airPtrPtrUnion appu;
appu.c = &(harg->str);
harg->strArr = airArrayNew(appu.v, &(harg->len), sizeof(char), INCR);
+ // underlying array harg->str will not be reallocated if shrunk
+ harg->strArr->noReallocWhenSmaller = AIR_TRUE;
airArrayStructCB(harg->strArr, setNul, NULL);
harg->source = hestSourceUnknown;
/* initialize with \0 so that harg->str is "" */
airArrayLenIncr(harg->strArr, 1);
/* now harg->str = {0:'\0'} and harg->len = 1; */
- return;
-}
-
-hestArg *
-hestArgNew(void) {
- hestArg *harg = AIR_CALLOC(1, hestArg);
- assert(harg);
- hargInit(harg);
return harg;
}
-static void
-hargDone(void *_harg) {
- hestArg *harg = (hestArg *)_harg;
- airArrayNuke(harg->strArr);
- return;
+static void *
+_hestArgNew_vp(void) {
+ return AIR_VOIDP(hestArgNew());
}
hestArg *
hestArgNix(hestArg *harg) {
if (harg) {
- hargDone(harg);
+ airArrayNuke(harg->strArr);
free(harg);
}
return NULL;
}
+static void *
+_hestArgNix_vp(void *_harg) {
+ hestArg *harg = (hestArg *)_harg;
+ return AIR_VOIDP(hestArgNix(harg));
+}
+
void
hestArgReset(hestArg *harg) {
assert(harg);
@@ -112,6 +103,15 @@
/* ---------------------- hestArgVec = havec = hestArgVec = havec ------------------ */
+/* to avoid strict aliasing warnings */
+typedef union {
+ hestArg **harg;
+ hestArg ***hargP;
+ hestArgVec **havec;
+ hestInput **hin;
+ void **v;
+} hestPtrPtrUnion;
+
hestArgVec *
hestArgVecNew() {
hestArgVec *havec = AIR_CALLOC(1, hestArgVec);
@@ -119,11 +119,12 @@
havec->harg = NULL;
havec->len = 0;
hestPtrPtrUnion hppu;
- hppu.harg = &(havec->harg);
- havec->hargArr = airArrayNew(hppu.v, &(havec->len), sizeof(hestArg), INCR);
+ hppu.hargP = &(havec->harg);
+ havec->hargArr = airArrayNew(hppu.v, &(havec->len), sizeof(hestArg *), INCR);
// underlying array havec->harg will not be reallocated if shrunk
havec->hargArr->noReallocWhenSmaller = AIR_TRUE;
- airArrayStructCB(havec->hargArr, hargInit, hargDone);
+ // airArrayStructCB(havec->hargArr, hargInit, hargDone);
+ airArrayPointerCB(havec->hargArr, _hestArgNew_vp, _hestArgNix_vp);
return havec;
}
@@ -144,17 +145,17 @@
return NULL;
}
-void
+hestArg *
hestArgVecRemove(hestArgVec *havec, uint popIdx) {
- // (experimented with allocating something to hold what was lost)
- // hestArg *ret = NULL;
+ hestArg *ret = NULL;
if (havec && popIdx < havec->len) { // note: this implies that havec->len >= 1
- // ret = AIR_CALLOC(1, hestArg); // (we don't have a constructor?)
- // memcpy(ret, havec->harg + popIdx);
- for (uint ai = popIdx; ai < havec->len - 1; ai++) {
+ ret = havec->harg[popIdx];
+ uint ai;
+ for (ai = popIdx; ai < havec->len - 1; ai++) {
// shuffle down info inside the hestArg elements of havec->harg
- hestArgSetString(havec->harg + ai, (havec->harg + ai + 1)->str);
- (havec->harg + ai)->source = (havec->harg + ai + 1)->source;
+ havec->harg[ai] = havec->harg[ai + 1];
+ // hestArgSetString(havec->harg + ai, (havec->harg + ai + 1)->str);
+ // (havec->harg + ai)->source = (havec->harg + ai + 1)->source;
/* why cannot just memcpy:
because then the last hestArg element of havec->harg
(the one that is being forgotten)
@@ -164,29 +165,46 @@
as the callack from airArrayLenIncr(), then it will also
free the str inside the second-to-last element; oops */
}
+ // NULL out final out, using final value of ai
+ havec->harg[ai] = NULL;
// decrement the nominal length of havec->harg
airArrayLenIncr(havec->hargArr, -1);
}
- return;
+ return ret;
}
void
hestArgVecAppendString(hestArgVec *havec, const char *str) {
uint idx = airArrayLenIncr(havec->hargArr, 1);
- hestArgSetString(havec->harg + idx, str);
+ hestArgSetString(havec->harg[idx], str);
}
void
+hestArgVecAppendArg(hestArgVec *havec, hestArg *harg) {
+ uint idx = airArrayLenIncr(havec->hargArr, 1);
+ // oops, delete the hestArg just created via callback
+ hestArgNix(havec->harg[idx]);
+ havec->harg[idx] = harg;
+}
+
+void
hestArgVecPrint(const char *caller, const char *info, const hestArgVec *havec) {
// fprintf(stderr, "!%s: %s hestArgVec %p has %u args:\n", caller, info, havec,
// havec->len);
+ char srcch[] = {
+ // quick way of identifying source
+ '?', // 0: hestSourceUnknown
+ 'c', // 1: hestSourceCommandLine
+ 'r', // 2: hestSourceResponseFile
+ 'd', // 3: hestSourceDefault
+ };
printf("%s: %s hestArgVec %p has %u args:\n", caller, info, havec, havec->len);
for (uint idx = 0; idx < havec->hargArr->len; idx++) {
const hestArg *harg;
- harg = havec->harg + idx;
+ harg = havec->harg[idx];
// fprintf(stderr, "!%s harg@%p=%u:<%s>\n", "", AIR_VOIDP(harg), idx,
// harg->str ? harg->str : "NULL");
- printf(" %u:<%s>", idx, harg->str ? harg->str : "NULL");
+ printf(" %u%c:<%s>", idx, srcch[harg->source], harg->str ? harg->str : "NULL");
}
printf("\n");
}
Modified: teem/trunk/src/hest/hest.h
===================================================================
--- teem/trunk/src/hest/hest.h 2025-09-23 11:04:43 UTC (rev 7476)
+++ teem/trunk/src/hest/hest.h 2025-09-23 14:21:24 UTC (rev 7477)
@@ -121,7 +121,7 @@
// hestArgVec: for building up a "vector" of arguments
typedef struct {
- hestArg *harg; // array of hestArg structs (not pointers to them)
+ hestArg **harg; // array of pointers to hestArg structs
unsigned int len; // number of arguments in this vector
airArray *hargArr; // (manages harg and len)
} hestArgVec;
@@ -231,7 +231,7 @@
array of strings
3: free((*valueP)[i]) and free(*valueP), because it is a dynamically
allocated array of strings */
- hestArgVec *havec; // the args attributed to this option
+ hestArgVec *havec; // the (non-flag) parm args attributed to this option
/* Since hest's beginning in 2002, the basic container for a set of options was an
array of hestOpt structs (not pointers to them, which rules out argv-style
NULL-termination of the array), also unfortunately with no other top-level container
@@ -343,8 +343,9 @@
HEST_EXPORT hestArgVec *hestArgVecNew(void);
HEST_EXPORT void hestArgVecReset(hestArgVec *havec);
HEST_EXPORT hestArgVec *hestArgVecNix(hestArgVec *havec);
-HEST_EXPORT void hestArgVecRemove(hestArgVec *havec, unsigned int popIdx);
+HEST_EXPORT hestArg *hestArgVecRemove(hestArgVec *havec, unsigned int popIdx);
HEST_EXPORT void hestArgVecAppendString(hestArgVec *havec, const char *str);
+HEST_EXPORT void hestArgVecAppendArg(hestArgVec *havec, hestArg *harg);
HEST_EXPORT void hestArgVecPrint(const char *caller, const char *info,
const hestArgVec *havec);
HEST_EXPORT hestInput *hestInputNew(void);
Modified: teem/trunk/src/hest/parsest.c
===================================================================
--- teem/trunk/src/hest/parsest.c 2025-09-23 11:04:43 UTC (rev 7476)
+++ teem/trunk/src/hest/parsest.c 2025-09-23 14:21:24 UTC (rev 7477)
@@ -607,7 +607,7 @@
srcstr, tharg->str, havec->len);
}
// set source in the hestArg we just appended
- (havec->harg + havec->len - 1)->source = topHin->source;
+ havec->harg[havec->len - 1]->source = topHin->source;
}
if (hist->len && nast == nastEmpty) {
biffAddf(HEST, "%s: non-empty stack (depth %u) can't generate args???", __func__,
@@ -713,44 +713,46 @@
uint argIdx = 0;
hestOpt *theOpt = NULL;
while (argIdx < havec->len) { // NOTE: havec->len may decrease within an interation!
- hestArg *theArg = havec->harg + argIdx;
if (hparm->verbosity) {
printf("%s: ------------- argIdx = %u (of %u) -> argv[argIdx] = |%s|\n", __func__,
- argIdx, havec->len, theArg->str);
+ argIdx, havec->len, havec->harg[argIdx]->str);
}
- uint optIdx = whichOptFlag(opt, theArg->str, hparm);
+ uint optIdx = whichOptFlag(opt, havec->harg[argIdx]->str, hparm);
if (UINT_MAX == optIdx) {
- // theArg->str is not a flag for any option, move on to next arg
+ // havec->harg[argIdx]->str is not a flag for any option, move on to next arg
argIdx++;
if (hparm->verbosity)
- printf("%s: |%s| not a flag arg, continue\n", __func__, theArg->str);
+ printf("%s: |%s| not a flag arg, continue\n", __func__,
+ havec->harg[argIdx]->str);
continue;
}
- // else theArg->str is a flag for option with index optIdx aka theOpt
+ // else havec->harg[argIdx]->str is a flag for option with index optIdx aka theOpt
theOpt = opt + optIdx;
if (hparm->verbosity)
printf("%s: argv[%u]=|%s| is flag of opt %u \"%s\"\n", __func__, argIdx,
- theArg->str, optIdx, theOpt->flag);
+ havec->harg[argIdx]->str, optIdx, theOpt->flag);
/* see if we can associate some parameters with the flag */
if (hparm->verbosity) printf("%s: any associated parms?\n", __func__);
- uint parmNum = 0;
int hitEnd = AIR_FALSE;
int varParm = (5 == opt[optIdx].kind);
- char VPS[3] = {'-', VAR_PARM_STOP_FLAG, '\0'};
+ const char *pas, VPS[3] = {'-', VAR_PARM_STOP_FLAG, '\0'};
int hitVPS = AIR_FALSE;
- uint nextOptIdx = 0; // what is index of option who's flag we see next
- while (AIR_INT(parmNum) < _hestMax(theOpt->max) // parmNum is plausible # parms
- && !(hitEnd = !(argIdx + 1 + parmNum < havec->len)) // and have valid index
- && (!varParm // and either this isn't a variable parm opt
- || // or, it is a varparm opt and we aren't looking at "--"
- !(hitVPS = !strcmp(VPS, (theArg + 1 + parmNum)->str)))
- && UINT_MAX // and we aren't looking at start of another flagged option
- == (nextOptIdx = whichOptFlag(opt, (theArg + 1 + parmNum)->str,
- hparm))) {
+ uint nextOptIdx = 0, // what is index of option who's flag we hit next
+ parmNum = 0, // how many parm args have we counted up
+ pai; // tmp parm arg index
+ while ( // parmNum is plausible # parms
+ AIR_INT(parmNum) < _hestMax(theOpt->max)
+ // and looking ahead by parmNum still gives us a valid index pai
+ && !(hitEnd = !((pai = argIdx + 1 + parmNum) < havec->len))
+ // and either this isn't a variable parm opt
+ && (!varParm || // or, it is a varparm opt, and we aren't looking at "--"
+ !(hitVPS = !strcmp(VPS, (pas = havec->harg[pai]->str))))
+ && UINT_MAX // and we aren't looking at start of another flagged option
+ == (nextOptIdx = whichOptFlag(opt, pas, hparm))) {
+ if (hparm->verbosity)
+ printf("%s: optIdx %d |%s|; argIdx %u < %u |%s| --> parmNum --> %d\n", __func__,
+ optIdx, theOpt->flag, argIdx, pai, pas, parmNum + 1);
parmNum++;
- if (hparm->verbosity)
- printf("%s: optIdx %d |%s|: |%s| --> parmNum --> %d\n", __func__, optIdx,
- theOpt->flag, (theArg + 1 + parmNum - 1)->str, parmNum);
}
/* we stopped because we got the max number of parameters, or
we hitEnd, or
@@ -788,15 +790,15 @@
}
if (hparm->verbosity) {
printf("%s: ________ argv[%d]=|%s|: optIdx %u |%s| followed by %u parms\n",
- __func__, argIdx, theArg->str, optIdx, theOpt->flag, parmNum);
+ __func__, argIdx, havec->harg[argIdx]->str, optIdx, theOpt->flag, parmNum);
}
// remember from whence this option came
- theOpt->source = theArg->source;
+ theOpt->source = havec->harg[argIdx]->source;
// lose the flag argument
if (hparm->verbosity > 1) {
hestArgVecPrint(__func__, "main havec as it came", havec);
}
- hestArgVecRemove(havec, argIdx);
+ hestArgNix(hestArgVecRemove(havec, argIdx));
if (hparm->verbosity > 1) {
char info[AIR_STRLEN_HUGE + 1];
sprintf(info, "main havec after losing argIdx %u", argIdx);
@@ -805,16 +807,14 @@
// empty any prior parm args learned for this option
hestArgVecReset(theOpt->havec);
for (uint pidx = 0; pidx < parmNum; pidx++) {
- // theArg still points to the next arg (at index argIdx) for this option
if (hparm->verbosity) {
- printf("%s: moving |%s| to theOpt->havec\n", __func__, theArg->str);
+ printf("%s: moving |%s| to theOpt->havec\n", __func__, havec->harg[argIdx]->str);
}
- hestArgVecAppendString(theOpt->havec, theArg->str);
- hestArgVecRemove(havec, argIdx);
+ hestArgVecAppendArg(theOpt->havec, hestArgVecRemove(havec, argIdx));
}
if (hitVPS) {
// drop the variable-parameter-stop flag
- hestArgVecRemove(havec, argIdx);
+ hestArgNix(hestArgVecRemove(havec, argIdx));
}
if (hparm->verbosity) {
char info[AIR_STRLEN_HUGE + 1];
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|