From: Fredrik Karlsson on
Hi,

I am trying to get a unique list of paths for later glob:ing, but I
cannot get stuff to be removed from the list I am using. What am I
doing wrong?

directories = Tcl_NewObj();
Tcl_IncrRefCount(directories);

// Get some sample dirs in
Tcl_ListObjAppendElement(interp, directories, Tcl_NewStringObj("/
Users/zak/Desktop/emutestdb/aetobi",-1));
Tcl_ListObjAppendElement(interp, directories, Tcl_NewStringObj("/
Users/zak/Desktop/emutestdb/epgdorsal",-1));
Tcl_ListObjAppendElement(interp, directories, Tcl_NewStringObj("/
Users/zak/Desktop/emutestdb/epgdorsal",-1));
Tcl_ListObjAppendElement(interp, directories, Tcl_NewStringObj("/
Users/zak/Desktop/emutestdb/epgdorsal",-1));

for(i=0; Tcl_ListObjLength(interp, directories, &imax) && i < imax ;
i++ ){
Tcl_ListObjIndex(interp, directories, i, &currDirectory);
Tcl_IncrRefCount(currDirectory);

/*Now loop through the upcomming list backwards and delete
identical entries */
for(j=imax-1; j > i ;j--){
Tcl_ListObjIndex(interp, directories, j, &compDirectory);
Tcl_IncrRefCount(compDirectory);
if(Tcl_FSEqualPaths(currDirectory,compDirectory) == 1){
//Delete from list
//This should work since j then should be the index of the
previous entry
Tcl_ListObjReplace(interp, directories, j, 1, 0, NULL);
}
}
Tcl_DecrRefCount(currDirectory);
Tcl_DecrRefCount(compDirectory);
}

I would appreciate all the help I can get on this.

/Fredrik
From: Alexandre Ferrieux on
On 28 nov, 15:23, Fredrik Karlsson <dargo...(a)gmail.com> wrote:
>
>                 for(i=0; Tcl_ListObjLength(interp, directories, &imax) && i < imax ;

In addition to being of questionable readability, the && above will
fail systematically because Tcl_ListObjLength returns TCL_OK, which is
zero, yielding false. You want:

for(i=0; (TCL_OK==Tcl_ListObjLength(interp, directories, &imax)) &&
i < imax ;

Note that while I had to RTFM to detect this, you could have easily
single-stepped or printf'ed the bug away. Try this before asking next
time.

As a side note, I wonder what you're trying to achieve at C level;
indeed all these operations are trivially handled in a few lines of
Tcl...

-Alex
From: Gerald W. Lester on
Fredrik Karlsson wrote:
> Hi,
>
> I am trying to get a unique list of paths for later glob:ing, but I
> cannot get stuff to be removed from the list I am using. What am I
> doing wrong?

Doing it in C. This is a simple task in Tcl. Just call Tcl_*Eval* with a
Tcl command.

>...
> I would appreciate all the help I can get on this.

The Tcl to eval would be:
set directories [lsort -unique $directories]



--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
From: Fredrik Karlsson on
On 28 Nov, 17:36, "Gerald W. Lester" <Gerald.Les...(a)cox.net> wrote:
> The Tcl to eval would be:
>         set directories [lsort -unique $directories]

Hi,

Yes, to be honest, I had forgot about Tcl_Eval().. though, I do admit
that I maybe would have not wanted to use it anyway. What happens if
the user input is
"Some file {which I will explain "later' `with" (qoutes removed) or
something really nasty. I am really insecure when it comes to
protecting strings in a fool-proof way. I am sure that {} protecting
it solves lots of issues, but maybe strings that are not meant to be
evaluated as part of a Tcl script should be kept from being possibly
evaluated as part of a script as much as possible?

Just my paranoia, maybe, but keeping user input away from parsing as
much as possible seems safer to me. If I am wrong, please tell me.

/Fredrik


From: Donal K. Fellows on
On 30 Nov, 15:22, Fredrik Karlsson <dargo...(a)gmail.com> wrote:
> Yes, to be honest, I had forgot about Tcl_Eval().. though, I do admit
> that I maybe would have not wanted to use it anyway. What happens if
> the user input is
> "Some file {which I will explain "later' `with" (qoutes removed) or
> something really nasty.

That's when you either use an appropriate construction function to
build the string (Tcl_NewListObj and Tcl_DStringAppendElement are
useful here), or switch to using Tcl_EvalObjv. The latter has no
quoting hazards, guaranteed (and in fact bypasses the parsing stage
entirely).

> Just my paranoia, maybe, but keeping user input away from parsing as
> much as possible seems safer to me. If I am wrong, please tell me.

Sounds very sensible to me. Good thing we've got the tools to do it
already.

Donal.