Subscribe to WolfCoder's Kawaii Column        RSS Feed
-----

Generating a list of strings from a file directory (WIN32)

Icon Leave Comment
I'll jump on the random snippet and experience bandwagon and post my solution to generating a list of strings from a file directory using the good ol' WIN32 API. This little snippet is actually from a part of some engine of mine that builds a list of all the resources in some folders and I needed it to be dynamic. That is, every time you start the game it will rebuild an index that reflects the current state of the resources- using the file system itself as the references for all resources so I don't have to write my own manifest files and stuff.

wbres.h
#ifndef WBRES_H
#define WBRES_H

/* Defines */
#define WIN32_LEAN_AND_MEAN

/* Typedefs */
typedef struct
{
	int count;
	char **files;
}wbres_list;

/*
	Returns the number of strings found in the directory given.
*/
extern int wbres_ls_num(char *dir);

/*
	Returns a list of strings found in the directory given.
	These strings must be freed when they are finished being used.
*/
extern wbres_list *wbres_ls(char *dir);

/*
	Frees the memory used by the resource string list.
*/
extern void wbres_ls_free(wbres_list *lss);

#endif



wbres.cpp
/*
	WeaponBirth Resource Commander
	written by Sayuri L. Kitsune (2010)
*/

/* Includes */
#include <windows.h>
#include "wbres.h"

/* Returns the number of names found in a directory */
int wbres_ls_num(char *dir)
{
	WIN32_FIND_DATAA fd;
	HANDLE hf;
	int t;
	/* Find the first file */
	hf = FindFirstFileA(dir,&fd);
	if(hf == INVALID_HANDLE_VALUE)
		return 0; /* Function failed */
	/* We have a total */
	t = 1;
	/* Find the rest of the files */
	while(FindNextFileA(hf,&fd))
		t++;
	/* Return the result */
	return t;
}

/* Generate a list of names in a directory */
wbres_list *wbres_ls(char *dir)
{
	WIN32_FIND_DATAA fd;
	HANDLE hf;
	int fcount;
	wbres_list *ret;
	int i;
	size_t tsize;
	/* Get the number of files */
	fcount = wbres_ls_num(dir);
	/* Allocate N pointers */
	ret = (wbres_list*)malloc(sizeof(wbres_list));
	ret->count = fcount;
	ret->files = (char**)malloc(sizeof(char*)*fcount);
	/* Find the first file */
	hf = FindFirstFileA(dir,&fd);
	if(hf == INVALID_HANDLE_VALUE)
		return NULL; /* Function failed */
	/* For each entry, copy the string */
	for(i = 0;i < fcount;i++)
	{
		/* Allocate and copy */
		tsize = strlen(fd.cFileName)+1;
		ret->files[i] = (char*)malloc(tsize);
		memcpy_s(ret->files[i],tsize,fd.cFileName,tsize);
		/* Get next */
		if(!FindNextFileA(hf,&fd))
			break;
	}
	/* Return the result */
	return ret;
}

/* Free memory */
void wbres_ls_free(wbres_list *lss)
{
	int i;
	/* Free all files */
	for(i = 0;i < lss->count;i++)
		free(lss->files[i]);
	/* Free data */
	free(lss->files);
	free(lss);
}



Pretty crude, quick, but at least it does what I need it to.

0 Comments On This Entry

 

November 2014

S M T W T F S
      1
2345678
9101112131415
16171819202122
232425262728 29
30      

...

...

Information

Information