Jump to content

  • Log In with Google      Sign In   
  • Create Account


MARS_999

Member Since 06 Mar 2001
Offline Last Active Jul 26 2014 02:04 PM
-----

Posts I've Made

In Topic: PhysicsFS help needed to read a directories files

05 July 2014 - 07:07 PM

I'm not sure how to do that in PhysFS, but if it helps at all, boost::filesystem does that fairly easily.
 
Here's my own convenience functions that I use:

//Elsewhere: typedef std::vector<std::string> StringList;

enum SubFoldersEnum {IncludeSubFolders, ExcludeSubFolders};

/*
	Returns a list of every file in the folder and (if desired) the files in each sub-folder.
	If 'timeThreshold' is specified, only files that have been written to *after* that threshold are returned.
	If 'fileFilterFunc' is specified, this filters out all file names (not the entire path) that don't pass the filter.
	If 'folderFilterFunc' is specified, entire subdirectories are filtered out.
	If 'pathPrefix' is specified, this determines what prefixes the returned paths.
	Suggestions for 'pathPrefix' are: Nothing ("FolderA"), "./" ("./FolderA") for relative paths, or passing in baseFolder for absolute paths.

	Example:
		"./File1.ext"
		"./File2.ext"
		"./FolderB/File3.ext"
		"./FolderB/File4.ext"
		"./FolderB/Folder2/File5.ext"
		"./File6.ext"
*/
StringList GetFilesInDirectory(const std::string &baseFolder, SubFoldersEnum subFoldersEnum, const std::string &pathPrefix, std::time_t timeThreshold)
{
	//Call the original function, but with filters that accept everything.
	return GetFilesInDirectory(baseFolder, IsAnything, IsAnything, subFoldersEnum, pathPrefix, timeThreshold);
}

StringList GetFilesInDirectory(const std::string &baseFolder, StringValidatorFunc fileFilterFunc, SubFoldersEnum subFoldersEnum, const std::string &pathPrefix, std::time_t timeThreshold)
{
	//Call the original function, but with a directory filter that accepts everything.
	return GetFilesInDirectory(baseFolder, fileFilterFunc, IsAnything, subFoldersEnum, pathPrefix, timeThreshold);
}

StringList GetFilesInDirectory(const std::string &baseFolder, StringValidatorFunc fileFilterFunc, StringValidatorFunc folderFilterFunc,
							   SubFoldersEnum subFoldersEnum, const std::string &pathPrefix, std::time_t timeThreshold)
{
	StringList listOfFiles;
	if(!DirectoryExists(baseFolder))
		return listOfFiles;

	boost::filesystem::directory_iterator directoryIterator(baseFolder);
	boost::filesystem::directory_iterator endOfDirectory; //An unitialized iterator is the end iterator.

	//Loop through everything in this directory.
	for(; directoryIterator != endOfDirectory; directoryIterator++)
	{
		//Make sure the item found is a file and not a folder.
		if(boost::filesystem::is_regular_file(directoryIterator->path()))
		{
			//Get the name of the folder.
			std::string filename = GetFilenameFromPath(directoryIterator->path().generic_string());

			//Check if the filename passes the filter.
			if(fileFilterFunc(filename))
			{
				//Check if it was written to after 'timeThreshold'.
				if(timeThreshold == 0 || timeThreshold < boost::filesystem::last_write_time(directoryIterator->path()))
				{
					//Add it to our list.
					listOfFiles.push_back(ConvertWinPathToUnix(pathPrefix + '/' + filename));
				}
			}
		}
		else if(boost::filesystem::is_directory(directoryIterator->path()))
		{
			//Check to make sure we want to crawl subfolders.
			if(subFoldersEnum == IncludeSubFolders)
			{
				//Get the name of the folder.
				std::string directoryName = GetFinalDirectoryFromPath(directoryIterator->path().generic_string());

				//Check if the directory name passes the filter.
				if(folderFilterFunc(directoryName))
				{
					//Get all subfolders and add them to the list.
					listOfFiles += GetFilesInDirectory(baseFolder + "/" + directoryName, fileFilterFunc, folderFilterFunc, IncludeSubFolders, (pathPrefix + '/' + directoryName + '/'), timeThreshold);
				}
			}
		}
	}

	return listOfFiles;
}

'StringValidatorFunc' is a function pointer (actually, a std::function<bool(const std::string&)>) that can take a string a return either true or false if it accepts the string. I use this for filtering files. Often I pass it my SV_WildcardMatch() functor, to easily filter out something like {"*.jpg", "*.jpeg", "*.png"} - I can also post the code for that, if you desire. It's a slightly modified version of this algorithm.

 

I have a similar function called GetFoldersInDirectory(). It returns folder names instead of file names.

yes please post it completely, I have a feeling I am going to need it and have to use boost. I didn't want to but SDL doesn't have anything to read a directories contents...

 

thanks!!!


In Topic: PhysicsFS help needed to read a directories files

05 July 2014 - 06:27 PM

There is a function that returns a list you have to free later:

https://icculus.org/physfs/docs/html/physfs_8h.html#a0f4ae950c2dae0735a91263ddd20fbf4

 

There is also a callback version just below that you provide your own data and function to.

 

I am not following this directory path requirements... I put this in and get nothing

 

 
char* path = "C:/Programming/Work/SDL/Debug/";
cmd_enumerate(path);
 

static int cmd_enumerate(char *args)
{
char **rc;
 
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
 
rc = PHYSFS_enumerateFiles(args);
 
if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
else
{
int file_count;
char **i;
for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
printf("%s\n", *i);
 
printf("\n total (%d) files.\n", file_count);
PHYSFS_freeList(rc);
} /* else */
 
return(1);
} /* cmd_enumerate */


In Topic: What are people using for a good GUI API with a layout editor...

01 July 2014 - 12:58 PM

I haven't tried it myself, but have you seen SFGUI? It's a GUI with widget layouts designed specifically for SFML.

 

Yeah I used it before when I was using SFML, but moved to SDL2... SFGUI currently only works with SFML and development of that lib is dead as far as I can tell, if not TOO slow last time I used it was year or more ago and still stuck in pre 1.0 version....


In Topic: What are people using for a good GUI API with a layout editor...

01 July 2014 - 12:38 PM

I would but I can get the binaries to work. I just want to grab an API e.g. SDL/SFML unzip and wham it works other than setup of VC++2012 directories to the libs and includes...

 

I am not seeing much out there sad...


In Topic: Help with the math on this?

21 June 2014 - 08:43 AM

I don't know enough to make it work, but in pseudocode

 

static int _x = 0;

int[red, green, blue, alpha] GetNextColor()

{

  //note, after 2^32 colors, this will fail. if you remove alpha, you only get 2^24 colors

  _x++;

  int alpha (_x >> 24) % 256 //Only take the last 8 bits

  int red = (_x >> 16) % 256 //I think this means that you will ignore the first 16 bits of _x, and only look at the next 8 bits

  int green = (_x >> 8) % 256 //Ignore the first 8 bits, and only look at the next 8 bits

  int blue = _x % 256 //Only look at the first 8 bits

  return [red, green, blue, alpha]

}

I am not sure what you are looking to do here? Is this allowing me to add one to the color palette and cycle all the way through rgb? so get all 16.7 million color variations?

 

If so thanks!

 

Update!

BedderDanu  thanks that worked great!!!

PARTNERS