wildcards (for txt_for_dir etc.)

Posted By: Reconnoiter

wildcards (for txt_for_dir etc.) - 07/07/16 12:57

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!
Posted By: Anonymous

Re: wildcards (for txt_for_dir etc.) - 07/07/16 14:33

Quote:
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");
Posted By: rayp

Re: wildcards (for txt_for_dir etc.) - 07/07/16 15:15

Code:
//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.
Posted By: Reconnoiter

Re: wildcards (for txt_for_dir etc.) - 07/07/16 16:11

Originally Posted By: Malice
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 confused :

(note: tmp_str is the path to the folder, same as I used with txt_for_dir but just without the wildcard)
Code:
var num = txt_for_dir_plus(mytext, tmp_str, "*.MDL|*.HMP|-*_open.*");

Posted By: Anonymous

Re: wildcards (for txt_for_dir etc.) - 07/07/16 17:24

While I can not yet say why. The error would be in these lines than.
Code:
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
Code:
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
Quote:
Invalid Memory Access

This error occurs when a read or write instruction references unallocated or deallocated memory.


Posted By: Ch40zzC0d3r

Re: wildcards (for txt_for_dir etc.) - 07/07/16 18:04

Originally Posted By: Malice
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.
Posted By: Anonymous

Re: wildcards (for txt_for_dir etc.) - 07/07/16 22:28

Quote:

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.
Code:
STRING* *all_wilds = (STRING**)



Things are beyond me because of ignorance not complexity.
Posted By: Reconnoiter

Re: wildcards (for txt_for_dir etc.) - 07/08/16 10:27

Does the function work for others?
Perhaps it conflicts with something else in my project or perhaps I making some mistake with calling the function.
Posted By: Anonymous

Re: wildcards (for txt_for_dir etc.) - 07/08/16 22:32

No error here. This works

Code:
var num = txt_for_dir_plus(mytext, "", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;




This fails
Code:
var num = txt_for_dir_plus(mytext, "\\FDay", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;



This works
Code:
var num = txt_for_dir_plus(mytext, "FDay", "*.MDL|*.HMP|-*_open.*");
mytext->flags |= SHOW;


THIS also works
Code:
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
Code:
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 --
Posted By: Wjbender

Re: wildcards (for txt_for_dir etc.) - 07/09/16 11:32

Code:
#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.
Posted By: Reconnoiter

Re: wildcards (for txt_for_dir etc.) - 07/10/16 09:56

Thanks guys for all your comments. Through printf I see that the names are not correct for some reason except with txt_for_dir. I dont know why, tried increasing nexus, checked if some define interferes and all the path suggestions by Malice. I quess its something related to this project but its to big now and almost completed that its bit of finding a needle in haystack, so I will use Malice's suggestion of using txt_for_dir multiple times.
© 2024 lite-C Forums