summaryrefslogtreecommitdiffstatshomepage
path: root/src/build
diff options
context:
space:
mode:
author Aaron Giles <aaron@aarongiles.com>2012-01-03 00:21:13 +0000
committer Aaron Giles <aaron@aarongiles.com>2012-01-03 00:21:13 +0000
commit64f1231c6300b144d946cb5530e40c288c43791f (patch)
tree92abaefa81552cb9ad78ff7c142e0fff5f9f7bba /src/build
parent8b492b8d80c5998f1e695706dfaa1bb1d35b9263 (diff)
Removed old C-based interface to astrings. astring exists only as
a class now. Updated all stragglers (mostly tools) to use the class form. [Aaron Giles]
Diffstat (limited to 'src/build')
-rw-r--r--src/build/makedep.c401
1 files changed, 165 insertions, 236 deletions
diff --git a/src/build/makedep.c b/src/build/makedep.c
index 51862ec3feb..2da090be743 100644
--- a/src/build/makedep.c
+++ b/src/build/makedep.c
@@ -57,45 +57,41 @@
TYPE DEFINITIONS
***************************************************************************/
-typedef struct _include_path include_path;
-struct _include_path
+struct include_path
{
include_path * next;
- const astring * path;
+ astring path;
};
-typedef struct _exclude_path exclude_path;
-struct _exclude_path
+struct exclude_path
{
exclude_path * next;
- const astring * path;
+ astring path;
int pathlen;
UINT8 recursive;
};
-typedef struct _list_entry list_entry;
-struct _list_entry
+struct list_entry
{
list_entry * next;
- const astring * name;
+ astring name;
};
-typedef struct _file_entry file_entry;
+struct file_entry;
-typedef struct _dependency dependency;
-struct _dependency
+struct dependency
{
dependency * next;
file_entry * file;
};
-struct _file_entry
+struct file_entry
{
- astring * name;
+ astring name;
dependency * deplist;
};
@@ -107,7 +103,7 @@ struct _file_entry
static include_path *incpaths;
static exclude_path *excpaths;
-static tagmap *file_map;
+static tagmap_t<file_entry *> file_map;
@@ -115,25 +111,12 @@ static tagmap *file_map;
PROTOTYPES
***************************************************************************/
-/* core output functions */
-static int recurse_dir(int srcrootlen, const astring *srcdir);
-static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);
+// core output functions
+static int recurse_dir(int srcrootlen, astring &srcdir);
+static file_entry &compute_dependencies(int srcrootlen, astring &srcfile);
-/* path helpers */
-static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
-
-
-
-/***************************************************************************
- INLINE FUNCTIONS
-***************************************************************************/
-
-/* core output functions */
-static int recurse_dir(int srcrootlen, const astring *srcdir);
-static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);
-
-/* path helpers */
-static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
+// path helpers
+static bool find_include_file(astring &srcincpath, int srcrootlen, const astring &srcfile, const astring &filename);
@@ -145,82 +128,66 @@ static astring *find_include_file(int srcrootlen, const astring *srcfile, const
main - main entry point
-------------------------------------------------*/
+void usage(const char *argv0)
+{
+ fprintf(stderr, "Usage:\n%s <srcroot> [-Iincpath [-Iincpath [...]]]\n", argv0);
+ exit(1);
+}
+
int main(int argc, char *argv[])
{
include_path **incpathhead = &incpaths;
exclude_path **excpathhead = &excpaths;
- astring *srcdir = NULL;
+ astring srcdir;
int unadorned = 0;
- int result;
- int argnum;
- /* loop over arguments */
- for (argnum = 1; argnum < argc; argnum++)
+ // loop over arguments
+ for (int argnum = 1; argnum < argc; argnum++)
{
char *arg = argv[argnum];
- /* include path? */
+ // include path?
if (arg[0] == '-' && arg[1] == 'I')
{
- *incpathhead = (include_path *)malloc(sizeof(**incpathhead));
- if (*incpathhead != NULL)
- {
- (*incpathhead)->next = NULL;
- (*incpathhead)->path = astring_replacechr(astring_dupc(&arg[2]), '/', PATH_SEPARATOR[0]);
- incpathhead = &(*incpathhead)->next;
- }
+ *incpathhead = new include_path;
+ (*incpathhead)->next = NULL;
+ (*incpathhead)->path.cpy(&arg[2]).replacechr('/', PATH_SEPARATOR[0]);
+ incpathhead = &(*incpathhead)->next;
}
- /* exclude path? */
+ // exclude path?
else if (arg[0] == '-' && arg[1] == 'X')
{
- *excpathhead = (exclude_path *)malloc(sizeof(**excpathhead));
- if (*excpathhead != NULL)
- {
- astring *path;
- (*excpathhead)->next = NULL;
- path = astring_replacechr(astring_dupc(&arg[2]), PATH_SEPARATOR[0], '/');
- (*excpathhead)->recursive = (astring_replacec(path, astring_len(path) - 4, "/...", "") != 0);
- (*excpathhead)->path = path;
- (*excpathhead)->pathlen = astring_len(path);
- excpathhead = &(*excpathhead)->next;
- }
+ *excpathhead = new exclude_path;
+ (*excpathhead)->next = NULL;
+ (*excpathhead)->path.cpy(&arg[2]).replacechr(PATH_SEPARATOR[0], '/');
+ (*excpathhead)->recursive = ((*excpathhead)->path.replace((*excpathhead)->path.len() - 4, "/...", "") != 0);
+ (*excpathhead)->pathlen = (*excpathhead)->path.len();
+ excpathhead = &(*excpathhead)->next;
}
- /* ignore -include which is used by sdlmame to include sdlprefix.h before all other includes */
+ // ignore -include which is used by sdlmame to include sdlprefix.h before all other includes
else if (strcmp(arg,"-include") == 0)
{
argnum++;
}
- /* other parameter */
+ // other parameter
else if (arg[0] != '-' && unadorned == 0)
{
- srcdir = astring_replacechr(astring_dupc(arg), '/', PATH_SEPARATOR[0]);
+ srcdir.cpy(arg).replacechr('/', PATH_SEPARATOR[0]);
unadorned++;
}
else
- goto usage;
+ usage(argv[0]);
}
- /* make sure we got 1 parameter */
- if (srcdir == NULL)
- goto usage;
-
- /* create a tagmap for tracking files we've visited */
- file_map = tagmap_alloc();
-
- /* recurse over subdirectories */
- result = recurse_dir(astring_len(srcdir), srcdir);
+ // make sure we got 1 parameter
+ if (srcdir.len() == 0)
+ usage(argv[0]);
- /* free source and destination directories */
- tagmap_free(file_map);
- astring_free(srcdir);
- return result;
-
-usage:
- fprintf(stderr, "Usage:\n%s <srcroot> [-Iincpath [-Iincpath [...]]]\n", argv[0]);
- return 1;
+ // recurse over subdirectories
+ return recurse_dir(srcdir.len(), srcdir);
}
@@ -233,7 +200,7 @@ static int compare_list_entries(const void *p1, const void *p2)
{
const list_entry *entry1 = *(const list_entry **)p1;
const list_entry *entry2 = *(const list_entry **)p2;
- return strcmp(astring_c(entry1->name), astring_c(entry2->name));
+ return entry1->name.cmp(entry2->name);
}
@@ -243,25 +210,22 @@ static int compare_list_entries(const void *p1, const void *p2)
unless we already exist in the map
-------------------------------------------------*/
-static void recurse_dependencies(file_entry *file, tagmap *map)
+static void recurse_dependencies(file_entry &file, tagmap_t<astring *> &map)
{
- int filelen = astring_len(file->name);
- exclude_path *exclude;
- dependency *dep;
-
- /* skip if we're in an exclude path */
- for (exclude = excpaths; exclude != NULL; exclude = exclude->next)
- if (exclude->pathlen < filelen && strncmp(astring_c(file->name), astring_c(exclude->path), exclude->pathlen) == 0)
- if (exclude->recursive || astring_chr(file->name, exclude->pathlen + 1, '/') == -1)
+ // skip if we're in an exclude path
+ int filelen = file.name.len();
+ for (exclude_path *exclude = excpaths; exclude != NULL; exclude = exclude->next)
+ if (exclude->pathlen < filelen && strncmp(file.name, exclude->path, exclude->pathlen) == 0)
+ if (exclude->recursive || file.name.chr(exclude->pathlen + 1, '/') == -1)
return;
- /* attempt to add; if we get an error, we're already present */
- if (tagmap_add(map, astring_c(file->name), file->name, FALSE) != TMERR_NONE)
+ // attempt to add; if we get an error, we're already present
+ if (map.add(file.name, &file.name) != TMERR_NONE)
return;
- /* recurse the list from there */
- for (dep = file->deplist; dep != NULL; dep = dep->next)
- recurse_dependencies(dep->file, map);
+ // recurse the list from there
+ for (dependency *dep = file.deplist; dep != NULL; dep = dep->next)
+ recurse_dependencies(*dep->file, map);
}
@@ -269,122 +233,106 @@ static void recurse_dependencies(file_entry *file, tagmap *map)
recurse_dir - recurse through a directory
-------------------------------------------------*/
-static int recurse_dir(int srcrootlen, const astring *srcdir)
+static int recurse_dir(int srcrootlen, astring &srcdir)
{
static const osd_dir_entry_type typelist[] = { ENTTYPE_DIR, ENTTYPE_FILE };
int result = 0;
- int entindex;
- /* iterate first over directories, then over files */
- for (entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++)
+ // iterate first over directories, then over files
+ for (int entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++)
{
osd_dir_entry_type entry_type = typelist[entindex];
- const osd_directory_entry *entry;
- list_entry **listarray = NULL;
- list_entry *list = NULL;
- list_entry *curlist;
- osd_directory *dir;
- int found = 0;
- /* open the directory and iterate through it */
- dir = osd_opendir(astring_c(srcdir));
+ // open the directory and iterate through it
+ osd_directory *dir = osd_opendir(srcdir);
if (dir == NULL)
{
result = 1;
goto error;
}
- /* build up the list of files */
+ // build up the list of files
+ const osd_directory_entry *entry;
+ list_entry *list = NULL;
+ int found = 0;
while ((entry = osd_readdir(dir)) != NULL)
if (entry->type == entry_type && entry->name[0] != '.')
{
- list_entry *lentry = (list_entry *)malloc(sizeof(*lentry));
- lentry->name = astring_dupc(entry->name);
+ list_entry *lentry = new list_entry;
+ lentry->name.cpy(entry->name);
lentry->next = list;
list = lentry;
found++;
}
- /* close the directory */
+ // close the directory
osd_closedir(dir);
- /* skip if nothing found */
+ // skip if nothing found
if (found == 0)
continue;
- /* allocate memory for sorting */
- listarray = (list_entry **)malloc(sizeof(list_entry *) * found);
+ // allocate memory for sorting
+ list_entry **listarray = new list_entry *[found];
found = 0;
- for (curlist = list; curlist != NULL; curlist = curlist->next)
+ for (list_entry *curlist = list; curlist != NULL; curlist = curlist->next)
listarray[found++] = curlist;
- /* sort the list */
+ // sort the list
qsort(listarray, found, sizeof(listarray[0]), compare_list_entries);
- /* rebuild the list */
+ // rebuild the list
list = NULL;
while (--found >= 0)
{
listarray[found]->next = list;
list = listarray[found];
}
- free(listarray);
+ delete[] listarray;
- /* iterate through each file */
- for (curlist = list; curlist != NULL && result == 0; curlist = curlist->next)
+ // iterate through each file
+ for (list_entry *curlist = list; curlist != NULL && result == 0; curlist = curlist->next)
{
- astring *srcfile;
+ astring srcfile;
- /* build the source filename */
- srcfile = astring_alloc();
- astring_printf(srcfile, "%s%c%s", astring_c(srcdir), PATH_SEPARATOR[0], astring_c(curlist->name));
+ // build the source filename
+ srcfile.printf("%s%c%s", srcdir.cstr(), PATH_SEPARATOR[0], curlist->name.cstr());
- /* if we have a file, output it */
+ // if we have a file, output it
if (entry_type == ENTTYPE_FILE)
{
- /* make sure we care, first */
- if (core_filename_ends_with(astring_c(curlist->name), ".c"))
+ // make sure we care, first
+ if (core_filename_ends_with(curlist->name, ".c"))
{
- tagmap *depend_map = tagmap_alloc();
- tagmap_entry *map_entry;
- file_entry *file;
- astring *target;
- int taghash;
-
- /* find dependencies */
- file = compute_dependencies(srcrootlen, srcfile);
- recurse_dependencies(file, depend_map);
+ tagmap_t<astring *> depend_map;
- /* convert the target from source to object (makes assumptions about rules) */
- target = astring_dup(file->name);
- astring_replacec(target, 0, "src/", "$(OBJ)/");
- astring_replacec(target, 0, ".c", ".o");
- printf("\n%s : \\\n", astring_c(target));
+ // find dependencies
+ file_entry &file = compute_dependencies(srcrootlen, srcfile);
+ recurse_dependencies(file, depend_map);
- /* iterate over the hashed dependencies and output them as well */
- for (taghash = 0; taghash < TAGMAP_HASH_SIZE; taghash++)
- for (map_entry = depend_map->table[taghash]; map_entry != NULL; map_entry = map_entry->next)
- printf("\t%s \\\n", astring_c((astring *)map_entry->object));
+ // convert the target from source to object (makes assumptions about rules)
+ astring target(file.name);
+ target.replace(0, "src/", "$(OBJ)/");
+ target.replace(0, ".c", ".o");
+ printf("\n%s : \\\n", target.cstr());
- astring_free(target);
- tagmap_free(depend_map);
+ // iterate over the hashed dependencies and output them as well
+ for (int taghash = 0; taghash < TAGMAP_HASH_SIZE; taghash++)
+ for (tagmap_entry *map_entry = depend_map.table[taghash]; map_entry != NULL; map_entry = map_entry->next)
+ printf("\t%s \\\n", ((astring *)map_entry->object)->cstr());
}
}
- /* if we have a directory, recurse */
+ // if we have a directory, recurse
else
result = recurse_dir(srcrootlen, srcfile);
-
- /* free memory for the names */
- astring_free(srcfile);
}
- /* free all the allocated entries */
+ // free all the allocated entries
while (list != NULL)
{
list_entry *next = list->next;
- astring_free((astring *)list->name);
- free(list);
+ delete list;
list = next;
}
}
@@ -399,84 +347,73 @@ error:
HTML
-------------------------------------------------*/
-static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile)
+static file_entry &compute_dependencies(int srcrootlen, astring &srcfile)
{
- astring *normalfile;
+ // see if we already have an entry
+ astring normalfile(srcfile);
+ normalfile.replacechr(PATH_SEPARATOR[0], '/');
+ file_entry *foundfile = file_map.find(normalfile);
+ if (foundfile != NULL)
+ return *foundfile;
+
+ // create a new header entry
+ file_entry &file = *new file_entry;
+ file.deplist = NULL;
+ file.name = normalfile;
+ file_map.add(file.name, &file);
+
+ // read the source file
UINT32 filelength;
- file_entry *file;
char *filedata;
- int index;
-
- /* see if we already have an entry */
- normalfile = astring_dup(srcfile);
- astring_replacechr(normalfile, PATH_SEPARATOR[0], '/');
- file = (file_entry *)tagmap_find(file_map, astring_c(normalfile));
- if (file != NULL)
- return file;
-
- /* create a new header entry */
- file = (file_entry *)malloc(sizeof(*file));
- file->deplist = NULL;
- file->name = normalfile;
- tagmap_add(file_map, astring_c(file->name), file, FALSE);
-
- /* read the source file */
- if (core_fload(astring_c(srcfile), (void **)&filedata, &filelength) != FILERR_NONE)
+ if (core_fload(srcfile, (void **)&filedata, &filelength) != FILERR_NONE)
{
- fprintf(stderr, "Unable to read file '%s'\n", astring_c(srcfile));
+ fprintf(stderr, "Unable to read file '%s'\n", srcfile.cstr());
return file;
}
- /* find the #include directives in this file */
- for (index = 0; index < filelength; index++)
+ // find the #include directives in this file
+ for (int index = 0; index < filelength; index++)
if (filedata[index] == '#' && strncmp(&filedata[index + 1], "include", 7) == 0)
{
- astring *filename, *target;
- int scan = index;
- dependency *dep;
- int start;
- int just_continue = 0;
-
- /* first make sure we're not commented or quoted */
- for (scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--)
+ // first make sure we're not commented or quoted
+ bool just_continue = false;
+ for (int scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--)
if ((filedata[scan] == '/' && filedata[scan - 1] == '/') || filedata[scan] == '"')
{
- just_continue = 1;
+ just_continue = true;
break;
}
if (just_continue)
continue;
- /* scan forward to find the quotes or bracket */
+ // scan forward to find the quotes or bracket
index += 7;
+ int scan;
for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ;
- /* ignore if not found or if it's bracketed */
+ // ignore if not found or if it's bracketed
if (scan >= filelength || filedata[scan] != '"')
continue;
- start = ++scan;
+ int start = ++scan;
- /* find the closing quote */
+ // find the closing quote
while (scan < filelength && filedata[scan] != '"')
scan++;
if (scan >= filelength)
continue;
- /* find the include file */
- filename = astring_dupch(&filedata[start], scan - start);
- target = find_include_file(srcrootlen, srcfile, filename);
+ // find the include file
+ astring filename(&filedata[start], scan - start);
+ astring target;
- /* create a new dependency */
- if (target != NULL)
+ // create a new dependency
+ if (find_include_file(target, srcrootlen, srcfile, filename))
{
- dep = (dependency *)malloc(sizeof(*dep));
- dep->next = file->deplist;
- file->deplist = dep;
- dep->file = compute_dependencies(srcrootlen, target);
- astring_free(target);
+ dependency *dep = new dependency;
+ dep->next = file.deplist;
+ file.deplist = dep;
+ dep->file = &compute_dependencies(srcrootlen, target);
}
-
- astring_free(filename);
}
osd_free(filedata);
@@ -493,59 +430,51 @@ static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile)
find_include_file - find an include file
-------------------------------------------------*/
-static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename)
+static bool find_include_file(astring &srcincpath, int srcrootlen, const astring &srcfile, const astring &filename)
{
- include_path *curpath;
-
- /* iterate over include paths and find the file */
- for (curpath = incpaths; curpath != NULL; curpath = curpath->next)
+ // iterate over include paths and find the file
+ for (include_path *curpath = incpaths; curpath != NULL; curpath = curpath->next)
{
- astring *srcincpath = astring_dup(curpath->path);
- core_file *testfile;
+ // a '.' include path is specially treated
+ if (curpath->path == ".")
+ srcincpath.cpysubstr(srcfile, 0, srcfile.rchr(0, PATH_SEPARATOR[0]));
+ else
+ srcincpath.cpy(curpath->path);
+
+ // append the filename piecemeal to account for directories
int lastsepindex = 0;
int sepindex;
-
- /* a '.' include path is specially treated */
- if (astring_cmpc(curpath->path, ".") == 0)
- astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0]));
-
- /* append the filename piecemeal to account for directories */
- while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1)
+ while ((sepindex = filename.chr(lastsepindex, '/')) != -1)
{
- astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex);
+ astring pathpart(filename, lastsepindex, sepindex - lastsepindex);
- /* handle .. by removing a chunk from the incpath */
- if (astring_cmpc(pathpart, "..") == 0)
+ // handle .. by removing a chunk from the incpath
+ if (pathpart == "..")
{
- int sepindex_part = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]);
+ int sepindex_part = srcincpath.rchr(0, PATH_SEPARATOR[0]);
if (sepindex_part != -1)
- astring_substr(srcincpath, 0, sepindex_part);
+ srcincpath.substr(0, sepindex_part);
}
- /* otherwise, append a path separator and the pathpart */
+ // otherwise, append a path separator and the pathpart
else
- astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart);
+ srcincpath.cat(PATH_SEPARATOR).cat(pathpart);
- /* advance past the previous index */
+ // advance past the previous index
lastsepindex = sepindex + 1;
-
- /* free the path part we extracted */
- astring_free(pathpart);
}
- /* now append the filename */
- astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1);
+ // now append the filename
+ srcincpath.cat(PATH_SEPARATOR).catsubstr(filename, lastsepindex, -1);
- /* see if we can open it */
- if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE)
+ // see if we can open it
+ core_file *testfile;
+ if (core_fopen(srcincpath, OPEN_FLAG_READ, &testfile) == FILERR_NONE)
{
- /* close the file */
+ // close the file
core_fclose(testfile);
- return srcincpath;
+ return true;
}
-
- /* free our include path */
- astring_free(srcincpath);
}
- return NULL;
+ return false;
}