2 registered members (SBGuy, Quad),
768
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
wildcards (for txt_for_dir etc.)
#460639
07/07/16 12:57
07/07/16 12:57
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi, is there any tutorial or example of using multiple wildcards with e.g. txt_for_dir?
I know e.g. "*.mdl" or "*", but how to e.g. also filter out filenames that start with a certain letter, like e.g. an underscore?
Thanks!
Last edited by Reconnoiter; 07/07/16 12:58.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: Reconnoiter]
#460646
07/07/16 14:33
07/07/16 14:33
|
Malice
Unregistered
|
Malice
Unregistered
|
but how to e.g. also filter out filenames that start with a certain letter, like e.g. an underscore? Edited Having looked over the manual , there is no way. The wildcards are used to find not exclude files found. The function reads all none-empty files into separate strings. *.mld or ?layer.mdl both are ways of finding files. EDIT2 - Thoughts on achieving the goal - In many cases the file extension name(char set) is not important. So save.txt and save.txx can both be text files and if a text editor opens and looks at them, it will read it perfectly as a text file. This can be used to sort text files. Like so txt_for_dir(tDir,"*.txt"); txt_for_dir(tDir,"*.txx");
Last edited by Malice; 07/07/16 14:50.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: ]
#460647
07/07/16 15:15
07/07/16 15:15
|
Joined: Jul 2008
Posts: 2,107 Germany
rayp
X
|
X
Joined: Jul 2008
Posts: 2,107
Germany
|
//Reads filenames that match the given wildcard/s into separate strings of a text object.
//This instruction can be used to scan a folder for files matching MULTIPLE wildcards.
//-----------------------------------------------------------------------------------------
//Parameters:
//targ Pointer to the target TEXT* pointer for the TEXT object to be filled.
// If the target TEXT* pointer is NULL, a new TEXT object is created there.
// If the target TEXT* pointer contains a TEXT object, then the filenames
// found by this function are ADDED to the END of this existing text object.
//
//pathname Absolute or Relative Pathname of folder to be searched
//
//wildcards File names (C-style wildcards) to be matched, '|' separates wildcards.
// eg: "*.MDL|*.HMP|-*_open.*" This will include all MDL and HMP files,
// and from that list it will then exclude all "*_open.*" files.
//-----------------------------------------------------------------------------------------
//Returns:
//Number of strings ADDED to 'targ' TEXT object.
//=========================================================================================
var txt_for_dir_plus( TEXT** targ, STRING* pathname, STRING* wildcards)
{ if((!targ)||(!pathname)||(!wildcards)) return(0); //un-resolvable parameter set
//--------------------------------------------------------------------------------------
TEXT *targ_txt, *tmp_txt=NULL,*sub_txt=NULL; STRING* tmp_str = str_create("");
var f_count,t_cnt, wilds, i,ii,sub;
//--------------------------------------------------------------------------------------
//Extract / separate wildcards
str_cpy(tmp_str, wildcards); wilds = 0;
while(str_stri(tmp_str,"|")) { str_clip(tmp_str,str_stri(tmp_str,"|")); wilds++; }
STRING* *all_wilds = (STRING**)sys_malloc(sizeof(STRING*) * (wilds++));
memset(all_wilds, 0, sizeof(STRING*) * wilds); str_cpy(tmp_str, wildcards);
for(i=0; i<wilds; i++)
{ all_wilds[i] = str_create(tmp_str);
if(str_stri(tmp_str,"|")) { str_clip(tmp_str,str_stri(tmp_str,"|"));
str_trunc(all_wilds[i],str_len(tmp_str)+1); } }
//--------------------------------------------------------------------------------------
//ADD files to temporary TEXT for each wildcard that DOESNT start with a '-' sign
tmp_txt=txt_create(0,0); tmp_txt.strings=0; sub_txt=txt_create(16,0);
for(t_cnt=0; t_cnt<wilds; t_cnt++)
{ if(str_to_asc(all_wilds[t_cnt])!='-')
{ str_cpy(tmp_str, pathname);
if(((tmp_str.chars)[tmp_str.length-2]!='\\')&&(str_len(tmp_str)))
{ str_cat(tmp_str, "\\"); } str_cat(tmp_str, all_wilds[t_cnt]);
while((sub=txt_for_dir(sub_txt,tmp_str))>=sub_txt.strings)
{ for(i=0; i<sub; i++) { txt_addstring(sub_txt, ""); } }
for(i=0; i<sub_txt.strings; i++)
{ if(str_len((sub_txt.pstring)[i])) txt_addstring(tmp_txt,(sub_txt.pstring)[i]);
str_cpy((sub_txt.pstring)[i], ""); } } }
//--------------------------------------------------------------------------------------
//REMOVE files from temporary TEXT for each wildcard that DOES start with a '-' sign
for(t_cnt=0; t_cnt<wilds; t_cnt++)
{ if(str_to_asc(all_wilds[t_cnt])=='-')
{ str_cpy(tmp_str, pathname);
if(((tmp_str.chars)[tmp_str.length-2]!='\\')&&(str_len(tmp_str)))
{ str_cat(tmp_str, "\\"); } str_cat(tmp_str, str_clip(all_wilds[t_cnt],1));
while((sub=txt_for_dir(sub_txt,tmp_str))>=sub_txt.strings)
{ for(i=0; i<sub; i++) { txt_addstring(sub_txt, ""); } }
for(i=0; i<sub_txt.strings; i++)
{ if(str_len((sub_txt.pstring)[i]))
for(ii=0; ii<tmp_txt.strings; ii++)
if(str_cmp((tmp_txt.pstring)[ii],(sub_txt.pstring)[i])==1)
{ str_cpy((tmp_txt.pstring)[ii], ""); }
str_cpy((sub_txt.pstring)[i], ""); } } }
//--------------------------------------------------------------------------------------
//TRANSFER from temporary TEXT to target TEXT, after creating target if it needed to be
targ_txt=*targ; if(!targ_txt)
{ *targ = targ_txt = txt_create(0,0); reset(targ_txt, SHOW); targ_txt.strings=0; }
f_count=0; for(i=0; i<tmp_txt.strings; i++)
{ if(str_len((tmp_txt.pstring)[i]))
{ txt_addstring(targ_txt, (tmp_txt.pstring)[i]); f_count++; } }
//--------------------------------------------------------------------------------------
//Cleanup Workspace
for(t_cnt=0; t_cnt<wilds; t_cnt++) { str_remove(all_wilds[t_cnt]); }
sys_free(all_wilds); str_remove(tmp_str);
if(tmp_txt) { for(i=0; i<tmp_txt.strings; i++)
if((tmp_txt.pstring)[i]) str_remove((tmp_txt.pstring)[i]); txt_remove(tmp_txt); }
if(sub_txt) { for(i=0; i<sub_txt.strings; i++)
if((sub_txt.pstring)[i]) str_remove((sub_txt.pstring)[i]); txt_remove(sub_txt); }
//--------------------------------------------------------------------------------------
return(f_count); }
//=========================================================================================
Might be usefull txt_for_dir_plus, written by evilSOB.
Acknex umgibt uns...zwischen Dir, mir, dem Stein dort... "Hey Griswold ... where u gonna put a tree that big ?" 1998 i married my loved wife ... Sheeva from Mortal Kombat, not Evil-Lyn as might have been expected rayp.flags |= UNTOUCHABLE;
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: ]
#460648
07/07/16 16:11
07/07/16 16:11
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
This can be used to sort text files. Like so txt_for_dir(tDir,"*.txt"); txt_for_dir(tDir,"*.txx"); , didn't know that, nice tip. @rayp, that looks like exactly I could use. I get an invalid memory error when trying to use it though : (note: tmp_str is the path to the folder, same as I used with txt_for_dir but just without the wildcard)
var num = txt_for_dir_plus(mytext, tmp_str, "*.MDL|*.HMP|-*_open.*");
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: Reconnoiter]
#460652
07/07/16 17:24
07/07/16 17:24
|
Malice
Unregistered
|
Malice
Unregistered
|
While I can not yet say why. The error would be in these lines than.
STRING* *all_wilds = (STRING**)sys_malloc(sizeof(STRING*) * (wilds++));
memset(all_wilds, 0, sizeof(STRING*) * wilds);
-The * use is unclear to me and a bit above my skill. Reference deference, point to pointer.. . Edit - I would think
STRING* all_wilds = (STRING*)sys_malloc(sizeof(STRING*) * (wilds++));
But that I'm sure I'm wrong... Also the sys_* function is dependent on the nexus size. You can try -nx 500 or something for testing if that is the issue. For a google Invalid Memory Access
This error occurs when a read or write instruction references unallocated or deallocated memory.
Last edited by Malice; 07/07/16 17:37.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: ]
#460654
07/07/16 18:04
07/07/16 18:04
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
While I can not yet say why. The error would be in these lines than.
-The * use is unclear to me and a bit above my skill. Reference deference, point to pointer.. . Edit - I would think
But that I'm sure I'm wrong... Also the sys_* function is dependent on the nexus size. You can try -nx 500 or something for testing if that is the issue. It simply is a dynamic array of String pointers, nothing advanced.
Last edited by Ch40zzC0d3r; 07/07/16 18:05.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: Ch40zzC0d3r]
#460655
07/07/16 22:28
07/07/16 22:28
|
Malice
Unregistered
|
Malice
Unregistered
|
It simply is a dynamic array of String pointers, nothing advanced.
Yes I'm sure it's not advanced and I'd have to just read a few minutes. However I meant I do not understand the doubling of * '**' '* *' in this code.
STRING* *all_wilds = (STRING**)
Things are beyond me because of ignorance not complexity.
Last edited by Malice; 07/07/16 22:30.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: Reconnoiter]
#460665
07/08/16 22:32
07/08/16 22:32
|
Malice
Unregistered
|
Malice
Unregistered
|
No error here. This works
var num = txt_for_dir_plus(mytext, "", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;
This fails
var num = txt_for_dir_plus(mytext, "\\FDay", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;
This works
var num = txt_for_dir_plus(mytext, "FDay", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;
THIS also works
var num = txt_for_dir_plus(mytext, "FDay\\", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;
Notice line 40-41 of evilSOB's code str_cat's
if(((tmp_str.chars)[tmp_str.length-2]!='\\')&&(str_len(tmp_str)))
{ str_cat(tmp_str, "\\"); } str_cat(tmp_str, all_wilds[t_cnt]);
Based on my results. Check the value of 'tmp_str' in you project. Because a hard/hand coded address works for me with no errors. Further notes are - I used a single main folder with only 10 mdl/hmp files to list, my text held 100 strings and I surely did not exceed that. Meaning that level memory and nexus on hand was more than enough. Take Care --
Last edited by Malice; 07/08/16 22:36.
|
|
|
Re: wildcards (for txt_for_dir etc.)
[Re: ]
#460670
07/09/16 11:32
07/09/16 11:32
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
#include <acknex.h>
#include <windows.h>
typedef struct
{
char* filename[MAX_PATH];
}my_files;
int total_found=0;
//char* findthis : search criteria
//char* skipthischar :exclude filenames with an occurance of this caracter/s
//int atpos :the occurance of the caracter must be at this position ,to skip the filename
// for example, skipthischar="_" , atposition=1 in string "_example.exe" wil be skipped ,but "ex_ample" not
// if atpos=0 ,then the filename is skipped no matter where the position is at eg:"ex_ample" will be skipped
//int max_filenames :max filename's array size
my_files* get_filenames(char* findthis,char* skipthischar,int atpos,int max_filenames)
{
if(atpos<1) atpos=0;
if(atpos>MAX_PATH)atpos=MAX_PATH;
int current=0;
my_files *filenames=malloc(sizeof(my_files)*max_filenames);
WIN32_FIND_DATA find_result;
char searchfor[MAX_PATH];
HANDLE find_handle=INVALID_HANDLE_VALUE;
DWORD error=0;
memcpy(searchfor,findthis,MAX_PATH);
find_handle=FindFirstFile(searchfor,&find_result);
if(find_handle==INVALID_HANDLE_VALUE)
{
printf("none found");
FindClose(find_handle);
return;
}
//first found
else
{
if(atpos>0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)!=atpos)
memcpy(filenames[0].filename,_chr(find_result.cFileName),MAX_PATH);
}
else if(atpos==0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)==atpos)
memcpy(filenames[0].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
//find others
while(FindNextFile(find_handle,&find_result)!=0)
{
error=GetLastError();
if(error!=ERROR_NO_MORE_FILES)
{
if(atpos>0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)!=atpos)
{
if(current+1<max_filenames)
{
current+=1;
memcpy(filenames[current].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
}
else if(atpos==0)
{
if(str_stri(_chr(find_result.cFileName),skipthischar)==atpos)
{
if(current+1<max_filenames)
{
current+=1;
memcpy(filenames[current].filename,_chr(find_result.cFileName),MAX_PATH);
}
}
}
}
}
FindClose(find_handle);
total_found=current+1;
return filenames;
}
void main()
{
//find *.mdl but skip all that has "_" at position 1
my_files *filenames=get_filenames("*.mdl","_",1,100);
//or
//find *.mdl but skip all that has "_" at any position
//my_files *filenames=get_filenames("*.mdl","_",0,100);
if(filenames!=NULL)
{
int i;
for(i=0;i<total_found;i++)
{
printf(_chr(filenames[i]));
}
sys_free(filenames);
}
}
theres some bad mistakes , sure you can fix it if you want it.
Compulsive compiler
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|