From 64f1231c6300b144d946cb5530e40c288c43791f Mon Sep 17 00:00:00 2001 From: Aaron Giles Date: Tue, 3 Jan 2012 00:21:13 +0000 Subject: [PATCH] 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] --- src/build/makedep.c | 399 +++++++---------- src/emu/clifront.c | 10 +- src/emu/debug/debugcpu.c | 8 +- src/emu/debug/express.c | 6 +- src/emu/debugint/debugint.c | 1 - src/emu/diimage.c | 16 +- src/emu/emucore.h | 2 +- src/emu/emuopts.c | 6 +- src/emu/hash.c | 2 +- src/emu/image.c | 22 +- src/emu/image.h | 2 +- src/emu/input.c | 2 +- src/emu/ioport.c | 21 +- src/emu/softlist.c | 2 +- src/emu/uiimage.c | 66 ++- src/emu/uiimage.h | 4 +- src/lib/util/astring.c | 826 ++++++++++++------------------------ src/lib/util/astring.h | 399 ++++------------- src/lib/util/corefile.c | 6 +- src/lib/util/corefile.h | 2 +- src/lib/util/zippath.c | 233 +++++----- src/lib/util/zippath.h | 11 +- src/mame/drivers/jaguar.c | 6 +- src/osd/sdl/sdlmain.c | 2 +- src/tools/regrep.c | 131 +++--- src/tools/split.c | 98 ++--- src/tools/src2html.c | 617 ++++++++++++--------------- 27 files changed, 1069 insertions(+), 1831 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_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 [-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; + // make sure we got 1 parameter + if (srcdir.len() == 0) + usage(argv[0]); - /* create a tagmap for tracking files we've visited */ - file_map = tagmap_alloc(); - - /* recurse over subdirectories */ - result = recurse_dir(astring_len(srcdir), srcdir); - - /* free source and destination directories */ - tagmap_free(file_map); - astring_free(srcdir); - return result; - -usage: - fprintf(stderr, "Usage:\n%s [-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 &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; + tagmap_t depend_map; - /* find dependencies */ - file = compute_dependencies(srcrootlen, srcfile); + // find dependencies + file_entry &file = compute_dependencies(srcrootlen, srcfile); recurse_dependencies(file, 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)); + // 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()); - /* 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)); - - 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; } diff --git a/src/emu/clifront.c b/src/emu/clifront.c index 5921f758c1b..1b848c0644f 100644 --- a/src/emu/clifront.c +++ b/src/emu/clifront.c @@ -166,7 +166,7 @@ int cli_frontend::execute(int argc, char **argv) // determine the base name of the EXE astring exename; - core_filename_extract_base(&exename, argv[0], TRUE); + core_filename_extract_base(exename, argv[0], true); // if we have a command, execute that if (strlen(m_options.command()) != 0) @@ -345,7 +345,7 @@ void cli_frontend::listsource(const char *gamename) // iterate through drivers and output the info astring filename; while (drivlist.next()) - mame_printf_info("%-16s %s\n", drivlist.driver().name, core_filename_extract_base(&filename, drivlist.driver().source_file, FALSE)->cstr()); + mame_printf_info("%-16s %s\n", drivlist.driver().name, core_filename_extract_base(filename, drivlist.driver().source_file).cstr()); } @@ -435,7 +435,7 @@ void cli_frontend::listbrothers(const char *gamename) while (drivlist.next()) { int clone_of = drivlist.clone(); - mame_printf_info("%-16s %-16s %-16s\n", core_filename_extract_base(&filename, drivlist.driver().source_file, FALSE)->cstr(), drivlist.driver().name, (clone_of == -1 ? "" : drivlist.driver(clone_of).name)); + mame_printf_info("%-16s %-16s %-16s\n", core_filename_extract_base(filename, drivlist.driver().source_file).cstr(), drivlist.driver().name, (clone_of == -1 ? "" : drivlist.driver(clone_of).name)); } } @@ -1488,7 +1488,7 @@ void media_identifier::identify_file(const char *name) { // output the name astring basename; - mame_printf_info("%-20s", core_filename_extract_base(&basename, name, FALSE)->cstr()); + mame_printf_info("%-20s", core_filename_extract_base(basename, name).cstr()); m_total++; // attempt to open as a CHD; fail if not @@ -1570,7 +1570,7 @@ void media_identifier::identify_data(const char *name, const UINT8 *data, int le // output the name m_total++; astring basename; - mame_printf_info("%-20s", core_filename_extract_base(&basename, name, FALSE)->cstr()); + mame_printf_info("%-20s", core_filename_extract_base(basename, name).cstr()); // see if we can find a match in the ROMs int found = find_by_hash(hashes, length); diff --git a/src/emu/debug/debugcpu.c b/src/emu/debug/debugcpu.c index cdfbf80a03f..c4a092dd160 100644 --- a/src/emu/debug/debugcpu.c +++ b/src/emu/debug/debugcpu.c @@ -51,9 +51,6 @@ #include "xmlfile.h" #include #include -#if defined(SDLMAME_FREEBSD) || defined(SDLMAME_NETBSD) || defined(SDLMAME_OS2) -# undef tolower -#endif /*************************************************************************** @@ -1712,7 +1709,7 @@ device_debug::device_debug(device_t &device) // add all registers into it astring tempstr; for (const device_state_entry *entry = m_state->state_first(); entry != NULL; entry = entry->next()) - m_symtable.add(tempstr.cpy(entry->symbol()).tolower(), (void *)(FPTR)entry->index(), get_state, set_state); + m_symtable.add(tempstr.cpy(entry->symbol()).makelower(), (void *)(FPTR)entry->index(), get_state, set_state); } // set up execution-related stuff @@ -3056,8 +3053,7 @@ UINT32 device_debug::dasm_wrapped(astring &buffer, offs_t pc) } // disassemble to our buffer - buffer.expand(200); - return disassemble(buffer.text, pc, opbuf, argbuf); + return disassemble(buffer.stringbuffer(200), pc, opbuf, argbuf); } diff --git a/src/emu/debug/express.c b/src/emu/debug/express.c index cf9fc8699e9..ac36701158a 100644 --- a/src/emu/debug/express.c +++ b/src/emu/debug/express.c @@ -955,15 +955,15 @@ void parsed_expression::parse_symbol_or_number(parse_token &token, const char *& // if we have an 0x prefix, we must be a hex value if (buffer[0] == '0' && buffer[1] == 'x') - return parse_number(token, &buffer[2], 16, expression_error::INVALID_NUMBER); + return parse_number(token, buffer.cstr() + 2, 16, expression_error::INVALID_NUMBER); // if we have a # prefix, we must be a decimal value if (buffer[0] == '#') - return parse_number(token, &buffer[1], 10, expression_error::INVALID_NUMBER); + return parse_number(token, buffer.cstr() + 1, 10, expression_error::INVALID_NUMBER); // if we have a $ prefix, we are a hex value if (buffer[0] == '$') - return parse_number(token, &buffer[1], 16, expression_error::INVALID_NUMBER); + return parse_number(token, buffer.cstr() + 1, 16, expression_error::INVALID_NUMBER); // check for a symbol match symbol_entry *symbol = m_symtable->find_deep(buffer); diff --git a/src/emu/debugint/debugint.c b/src/emu/debugint/debugint.c index 4a9f4ec326a..f18f9465b0c 100644 --- a/src/emu/debugint/debugint.c +++ b/src/emu/debugint/debugint.c @@ -285,7 +285,6 @@ static DView *dview_alloc(render_target *target, running_machine &machine, debug static void dview_free(DView *dv) { - //astring_free(dv->title); LIST_REMOVE(list, dv, DView); auto_free(dv->machine(), dv); } diff --git a/src/emu/diimage.c b/src/emu/diimage.c index 39bcc92d6f8..e9d941e0fb6 100644 --- a/src/emu/diimage.c +++ b/src/emu/diimage.c @@ -182,7 +182,7 @@ void device_image_interface::device_compute_hash(hash_collection &hashes, const image_error_t device_image_interface::set_image_filename(const char *filename) { m_image_name = filename; - zippath_parent(&m_working_directory, filename); + zippath_parent(m_working_directory, filename); m_basename.cpy(m_image_name); int loc1 = m_image_name.rchr(0,'\\'); @@ -352,7 +352,7 @@ bool device_image_interface::try_change_working_directory(const char *subdir) /* did we successfully identify the directory? */ if (success) - zippath_combine(&m_working_directory, m_working_directory, subdir); + zippath_combine(m_working_directory, m_working_directory, subdir); return success; } @@ -532,10 +532,9 @@ UINT32 device_image_interface::crc() -------------------------------------------------*/ void device_image_interface::battery_load(void *buffer, int length, int fill) { - astring *fname = astring_assemble_4(astring_alloc(), device().machine().system().name, PATH_SEPARATOR, m_basename_noext, ".nv"); + astring fname(device().machine().system().name, PATH_SEPARATOR, m_basename_noext, ".nv"); - image_battery_load_by_name(device().machine().options(), astring_c(fname), buffer, length, fill); - astring_free(fname); + image_battery_load_by_name(device().machine().options(), fname, buffer, length, fill); } /*------------------------------------------------- @@ -546,10 +545,9 @@ void device_image_interface::battery_load(void *buffer, int length, int fill) -------------------------------------------------*/ void device_image_interface::battery_save(const void *buffer, int length) { - astring *fname = astring_assemble_4(astring_alloc(), device().machine().system().name, PATH_SEPARATOR, m_basename_noext, ".nv"); + astring fname(device().machine().system().name, PATH_SEPARATOR, m_basename_noext, ".nv"); - image_battery_save_by_name(device().machine().options(), astring_c(fname), buffer, length); - astring_free(fname); + image_battery_save_by_name(device().machine().options(), fname, buffer, length); } //------------------------------------------------- @@ -605,7 +603,7 @@ image_error_t device_image_interface::load_image_by_path(UINT32 open_flags, cons astring revised_path; /* attempt to read the file */ - filerr = zippath_fopen(path, open_flags, &m_file, &revised_path); + filerr = zippath_fopen(path, open_flags, m_file, revised_path); /* did the open succeed? */ switch(filerr) diff --git a/src/emu/emucore.h b/src/emu/emucore.h index 20f392b1bb8..8ea6c5a7ccb 100644 --- a/src/emu/emucore.h +++ b/src/emu/emucore.h @@ -33,9 +33,9 @@ // core system includes #include "osdcomm.h" +#include "astring.h" #include "emualloc.h" #include "corestr.h" -#include "astring.h" #include "bitmap.h" #include "tagmap.h" diff --git a/src/emu/emuopts.c b/src/emu/emuopts.c index 871a3deba63..3d08d3db051 100644 --- a/src/emu/emuopts.c +++ b/src/emu/emuopts.c @@ -448,10 +448,10 @@ void emu_options::parse_standard_inis(astring &error_string) // next parse "source/.ini"; if that doesn't exist, try .ini astring sourcename; - core_filename_extract_base(&sourcename, cursystem->source_file, TRUE)->ins(0, "source" PATH_SEPARATOR); + core_filename_extract_base(sourcename, cursystem->source_file, true).ins(0, "source" PATH_SEPARATOR); if (!parse_one_ini(sourcename, OPTION_PRIORITY_SOURCE_INI, &error_string)) { - core_filename_extract_base(&sourcename, cursystem->source_file, TRUE); + core_filename_extract_base(sourcename, cursystem->source_file, true); parse_one_ini(sourcename, OPTION_PRIORITY_SOURCE_INI, &error_string); } @@ -474,7 +474,7 @@ void emu_options::parse_standard_inis(astring &error_string) const game_driver *emu_options::system() const { astring tempstr; - int index = driver_list::find(*core_filename_extract_base(&tempstr, system_name(), TRUE)); + int index = driver_list::find(core_filename_extract_base(tempstr, system_name(), true)); return (index != -1) ? &driver_list::driver(index) : NULL; } diff --git a/src/emu/hash.c b/src/emu/hash.c index 07a57587531..d50395b2f74 100644 --- a/src/emu/hash.c +++ b/src/emu/hash.c @@ -590,7 +590,7 @@ const char *hash_collection::macro_string(astring &buffer) const buffer.reset(); for (hash_base *hash = m_hashlist.first(); hash != NULL; hash = hash->next()) { - buffer.cat(temp.cpy(hash->name()).toupper()); + buffer.cat(temp.cpy(hash->name()).makeupper()); buffer.cat("(").cat(hash->string(temp)).cat(") "); } diff --git a/src/emu/image.c b/src/emu/image.c index 9de80eaf6af..fc8776fa218 100644 --- a/src/emu/image.c +++ b/src/emu/image.c @@ -371,17 +371,17 @@ static char *strip_extension(const char *filename) string with the image info text -------------------------------------------------*/ -astring *image_info_astring(running_machine &machine, astring *string) +astring &image_info_astring(running_machine &machine, astring &string) { device_image_interface *image = NULL; - astring_printf(string, "%s\n\n", machine.system().description); + string.printf("%s\n\n", machine.system().description); #if 0 if (mess_ram_size > 0) { char buf2[RAM_STRING_BUFLEN]; - astring_catprintf(string, "RAM: %s\n\n", ram_string(buf2, mess_ram_size)); + string.catprintf("RAM: %s\n\n", ram_string(buf2, mess_ram_size)); } #endif @@ -398,28 +398,28 @@ astring *image_info_astring(running_machine &machine, astring *string) base_filename_noextension = strip_extension(base_filename); /* display device type and filename */ - astring_catprintf(string, "%s: %s\n", image->device().name(), base_filename); + string.catprintf("%s: %s\n", image->device().name(), base_filename); /* display long filename, if present and doesn't correspond to name */ info = image->longname(); if (info && (!base_filename_noextension || mame_stricmp(info, base_filename_noextension))) - astring_catprintf(string, "%s\n", info); + string.catprintf("%s\n", info); /* display manufacturer, if available */ info = image->manufacturer(); if (info != NULL) { - astring_catprintf(string, "%s", info); + string.catprintf("%s", info); info = stripspace(image->year()); if (info && *info) - astring_catprintf(string, ", %s", info); - astring_catprintf(string,"\n"); + string.catprintf(", %s", info); + string.catprintf("\n"); } /* display supported information, if available */ switch(image->supported()) { - case SOFTWARE_SUPPORTED_NO : astring_catprintf(string, "Not supported\n"); break; - case SOFTWARE_SUPPORTED_PARTIAL : astring_catprintf(string, "Partialy supported\n"); break; + case SOFTWARE_SUPPORTED_NO : string.catprintf("Not supported\n"); break; + case SOFTWARE_SUPPORTED_PARTIAL : string.catprintf("Partialy supported\n"); break; default : break; } @@ -428,7 +428,7 @@ astring *image_info_astring(running_machine &machine, astring *string) } else { - astring_catprintf(string, "%s: ---\n", image->device().name()); + string.catprintf("%s: ---\n", image->device().name()); } } return string; diff --git a/src/emu/image.h b/src/emu/image.h index f1c963ffd3c..f194f006a0c 100644 --- a/src/emu/image.h +++ b/src/emu/image.h @@ -26,7 +26,7 @@ extern struct io_procs image_ioprocs; void image_battery_load_by_name(emu_options &options, const char *filename, void *buffer, int length, int fill); void image_battery_save_by_name(emu_options &options, const char *filename, const void *buffer, int length); -astring *image_info_astring(running_machine &machine, astring *string); +astring &image_info_astring(running_machine &machine, astring &string); device_image_interface *image_from_absolute_index(running_machine &machine, int absolute_index); diff --git a/src/emu/input.c b/src/emu/input.c index 097de87c213..a45338abb01 100644 --- a/src/emu/input.c +++ b/src/emu/input.c @@ -2179,7 +2179,7 @@ input_device_item::input_device_item(input_device &device, const char *name, voi // otherwise, create a tokenized name else - m_token.cpy(name).toupper().delchr(' ').delchr('_'); + m_token.cpy(name).makeupper().delchr(' ').delchr('_'); } diff --git a/src/emu/ioport.c b/src/emu/ioport.c index e6fcc448c34..0bda71834e1 100644 --- a/src/emu/ioport.c +++ b/src/emu/ioport.c @@ -1911,9 +1911,8 @@ static const char *inputx_key_name(unicode_char ch) a key based on natural keyboard characters -------------------------------------------------*/ -static astring *get_keyboard_key_name(const input_field_config *field) +static astring &get_keyboard_key_name(astring &name, const input_field_config *field) { - astring *result = astring_alloc(); int i; unicode_char ch; @@ -1922,17 +1921,17 @@ static astring *get_keyboard_key_name(const input_field_config *field) for (i = 0; i < ARRAY_LENGTH(field->chars) && (field->chars[i] != '\0'); i++) { ch = get_keyboard_code(field, i); - astring_printf(result, "%s%-*s ", astring_c(result), MAX(SPACE_COUNT - 1, 0), inputx_key_name(ch)); + name.catprintf("%-*s ", MAX(SPACE_COUNT - 1, 0), inputx_key_name(ch)); } /* trim extra spaces */ - astring_trimspace(result); + name.trimspace(); /* special case */ - if (astring_len(result) == 0) - astring_cpyc(result, "Unnamed Key"); + if (name.len() == 0) + name.cpy("Unnamed Key"); - return result; + return name; } /*------------------------------------------------- @@ -2036,12 +2035,8 @@ static void init_port_state(running_machine &machine) /* Name keyboard key names */ if ((field->type == IPT_KEYBOARD || field->type == IPT_KEYPAD) && (field->name == NULL)) { - astring *name = get_keyboard_key_name(field); - if (name != NULL) - { - field->state->name = auto_strdup(machine, astring_c(name)); - astring_free(name); - } + astring name; + field->state->name = auto_strdup(machine, get_keyboard_key_name(name, field)); } } } diff --git a/src/emu/softlist.c b/src/emu/softlist.c index b084c5403fb..9a483dc9788 100644 --- a/src/emu/softlist.c +++ b/src/emu/softlist.c @@ -1911,7 +1911,7 @@ void validate_softlists(emu_options &options) } /* check for duplicate descriptions */ - if (descriptions.add(astring(swinfo->longname).tolower().cstr(), swinfo, FALSE) == TMERR_DUPLICATE) + if (descriptions.add(astring(swinfo->longname).makelower().cstr(), swinfo, FALSE) == TMERR_DUPLICATE) { mame_printf_error("%s: %s is a duplicate description (%s)\n", list->file->filename(), swinfo->longname, swinfo->shortname); error = TRUE; diff --git a/src/emu/uiimage.c b/src/emu/uiimage.c index 70b93ccdd92..c36c6c98071 100644 --- a/src/emu/uiimage.c +++ b/src/emu/uiimage.c @@ -224,7 +224,7 @@ static int is_valid_filename_char(unicode_char unichar) void ui_menu_file_create::custom_render(void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2) { extra_text_render(container, top, bottom, origx1, origy1, origx2, origy2, - astring_c(manager->current_directory), + manager->current_directory, NULL); } @@ -254,8 +254,8 @@ void ui_menu_file_create::populate() /* append the "New Image Name" item */ if (get_selection() == ITEMREF_NEW_IMAGE_NAME) { - astring_assemble_2(&buffer, filename_buffer, "_"); - new_image_name = astring_c(&buffer); + buffer.cat(filename_buffer).cat("_"); + new_image_name = buffer; } else { @@ -286,17 +286,17 @@ void ui_menu_file_create::populate() int ui_menu_file_create::create_new_image(device_image_interface *image, const char *directory, const char *filename, int *yes) { - astring *path; + astring path; osd_directory_entry *entry; osd_dir_entry_type file_type; int do_create, err; int result = FALSE; /* assemble the full path */ - path = zippath_combine(astring_alloc(), directory, filename); + zippath_combine(path, directory, filename); /* does a file or a directory exist at the path */ - entry = osd_stat(astring_c(path)); + entry = osd_stat(path); file_type = (entry != NULL) ? entry->type : ENTTYPE_NONE; if (entry != NULL) free(entry); @@ -333,16 +333,13 @@ int ui_menu_file_create::create_new_image(device_image_interface *image, const c /* create the image, if appropriate */ if (do_create) { - err = image->create(astring_c(path), 0, NULL); + err = image->create(path, 0, NULL); if (err != 0) popmessage("Error: %s", image->error()); else result = TRUE; } - /* free the path */ - astring_free(path); - return result; } @@ -382,7 +379,7 @@ void ui_menu_file_create::handle() { if (create_new_image( manager->selected_device, - astring_c(manager->current_directory), + manager->current_directory, filename_buffer, &confirm_save_as_yes)) { @@ -422,7 +419,7 @@ void ui_menu_file_selector::custom_render(void *selectedref, float top, float bo { extra_text_render(container, top, bottom, origx1, origy1, origx2, origy2, - astring_c(manager->current_directory), + manager->current_directory, NULL); } @@ -506,7 +503,7 @@ ui_menu_file_selector::file_selector_entry *ui_menu_file_selector::append_entry( ui_menu_file_selector::file_selector_entry *ui_menu_file_selector::append_dirent_entry(const osd_directory_entry *dirent) { - astring *buffer; + astring buffer; file_selector_entry_type entry_type; file_selector_entry *entry; @@ -526,18 +523,17 @@ ui_menu_file_selector::file_selector_entry *ui_menu_file_selector::append_dirent } /* determine the full path */ - buffer = zippath_combine( - astring_alloc(), - astring_c(manager->current_directory), + zippath_combine( + buffer, + manager->current_directory, dirent->name); /* create the file selector entry */ entry = append_entry( entry_type, dirent->name, - astring_c(buffer)); + buffer); - astring_free(buffer); return entry; } @@ -610,7 +606,7 @@ void ui_menu_file_selector::populate() int i; const char *volume_name; device_image_interface *device = manager->selected_device; - const char *path = astring_c(manager->current_directory); + const char *path = manager->current_directory; /* open the directory */ err = zippath_opendir(path, &directory); @@ -654,7 +650,7 @@ void ui_menu_file_selector::populate() selected_entry = entry; /* do we have to select this file? */ - if (!mame_stricmp(astring_c(manager->current_file), dirent->name)) + if (!mame_stricmp(manager->current_file, dirent->name)) selected_entry = entry; } } @@ -731,7 +727,7 @@ void ui_menu_file_selector::handle() ui_popup_time(1, "Error accessing %s", entry->fullpath); break; } - astring_cpyc(manager->current_directory, entry->fullpath); + manager->current_directory.cpy(entry->fullpath); reset((ui_menu_reset_options)0); break; @@ -845,10 +841,9 @@ void ui_menu_file_manager::fix_working_directory(device_image_interface *image) /* if the image exists, set the working directory to the parent directory */ if (image->exists()) { - astring *astr = astring_alloc(); + astring astr; zippath_parent(astr, image->filename()); - image->set_working_directory(astring_c(astr)); - astring_free(astr); + image->set_working_directory(astr); } /* check to see if the path exists; if not clear it */ @@ -882,8 +877,6 @@ void ui_menu_file_manager::custom_render(void *selectedref, float top, float bot ui_menu_file_manager::ui_menu_file_manager(running_machine &machine, render_container *container) : ui_menu(machine, container) { - current_directory = astring_alloc(); - current_file = astring_alloc(); } void ui_menu_file_manager::populate() @@ -941,8 +934,6 @@ void ui_menu_file_manager::populate() ui_menu_file_manager::~ui_menu_file_manager() { - astring_free(current_directory); - astring_free(current_file); } @@ -967,8 +958,8 @@ void ui_menu_file_manager::handle() fix_working_directory(selected_device); /* set up current_directory and current_file - depends on whether we have an image */ - astring_cpyc(current_directory, selected_device->working_directory()); - astring_cpyc(current_file, selected_device->exists() ? selected_device->basename() : ""); + current_directory.cpy(selected_device->working_directory()); + current_file.cpy(selected_device->exists() ? selected_device->basename() : ""); /* reset the existing menu */ reset(UI_MENU_RESET_REMEMBER_POSITION); @@ -990,9 +981,8 @@ ui_menu_image_info::ui_menu_image_info(running_machine &machine, render_containe void ui_menu_image_info::populate() { - astring *tempstring = image_info_astring(machine(), astring_alloc()); - item_append(astring_c(tempstring), NULL, MENU_FLAG_MULTILINE, NULL); - astring_free(tempstring); + astring tempstring; + item_append(image_info_astring(machine(), tempstring), NULL, MENU_FLAG_MULTILINE, NULL); } ui_menu_image_info::~ui_menu_image_info() @@ -1084,7 +1074,7 @@ int ui_menu_mess_bitbanger_control::bitbanger_count() representation of the time -------------------------------------------------*/ -astring *tapecontrol_gettime(astring *dest, cassette_image_device *cassette, int *curpos, int *endpos) +astring &tapecontrol_gettime(astring &dest, cassette_image_device *cassette, int *curpos, int *endpos) { double t0, t1; @@ -1092,9 +1082,9 @@ astring *tapecontrol_gettime(astring *dest, cassette_image_device *cassette, int t1 = cassette->get_length(); if (t1) - astring_printf(dest, "%04d/%04d", (int) t0, (int) t1); + dest.printf("%04d/%04d", (int) t0, (int) t1); else - astring_printf(dest, "%04d/%04d", 0, (int) t1); + dest.printf("%04d/%04d", 0, (int) t1); if (curpos != NULL) *curpos = t0; @@ -1147,7 +1137,7 @@ void ui_menu_mess_tape_control::populate() item_append(device->device().name(), device->filename(), flags, TAPECMD_SELECT); /* state */ - tapecontrol_gettime(&timepos, cassette, NULL, NULL); + tapecontrol_gettime(timepos, cassette, NULL, NULL); state = cassette->get_state(); item_append( (state & CASSETTE_MASK_UISTATE) == CASSETTE_STOPPED @@ -1156,7 +1146,7 @@ void ui_menu_mess_tape_control::populate() ? ((state & CASSETTE_MASK_MOTOR) == CASSETTE_MOTOR_ENABLED ? "playing" : "(playing)") : ((state & CASSETTE_MASK_MOTOR) == CASSETTE_MOTOR_ENABLED ? "recording" : "(recording)") ), - astring_c(&timepos), + timepos, tapeflags, TAPECMD_SLIDER); diff --git a/src/emu/uiimage.h b/src/emu/uiimage.h index 1687500ee66..aaf11b101ff 100644 --- a/src/emu/uiimage.h +++ b/src/emu/uiimage.h @@ -24,8 +24,8 @@ public: class ui_menu_file_manager : public ui_menu { public: - astring *current_directory; - astring *current_file; + astring current_directory; + astring current_file; device_image_interface *selected_device; ui_menu_file_manager(running_machine &machine, render_container *container); diff --git a/src/lib/util/astring.c b/src/lib/util/astring.c index 9a2507ca313..984848f152f 100644 --- a/src/lib/util/astring.c +++ b/src/lib/util/astring.c @@ -44,202 +44,107 @@ #include #include #include - +#include /*************************************************************************** GLOBAL VARIABLES ***************************************************************************/ -static const char dummy_text[2] = { 0 }; - -#ifdef __cplusplus static const astring dummy_astring; -#else -static const astring dummy_astring = { (char *)dummy_text, 1, { 0 } }; -#endif -/*************************************************************************** - INLINE FUNCTIONS -***************************************************************************/ +//************************************************************************** +// INLINE FUNCTIONS +//************************************************************************** -/*------------------------------------------------- - ensure_room - ensure we have room for a - given string, or else reallocate our buffer --------------------------------------------------*/ +//------------------------------------------------- +// ensure_room - ensure we have room for a +// given string, or else reallocate our buffer +//------------------------------------------------- -INLINE int ensure_room(astring *str, int length) +inline bool astring::ensure_room(int length) { - char *newbuf, *oldbuf; - int alloclen; + // always fail to expand the dummy + if (this == &dummy_astring) + return false; - /* always fail to expand the dummy */ - if (str == &dummy_astring) - return FALSE; + // if we have the room, do nothing + if (m_alloclen >= length + 1) + return true; - /* if we have the room, do nothing */ - if (str->alloclen >= length + 1) - return TRUE; + // allocate a new buffer with some slop + int alloclen = length + 256; + char *newbuf = new char[alloclen]; - /* allocate a new buffer with some slop */ - alloclen = length + 256; - newbuf = (char *)malloc(alloclen); - if (newbuf == NULL) - return FALSE; + // swap in the new buffer and free the old one + char *oldbuf = (m_text == m_smallbuf) ? NULL : m_text; + m_text = strcpy(newbuf, m_text); + m_alloclen = alloclen; + delete[] oldbuf; - /* swap in the new buffer and free the old one */ - oldbuf = (str->text == str->smallbuf) ? NULL : str->text; - str->text = strcpy(newbuf, str->text); - str->alloclen = alloclen; - if (oldbuf != NULL) - free(oldbuf); - - return TRUE; + return true; } -/*------------------------------------------------- - safe_string_base - return a "safe" string - base for a given start index --------------------------------------------------*/ +//------------------------------------------------- +// safe_string_base - return a "safe" string +// base for a given start index +//------------------------------------------------- -INLINE char *safe_string_base(char *base, int start) +inline char *astring::safe_string_base(int start) const { - int max = strlen(base); - return (start >= 0 && start < max) ? base + start : base + max; + int max = len(); + return (start >= 0 && start < max) ? m_text + start : m_text + max; } -/*------------------------------------------------- - normalize_substr - normalize substr parameters --------------------------------------------------*/ +//------------------------------------------------- +// normalize_substr - normalize substr parameters +//------------------------------------------------- -INLINE void normalize_substr(int *start, int *count, int length) +inline void astring::normalize_substr(int &start, int &count, int length) const { - /* limit start */ - if (*start < 0) - *start = 0; - else if (*start > length) - *start = length; + // limit start + if (start < 0) + start = 0; + else if (start > length) + start = length; - /* update count */ - if (*count == -1 || *start + *count > length) - *count = length - *start; + // update count + if (count == -1 || start + count > length) + count = length - start; } -/*************************************************************************** - ASTRING ALLOCATION -***************************************************************************/ +//************************************************************************** +// ASTRING ALLOCATION +//************************************************************************** -#ifdef __cplusplus - -#include - -/*------------------------------------------------- - init - constructor helper --------------------------------------------------*/ +//------------------------------------------------- +// init - constructor helper +//------------------------------------------------- astring &astring::init() { - text = smallbuf; - alloclen = ARRAY_LENGTH(smallbuf); - smallbuf[0] = 0; + // initialize ourselves to point to the internal buffer + m_text = m_smallbuf; + m_alloclen = ARRAY_LENGTH(m_smallbuf); + m_smallbuf[0] = 0; return *this; } -/*------------------------------------------------- - ~astring - basic destructor --------------------------------------------------*/ +//------------------------------------------------- +// ~astring - destructor +//------------------------------------------------- astring::~astring() { - if (text != smallbuf) - free(text); -} - - -/*------------------------------------------------- - astring_alloc - allocate a new astring --------------------------------------------------*/ - -astring *astring_alloc(void) -{ - // we do our own malloc and use placement new here to - // make our memory tracking happy - void *ptr = malloc(sizeof(astring)); - if (ptr == NULL) - return NULL; - return new(ptr) astring; -} - - -/*------------------------------------------------- - astring_free - free an astring --------------------------------------------------*/ - -void astring_free(astring *str) -{ - // since we malloc'ed the memory ourselves, we directly - // call the destructor and free the memory ourselves - str->~astring(); - free(str); -} - -#else - -/*------------------------------------------------- - astring_alloc - allocate a new astring --------------------------------------------------*/ - -astring *astring_alloc(void) -{ - astring *str; - - /* allocate memory; if we fail, return the dummy */ - str = (astring *)malloc(sizeof(*str)); - if (str == NULL) - return (astring *)&dummy_astring; - memset(str, 0, sizeof(*str)); - - /* initialize the small buffer */ - str->text = str->smallbuf; - str->alloclen = ARRAY_LENGTH(str->smallbuf); - return str; -} - - -/*------------------------------------------------- - astring_free - free an astring --------------------------------------------------*/ - -void astring_free(astring *str) -{ - /* ignore attempts to free the dummy */ - if (str == &dummy_astring) - return; - - /* if we allocated additional memory, free that */ - if (str->text != str->smallbuf) - free(str->text); - free(str); -} - -#endif - - -/*------------------------------------------------- - astring_expand - expand an astring to - guarantee the given amount of space --------------------------------------------------*/ - -void astring_expand(astring *str, int length) -{ - ensure_room(str, length); + if (m_text != m_smallbuf) + delete[] m_text; } @@ -248,238 +153,146 @@ void astring_expand(astring *str, int length) INLINE ASTRING CHANGES ***************************************************************************/ -/*------------------------------------------------- - astring_cpy - copy one astring into another --------------------------------------------------*/ +//------------------------------------------------- +// cpy - copy a character array into an astring +//------------------------------------------------- -astring *astring_cpy(astring *dst, const astring *src) +astring &astring::cpy(const char *src, int count) { - return astring_cpyc(dst, src->text); + // make room; if we fail or if we are the dummy, do nothing + if (!ensure_room(count)) + return *this; + + // copy the raw data and NULL-terminate + if (count > 0 && m_text != src) + memcpy(m_text, src, count); + m_text[count] = 0; + return *this; } -/*------------------------------------------------- - astring_cpyc - copy a C string into an astring --------------------------------------------------*/ +//------------------------------------------------- +// cpysubstr - copy a substring of one string to +// another +//------------------------------------------------- -astring *astring_cpyc(astring *dst, const char *src) +astring &astring::cpysubstr(const astring &src, int start, int count) { - return astring_cpych(dst, src, strlen(src)); + normalize_substr(start, count, strlen(src.m_text)); + return cpy(src.m_text + start, count); } -/*------------------------------------------------- - astring_cpych - copy a character array into - an astring --------------------------------------------------*/ +//------------------------------------------------- +// ins - insert a character array into an astring +//------------------------------------------------- -astring *astring_cpych(astring *dst, const char *src, int count) +astring &astring::ins(int insbefore, const char *src, int count) { - /* make room; if we fail or if dst is the dummy, do nothing */ - if (!ensure_room(dst, count)) - return dst; + // make room; if we fail or if we are the dummy, do nothing + int dstlength = len(); + if (!ensure_room(dstlength + count)) + return *this; - /* copy the raw data and NULL-terminate */ - if ((count > 0) && (dst->text != src)) - memcpy(dst->text, src, count); - dst->text[count] = 0; - return dst; -} - - -/*------------------------------------------------- - astring_cpysubstr - copy a substring of one - string to another --------------------------------------------------*/ - -astring *astring_cpysubstr(astring *dst, const astring *src, int start, int count) -{ - normalize_substr(&start, &count, strlen(src->text)); - return astring_cpych(dst, src->text + start, count); -} - - -/*------------------------------------------------- - astring_ins - insert one astring into another --------------------------------------------------*/ - -astring *astring_ins(astring *dst, int insbefore, const astring *src) -{ - return astring_insc(dst, insbefore, src->text); -} - - -/*------------------------------------------------- - astring_insc - insert a C string into an - astring --------------------------------------------------*/ - -astring *astring_insc(astring *dst, int insbefore, const char *src) -{ - return astring_insch(dst, insbefore, src, strlen(src)); -} - - -/*------------------------------------------------- - astring_insch - insert a character array - into an astring --------------------------------------------------*/ - -astring *astring_insch(astring *dst, int insbefore, const char *src, int count) -{ - int dstlength = strlen(dst->text); - - /* make room; if we fail or if dst is the dummy, do nothing */ - if (!ensure_room(dst, dstlength + count)) - return dst; - - /* adjust insbefore to be logical */ + // adjust insbefore to be logical if (insbefore < 0 || insbefore > dstlength) insbefore = dstlength; - /* copy the data an NULL-terminate */ + // copy the data an NULL-terminate if (insbefore < dstlength) - memmove(dst->text + insbefore + count, dst->text + insbefore, dstlength - insbefore); - memcpy(dst->text + insbefore, src, count); - dst->text[dstlength + count] = 0; - return dst; + memmove(m_text + insbefore + count, m_text + insbefore, dstlength - insbefore); + memcpy(m_text + insbefore, src, count); + m_text[dstlength + count] = 0; + return *this; } -/*------------------------------------------------- - astring_inssubstr - insert a substring of - one string to another --------------------------------------------------*/ +//------------------------------------------------- +// inssubstr - insert a substring of one string +// into another +//------------------------------------------------- -astring *astring_inssubstr(astring *dst, int insbefore, const astring *src, int start, int count) +astring &astring::inssubstr(int insbefore, const astring &src, int start, int count) { - normalize_substr(&start, &count, strlen(src->text)); - return astring_insch(dst, insbefore, src->text + start, count); + normalize_substr(start, count, strlen(src.m_text)); + return ins(insbefore, src.m_text + start, count); } -/*------------------------------------------------- - astring_substr - extract a substring of - ourself, removing everything else --------------------------------------------------*/ +//------------------------------------------------- +// substr - extract a substring of ourself, +// removing everything else +//------------------------------------------------- -astring *astring_substr(astring *str, int start, int count) +astring &astring::substr(int start, int count) { - /* ignore attempts to do this on the dummy */ - if (str == &dummy_astring) - return str; + // ignore attempts to do this on the dummy + if (this == &dummy_astring) + return *this; - /* normalize parameters */ - normalize_substr(&start, &count, strlen(str->text)); + // normalize parameters + normalize_substr(start, count, strlen(m_text)); - /* move the data and NULL-terminate */ + // move the data and NULL-terminate if (count > 0 && start > 0) - memmove(str->text, str->text + start, count); - str->text[count] = 0; - return str; + memmove(m_text, m_text + start, count); + m_text[count] = 0; + return *this; } -/*------------------------------------------------- - astring_del - delete a substring of - ourself, keeping everything else --------------------------------------------------*/ +//------------------------------------------------- +// del - delete a substring of ourself, keeping +// everything else +//------------------------------------------------- -astring *astring_del(astring *str, int start, int count) +astring &astring::del(int start, int count) { - int strlength = strlen(str->text); + // ignore attempts to do this on the dummy + if (this == &dummy_astring) + return *this; - /* ignore attempts to do this on the dummy */ - if (str == &dummy_astring) - return str; + // normalize parameters + int strlength = strlen(m_text); + normalize_substr(start, count, strlength); - /* normalize parameters */ - normalize_substr(&start, &count, strlength); - - /* move the data and NULL-terminate */ + // move the data and NULL-terminate if (count > 0) - memmove(str->text + start, str->text + start + count, strlength - (start + count)); - str->text[strlength - count] = 0; - return str; + memmove(m_text + start, m_text + start + count, strlength - (start + count)); + m_text[strlength - count] = 0; + return *this; } -/*------------------------------------------------- - astring_printf - printf text into an astring --------------------------------------------------*/ +//------------------------------------------------- +// vprintf - vprintf text into an astring +//-------------------------------------------------*/ -int astring_printf(astring *dst, const char *format, ...) +int astring::vprintf(const char *format, va_list args) { + // sprintf into the temporary buffer char tempbuf[4096]; - va_list args; - int result; + int result = vsprintf(tempbuf, format, args); - /* sprintf into the temporary buffer */ - va_start(args, format); - result = vsprintf(tempbuf, format, args); - va_end(args); - - /* set the result */ - astring_cpyc(dst, tempbuf); + // set the result + cpy(tempbuf); return result; } -/*------------------------------------------------- - astring_vprintf - vprintf text into an astring --------------------------------------------------*/ +//------------------------------------------------- +// catprintf - formatted vprintf to the end of +// an astring +//------------------------------------------------- -int astring_vprintf(astring *dst, const char *format, va_list args) +int astring::catvprintf(const char *format, va_list args) { + // sprintf into the temporary buffer char tempbuf[4096]; - int result; + int result = vsprintf(tempbuf, format, args); - /* sprintf into the temporary buffer */ - result = vsprintf(tempbuf, format, args); - - /* set the result */ - astring_cpyc(dst, tempbuf); - return result; -} - - -/*------------------------------------------------- - astring_catprintf - formatted printf to - the end of an astring --------------------------------------------------*/ - -int astring_catprintf(astring *dst, const char *format, ...) -{ - char tempbuf[4096]; - va_list args; - int result; - - /* sprintf into the temporary buffer */ - va_start(args, format); - result = vsprintf(tempbuf, format, args); - va_end(args); - - /* append the result */ - astring_catc(dst, tempbuf); - return result; -} - - -/*------------------------------------------------- - astring_catprintf - formatted vprintf to - the end of an astring --------------------------------------------------*/ - -int astring_catvprintf(astring *dst, const char *format, va_list args) -{ - char tempbuf[4096]; - int result; - - /* sprintf into the temporary buffer */ - result = vsprintf(tempbuf, format, args); - - /* append the result */ - astring_catc(dst, tempbuf); + // append the result + cat(tempbuf); return result; } @@ -489,316 +302,203 @@ int astring_catvprintf(astring *dst, const char *format, va_list args) ASTRING QUERIES ***************************************************************************/ -/*------------------------------------------------- - astring_c - return a pointer to a C string - from an astring --------------------------------------------------*/ +//------------------------------------------------- +// cmp - compare a character array to an astring +//------------------------------------------------- -const char *astring_c(const astring *str) +int astring::cmp(const char *str2, int count) const { - return str->text; + // loop while equal until we hit the end of strings + int index; + for (index = 0; index < count; index++) + if (m_text[index] == 0 || m_text[index] != str2[index]) + break; + + // determine the final result + if (index < count) + return m_text[index] - str2[index]; + if (m_text[index] == 0) + return 0; + return 1; } -/*------------------------------------------------- - astring_len - return the length of an astring --------------------------------------------------*/ +//------------------------------------------------- +// cmpsubstr - compare a substring to an astring +//------------------------------------------------- -int astring_len(const astring *str) +int astring::cmpsubstr(const astring &str2, int start, int count) const { - return strlen(str->text); + normalize_substr(start, count, strlen(str2.m_text)); + return cmp(str2.m_text + start, count); } -/*------------------------------------------------- - astring_cmp - compare one astring to another --------------------------------------------------*/ +//------------------------------------------------- +// icmp - compare a character array to an astring +//------------------------------------------------- -int astring_cmp(const astring *str1, const astring *str2) +int astring::icmp(const char *str2, int count) const { - return astring_cmpc(str1, str2->text); + // loop while equal until we hit the end of strings + int index; + for (index = 0; index < count; index++) + if (m_text[index] == 0 || tolower(m_text[index]) != tolower(str2[index])) + break; + + // determine the final result + if (index < count) + return tolower(m_text[index]) - tolower(str2[index]); + if (m_text[index] == 0) + return 0; + return 1; } -/*------------------------------------------------- - astring_cmpc - compare a C string to an astring --------------------------------------------------*/ +//------------------------------------------------- +// icmpsubstr - compare a substring to an astring +//------------------------------------------------- -int astring_cmpc(const astring *str1, const char *str2) +int astring::icmpsubstr(const astring &str2, int start, int count) const { - const char *s1 = str1->text; - - /* loop while equal until we hit the end of strings */ - while (*s1 != 0 && *str2 != 0 && *s1 == *str2) - s1++, str2++; - return *s1 - *str2; + normalize_substr(start, count, strlen(str2.m_text)); + return icmp(str2.m_text + start, count); } -/*------------------------------------------------- - astring_cmpch - compare a character array to - an astring --------------------------------------------------*/ +//------------------------------------------------- +// chr - return the index of a character in an +// astring +//------------------------------------------------- -int astring_cmpch(const astring *str1, const char *str2, int count) +int astring::chr(int start, int ch) const { - const char *s1 = str1->text; - int result; - - /* loop while equal until we hit the end of strings */ - while (count-- > 0 && *s1 != 0 && *str2 != 0 && *s1 == *str2) - s1++, str2++; - result = (count == -1) ? 0 : *s1 - *str2; - if (result == 0 && *s1 != 0) - result = 1; - return result; + char *result = strchr(safe_string_base(start), ch); + return (result != NULL) ? (result - m_text) : -1; } -/*------------------------------------------------- - astring_cmpsubstr - compare a substring to - an astring --------------------------------------------------*/ +//------------------------------------------------- +// rchr - return the index of a character in an +// astring, searching from the end +//------------------------------------------------- -int astring_cmpsubstr(const astring *str1, const astring *str2, int start, int count) +int astring::rchr(int start, int ch) const { - normalize_substr(&start, &count, strlen(str2->text)); - return astring_cmpch(str1, str2->text + start, count); + char *result = strrchr(safe_string_base(start), ch); + return (result != NULL) ? (result - m_text) : -1; } -/*------------------------------------------------- - astring_icmp - case-insenstive compare one - astring to another --------------------------------------------------*/ +//------------------------------------------------- +// find - find a C string in an astring +//-------------------------------------------------*/ -int astring_icmp(const astring *str1, const astring *str2) +int astring::find(int start, const char *search) const { - return astring_icmpc(str1, str2->text); + char *result = strstr(safe_string_base(start), search); + return (result != NULL) ? (result - m_text) : -1; } -/*------------------------------------------------- - astring_icmpc - case-insenstive compare a C - string to an astring --------------------------------------------------*/ +//------------------------------------------------- +// replacec - search in an astring for a C string, +// replacing all instances with another C string +// and returning the number of matches +//------------------------------------------------- -int astring_icmpc(const astring *str1, const char *str2) -{ - const char *s1 = str1->text; - - /* loop while equal until we hit the end of strings */ - while (*s1 != 0 && *str2 != 0 && tolower((UINT8)*s1) == tolower((UINT8)*str2)) - s1++, str2++; - return tolower((UINT8)*s1) - tolower((UINT8)*str2); -} - - -/*------------------------------------------------- - astring_icmpch - case-insenstive compare a - character array to an astring --------------------------------------------------*/ - -int astring_icmpch(const astring *str1, const char *str2, int count) -{ - const char *s1 = str1->text; - int result; - - /* loop while equal until we hit the end of strings */ - while (count-- > 0 && *s1 != 0 && *str2 != 0 && tolower((UINT8)*s1) == tolower((UINT8)*str2)) - s1++, str2++; - result = (count == -1) ? 0 : tolower((UINT8)*s1) - tolower((UINT8)*str2); - if (result == 0 && *s1 != 0) - result = 1; - return result; -} - - -/*------------------------------------------------- - astring_icmpsubstr - case-insenstive compare a - substring to an astring --------------------------------------------------*/ - -int astring_icmpsubstr(const astring *str1, const astring *str2, int start, int count) -{ - normalize_substr(&start, &count, strlen(str2->text)); - return astring_icmpch(str1, str2->text + start, count); -} - - -/*------------------------------------------------- - astring_chr - return the index of a character - in an astring --------------------------------------------------*/ - -int astring_chr(const astring *str, int start, int ch) -{ - char *result = strchr(safe_string_base(str->text, start), ch); - return (result != NULL) ? (result - str->text) : -1; -} - - -/*------------------------------------------------- - astring_rchr - return the index of a character - in an astring, searching from the end --------------------------------------------------*/ - -int astring_rchr(const astring *str, int start, int ch) -{ - char *result = strrchr(safe_string_base(str->text, start), ch); - return (result != NULL) ? (result - str->text) : -1; -} - - -/*------------------------------------------------- - astring_find - find one astring in another --------------------------------------------------*/ - -int astring_find(const astring *str, int start, const astring *search) -{ - return astring_findc(str, start, search->text); -} - - -/*------------------------------------------------- - astring_findc - find a C string in an astring --------------------------------------------------*/ - -int astring_findc(const astring *str, int start, const char *search) -{ - char *result = strstr(safe_string_base(str->text, start), search); - return (result != NULL) ? (result - str->text) : -1; -} - - -/*------------------------------------------------- - astring_replace - search in an astring for - another astring, replacing all instances with - a third and returning the number of matches --------------------------------------------------*/ - -int astring_replace(astring *str, int start, const astring *search, const astring *replace) -{ - return astring_replacec(str, start, search->text, replace->text); -} - - -/*------------------------------------------------- - astring_replacec - search in an astring for a - C string, replacing all instances with another - C string and returning the number of matches --------------------------------------------------*/ - -int astring_replacec(astring *str, int start, const char *search, const char *replace) +int astring::replace(int start, const char *search, const char *replace) { int searchlen = strlen(search); int replacelen = strlen(replace); int matches = 0; - int curindex; - for (curindex = astring_findc(str, start, search); curindex != -1; curindex = astring_findc(str, curindex + replacelen, search)) + for (int curindex = find(start, search); curindex != -1; curindex = find(curindex + replacelen, search)) { matches++; - astring_del(str, curindex, searchlen); - astring_insc(str, curindex, replace); + del(curindex, searchlen).ins(curindex, replace); } return matches; } -/*************************************************************************** - ASTRING UTILITIES -***************************************************************************/ +//************************************************************************** +// ASTRING UTILITIES +//************************************************************************** -/*------------------------------------------------- - astring_delchr - delete all instances of - 'ch' --------------------------------------------------*/ +//------------------------------------------------- +// delchr - delete all instances of 'ch' +//------------------------------------------------- -astring *astring_delchr(astring *str, int ch) +astring &astring::delchr(int ch) { - char *src, *dst; - - /* simple deletion */ - for (src = dst = str->text; *src != 0; src++) + // simple deletion + char *dst = m_text; + for (char *src = m_text; *src != 0; src++) if (*src != ch) *dst++ = *src; *dst = 0; - - return str; + return *this; } -/*------------------------------------------------- - astring_replacechr - replace all instances of - 'ch' with 'newch' --------------------------------------------------*/ +//------------------------------------------------- +// replacechr - replace all instances of 'ch' +// with 'newch' +//------------------------------------------------- -astring *astring_replacechr(astring *str, int ch, int newch) +astring &astring::replacechr(int ch, int newch) { - char *text; - - /* simple replacement */ - for (text = str->text; *text != 0; text++) + // simple replacement + for (char *text = m_text; *text != 0; text++) if (*text == ch) *text = newch; - - return str; + return *this; } -/*------------------------------------------------- - astring_toupper - convert string to all - upper-case --------------------------------------------------*/ +//------------------------------------------------- +// makeupper - convert string to all upper-case +//------------------------------------------------- -astring *astring_toupper(astring *str) +astring &astring::makeupper() { - char *text; - - /* just toupper() on all characters */ - for (text = str->text; *text != 0; text++) + // just makeupper() on all characters + for (char *text = m_text; *text != 0; text++) *text = toupper((UINT8)*text); - - return str; + return *this; } -/*------------------------------------------------- - astring_tolower - convert string to all - lower-case --------------------------------------------------*/ +//------------------------------------------------- +// makelower - convert string to all lower-case +//------------------------------------------------- -astring *astring_tolower(astring *str) +astring &astring::makelower() { - char *text; - - /* just tolower() on all characters */ - for (text = str->text; *text != 0; text++) + // just tolower() on all characters + for (char *text = m_text; *text != 0; text++) *text = tolower((UINT8)*text); - - return str; + return *this; } -/*------------------------------------------------- - astring_trimspace - remove all space - characters from beginning/end --------------------------------------------------*/ +//------------------------------------------------- +// trimspace - remove all space characters from +// beginning/end +//------------------------------------------------- -astring *astring_trimspace(astring *str) +astring &astring::trimspace() { - char *ptr; - - /* first remove stuff from the end */ - for (ptr = str->text + strlen(str->text) - 1; ptr >= str->text && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr--) + // first remove stuff from the end + for (char *ptr = m_text + strlen(m_text) - 1; ptr >= m_text && (!(*ptr & 0x80) && isspace(UINT8(*ptr))); ptr--) *ptr = 0; - /* then count how much to remove from the beginning */ - for (ptr = str->text; *ptr != 0 && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr++) ; - if (ptr > str->text) - astring_substr(str, ptr - str->text, -1); - - return str; + // then count how much to remove from the beginning + char *ptr; + for (ptr = m_text; *ptr != 0 && (!(*ptr & 0x80) && isspace(UINT8(*ptr))); ptr++) ; + if (ptr > m_text) + substr(ptr - m_text); + return *this; } diff --git a/src/lib/util/astring.h b/src/lib/util/astring.h index 54b437c6186..dfac762f065 100644 --- a/src/lib/util/astring.h +++ b/src/lib/util/astring.h @@ -46,278 +46,21 @@ #include #include "osdcomm.h" -#ifdef toupper -#undef toupper -#endif -#ifdef tolower -#undef tolower -#endif -/*************************************************************************** - TYPE DEFINITIONS -***************************************************************************/ -/* base astring structure */ -typedef struct _astring_base astring_base; -struct _astring_base +//************************************************************************** +// TYPE DEFINITIONS +//************************************************************************** + +// derived class for C++ +class astring { - char * text; - int alloclen; - char smallbuf[64 - sizeof(int) - sizeof(char *)]; -}; - - -/* class for C++, direct map for C */ -#ifdef __cplusplus -class astring; -#else -typedef astring_base astring; -#endif - - - -/*************************************************************************** - FUNCTION PROTOTYPES -***************************************************************************/ - - -/* ----- astring allocation ----- */ - -/* allocate a new astring */ -astring *astring_alloc(void); - -/* free an astring */ -void astring_free(astring *str); - -/* free an astring */ -void astring_expand(astring *str, int length); - - - -/* ----- inline astring changes ----- */ - -/* copy one astring into another */ -astring *astring_cpy(astring *dst, const astring *src); - -/* copy a C string into an astring */ -astring *astring_cpyc(astring *dst, const char *src); - -/* copy a character array into an astring */ -astring *astring_cpych(astring *dst, const char *src, int count); - -/* copy a substring of one string into another */ -astring *astring_cpysubstr(astring *dst, const astring *src, int start, int count); - -/* insert one astring into another */ -astring *astring_ins(astring *dst, int insbefore, const astring *src); - -/* insert a C string into an astring */ -astring *astring_insc(astring *dst, int insbefore, const char *src); - -/* insert a character array into an astring */ -astring *astring_insch(astring *dst, int insbefore, const char *src, int count); - -/* insert a substring of one string into another */ -astring *astring_inssubstr(astring *dst, int insbefore, const astring *src, int start, int count); - -/* extract a substring of ourself, removing everything else */ -astring *astring_substr(astring *str, int start, int count); - -/* delete a substring from ourself, keeping everything else */ -astring *astring_del(astring *str, int start, int count); - -/* formatted printf to an astring */ -int astring_printf(astring *dst, const char *format, ...) ATTR_PRINTF(2,3); - -/* formatted vprintf to an astring */ -int astring_vprintf(astring *dst, const char *format, va_list args); - -/* formatted printf to the end of an astring */ -int astring_catprintf(astring *dst, const char *format, ...) ATTR_PRINTF(2,3); - -/* formatted vprintf to the end of an astring */ -int astring_catvprintf(astring *dst, const char *format, va_list args); - - - -/* ----- astring queries ----- */ - -/* return a pointer to a C string from an astring */ -const char *astring_c(const astring *str); - -/* return the length of an astring */ -int astring_len(const astring *str); - -/* compare two astrings */ -int astring_cmp(const astring *str1, const astring *str2); - -/* compare an astring to a C string */ -int astring_cmpc(const astring *str1, const char *str2); - -/* compare an astring to a character buffer */ -int astring_cmpch(const astring *str1, const char *str2, int count); - -/* compare an astring to a character buffer */ -int astring_cmpsubstr(const astring *str1, const astring *str2, int start, int count); - -/* case-insenstive compare two astrings */ -int astring_icmp(const astring *str1, const astring *str2); - -/* case-insenstive compare an astring to a C string */ -int astring_icmpc(const astring *str1, const char *str2); - -/* case-insenstive compare an astring to a character buffer */ -int astring_icmpch(const astring *str1, const char *str2, int count); - -/* case-insenstive compare an astring to a character buffer */ -int astring_icmpsubstr(const astring *str1, const astring *str2, int start, int count); - -/* search an astring for a character, returning offset or -1 if not found */ -int astring_chr(const astring *str, int start, int ch); - -/* reverse search an astring for a character, returning offset or -1 if not found */ -int astring_rchr(const astring *str, int start, int ch); - -/* search in an astring for another astring, returning offset or -1 if not found */ -int astring_find(const astring *str, int start, const astring *search); - -/* search in an astring for a C string, returning offset or -1 if not found */ -int astring_findc(const astring *str, int start, const char *search); - -/* search in an astring for another astring, replacing all instances with a third and returning the number of matches */ -int astring_replace(astring *str, int start, const astring *search, const astring *replace); - -/* search in an astring for a C string, replacing all instances with another C string and returning the number of matches */ -int astring_replacec(astring *str, int start, const char *search, const char *replace); - - - -/* ----- astring utilties ----- */ - -/* delete all instances of 'ch' */ -astring *astring_delchr(astring *str, int ch); - -/* replace all instances of 'ch' with 'newch' */ -astring *astring_replacechr(astring *str, int ch, int newch); - -/* convert string to all upper-case */ -astring *astring_toupper(astring *str); - -/* convert string to all lower-case */ -astring *astring_tolower(astring *str); - -/* remove all space characters from beginning/end */ -astring *astring_trimspace(astring *str); - - - -/*************************************************************************** - INLINE FUNCTIONS -***************************************************************************/ - -/* allocate a new duplicate of an astring */ -INLINE astring *astring_dup(const astring *str) -{ - return astring_cpy(astring_alloc(), str); -} - -/* allocate a new duplicate of an astring */ -INLINE astring *astring_dupc(const char *str) -{ - return astring_cpyc(astring_alloc(), str); -} - -/* allocate a new duplicate of an astring */ -INLINE astring *astring_dupch(const char *str, int count) -{ - return astring_cpych(astring_alloc(), str, count); -} - -/* allocate a duplicate of a substring */ -INLINE astring *astring_dupsubstr(const astring *str, int start, int count) -{ - return astring_cpysubstr(astring_alloc(), str, start, count); -} - - -/* reset an astring to an empty string */ -INLINE astring *astring_reset(astring *dst) -{ - return astring_cpyc(dst, ""); -} - - -/* concatenate one astring to the end of another */ -INLINE astring *astring_cat(astring *dst, const astring *src) -{ - return astring_ins(dst, -1, src); -} - -/* concatenate a C string to the end of an astring */ -INLINE astring *astring_catc(astring *dst, const char *src) -{ - return astring_insc(dst, -1, src); -} - -/* concatenate a character array to the end of an astring */ -INLINE astring *astring_catch(astring *dst, const char *src, int count) -{ - return astring_insch(dst, -1, src, count); -} - -/* concatenate a substring of one string into another */ -INLINE astring *astring_catsubstr(astring *dst, const astring *src, int start, int count) -{ - return astring_inssubstr(dst, -1, src, start, count); -} - - -/* assemble an astring from 2 C strings */ -INLINE astring *astring_assemble_2(astring *dst, const char *src1, const char *src2) -{ - return astring_catc(astring_cpyc(dst, src1), src2); -} - -/* assemble an astring from 3 C strings */ -INLINE astring *astring_assemble_3(astring *dst, const char *src1, const char *src2, const char *src3) -{ - return astring_catc(astring_assemble_2(dst, src1, src2), src3); -} - -/* assemble an astring from 4 C strings */ -INLINE astring *astring_assemble_4(astring *dst, const char *src1, const char *src2, const char *src3, const char *src4) -{ - return astring_catc(astring_assemble_3(dst, src1, src2, src3), src4); -} - -/* assemble an astring from 5 C strings */ -INLINE astring *astring_assemble_5(astring *dst, const char *src1, const char *src2, const char *src3, const char *src4, const char *src5) -{ - return astring_catc(astring_assemble_4(dst, src1, src2, src3, src4), src5); -} - - - -/*************************************************************************** - C++ WRAPPERS -***************************************************************************/ - -#ifdef __cplusplus -#ifdef SDLMAME_NETBSD -#undef toupper -#undef tolower -#endif - -/* derived class for C++ */ -class astring : public astring_base -{ -private: - astring &init(); - public: + // simple construction/destruction astring() { init(); } ~astring(); + // construction with copy astring(const char *string) { init().cpy(string); } astring(const char *string, int length) { init().cpy(string, length); } astring(const char *str1, const char *str2) { init().cpy(str1).cat(str2); } @@ -327,14 +70,17 @@ public: astring(const astring &string) { init().cpy(string); } astring(const astring &string, int start, int count = -1) { init().cpysubstr(string, start, count); } + // assignment operators astring &operator=(const char *string) { return cpy(string); } astring &operator=(const astring &string) { return cpy(string); } + // concatenation operators astring& operator+=(const astring &string) { return cat(string); } friend astring operator+(const astring &lhs, const astring &rhs) { return astring(lhs) += rhs; } friend astring operator+(const astring &lhs, const char *rhs) { return astring(lhs) += rhs; } friend astring operator+(const char *lhs, const astring &rhs) { return astring(lhs) += rhs; } + // comparison operators bool operator==(const char *string) const { return (cmp(string) == 0); } bool operator==(const astring &string) const { return (cmp(string) == 0); } bool operator!=(const char *string) const { return (cmp(string) != 0); } @@ -348,68 +94,97 @@ public: bool operator>=(const char *string) const { return (cmp(string) >= 0); } bool operator>=(const astring &string) const { return (cmp(string) >= 0); } + // character access operators + char operator[](int index) const { return (index < len()) ? m_text[index] : 0; } + + // implicit boolean conversion operators + operator bool() { return m_text[0] != 0; } + operator bool() const { return m_text[0] != 0; } + + // C string conversion operators and helpers + operator const char *() const { return m_text; } + const char *cstr() const { return m_text; } + char *stringbuffer(int size) { ensure_room(size); return m_text; } + + // buffer management astring &reset() { return cpy(""); } - astring &expand(int length) { astring_expand(this, length); return *this; } + astring &expand(int length) { ensure_room(length); return *this; } - operator bool() { return this->text[0] != 0; } - operator bool() const { return this->text[0] != 0; } - operator const char *() const { return astring_c(this); } - const char *cstr() const { return astring_c(this); } - int len() const { return astring_len(this); } + // length query + int len() const { return strlen(m_text); } - astring &cpy(const astring &src) { return *astring_cpy(this, &src); } - astring &cpy(const char *src) { return *astring_cpyc(this, src); } - astring &cpy(const char *src, int count) { return *astring_cpych(this, src, count); } - astring &cpysubstr(const astring &src, int start, int count) { return *astring_cpysubstr(this, &src, start, count); } + // copy helpers + astring &cpy(const char *src, int count); + astring &cpysubstr(const astring &src, int start, int count = -1); + astring &cpy(const astring &src) { return cpy(src.cstr(), src.len()); } + astring &cpy(const char *src) { return cpy(src, strlen(src)); } - astring &cat(char ch) { return *astring_insch(this, -1, &ch, 1); } - astring &cat(const astring &src) { return ins(-1, src); } - astring &cat(const char *src) { return ins(-1, src); } + // insertion helpers + astring &ins(int insbefore, const char *src, int count); + astring &inssubstr(int insbefore, const astring &src, int start, int count = -1); + astring &ins(int insbefore, const astring &src) { return ins(insbefore, src.cstr(), src.len()); } + astring &ins(int insbefore, const char *src) { return ins(insbefore, src, strlen(src)); } + + // concatenation helpers (== insert at end) astring &cat(const char *src, int count) { return ins(-1, src, count); } - astring &catsubstr(const astring &src, int start, int count) { return inssubstr(-1, src, start, count); } + astring &catsubstr(const astring &src, int start, int count = -1) { return inssubstr(-1, src, start, count); } + astring &cat(const astring &src) { return ins(-1, src.cstr(), src.len()); } + astring &cat(const char *src) { return ins(-1, src, strlen(src)); } + astring &cat(char ch) { return ins(-1, &ch, 1); } - astring &ins(int insbefore, const astring &src) { return *astring_ins(this, insbefore, &src); } - astring &ins(int insbefore, const char *src) { return *astring_insc(this, insbefore, src); } - astring &ins(int insbefore, const char *src, int count) { return *astring_insch(this, insbefore, src, count); } - astring &inssubstr(int insbefore, const astring &src, int start, int count) { return *astring_inssubstr(this, insbefore, &src, start, count); } + // substring helpers + astring &substr(int start, int count = -1); + astring &del(int start, int count = -1); - astring &substr(int start, int count) { return *astring_substr(this, start, count); } - astring &del(int start, int count) { return *astring_del(this, start, count); } + // formatted string helpers + int vprintf(const char *format, va_list args); + int catvprintf(const char *format, va_list args); + int printf(const char *format, ...) { va_list ap; va_start(ap, format); int result = this->vprintf(format, ap); va_end(ap); return result; } + int catprintf(const char *format, ...) { va_list ap; va_start(ap, format); int result = catvprintf(format, ap); va_end(ap); return result; } + astring &format(const char *format, ...) { va_list ap; va_start(ap, format); this->vprintf(format, ap); va_end(ap); return *this; } + astring &catformat(const char *format, ...) { va_list ap; va_start(ap, format); catvprintf(format, ap); va_end(ap); return *this; } - int printf(const char *format, ...) { va_list ap; va_start(ap, format); int result = astring_vprintf(this, format, ap); va_end(ap); return result; } - int vprintf(const char *format, va_list args) { return astring_vprintf(this, format, args); } - int catprintf(const char *format, ...) { va_list ap; va_start(ap, format); int result = astring_catvprintf(this, format, ap); va_end(ap); return result; } - int catvprintf(const char *format, va_list args) { return astring_catvprintf(this, format, args); } + // comparison helpers + int cmp(const char *str2, int count) const; + int cmpsubstr(const astring &str2, int start, int count = -1) const; + int cmp(const astring &str2) const { return cmp(str2.cstr(), str2.len()); } + int cmp(const char *str2) const { return cmp(str2, strlen(str2)); } - astring &format(const char *format, ...) { va_list ap; va_start(ap, format); astring_vprintf(this, format, ap); va_end(ap); return *this; } + // case-insensitive comparison helpers + int icmp(const char *str2, int count) const; + int icmpsubstr(const astring &str2, int start, int count = -1) const; + int icmp(const astring &str2) const { return icmp(str2.cstr(), str2.len()); } + int icmp(const char *str2) const { return icmp(str2, strlen(str2)); } - int cmp(const astring &str2) const { return astring_cmp(this, &str2); } - int cmp(const char *str2) const { return astring_cmpc(this, str2); } - int cmp(const char *str2, int count) const { return astring_cmpch(this, str2, count); } - int cmpsubstr(const astring &str2, int start, int count) const { return astring_cmpsubstr(this, &str2, start, count); } + // character searching helpers + int chr(int start, int ch) const; + int rchr(int start, int ch) const; - int icmp(const astring &str2) const { return astring_icmp(this, &str2); } - int icmp(const char *str2) const { return astring_icmpc(this, str2); } - int icmp(const char *str2, int count) const { return astring_icmpch(this, str2, count); } - int icmpsubstr(const astring &str2, int start, int count) const { return astring_icmpsubstr(this, &str2, start, count); } + // string searching/replacing helpers + int find(int start, const char *search) const; + int find(const char *search) const { return find(0, search); } + int replace(int start, const char *search, const char *replace); + int replace(const char *search, const char *_replace) { return replace(0, search, _replace); } - int chr(int start, int ch) const { return astring_chr(this, start, ch); } - int rchr(int start, int ch) const { return astring_rchr(this, start, ch); } + // misc utilities + astring &delchr(int ch); + astring &replacechr(int ch, int newch); + astring &makeupper(); + astring &makelower(); + astring &trimspace(); - int find(int start, const astring &search) const { return astring_find(this, start, &search); } - int find(int start, const char *search) const { return astring_findc(this, start, search); } +private: + // internal helpers + astring &init(); + char *safe_string_base(int start) const; + bool ensure_room(int length); + void normalize_substr(int &start, int &count, int length) const; - int replace(int start, const astring &search, const astring &replace) { return astring_replace(this, start, &search, &replace); } - int replace(int start, const char *search, const char *replace) { return astring_replacec(this, start, search, replace); } - - astring &delchr(int ch) { return *astring_delchr(this, ch); } - astring &replacechr(int ch, int newch) { return *astring_replacechr(this, ch, newch); } - astring &toupper() { return *astring_toupper(this); } - astring &tolower() { return *astring_tolower(this); } - astring &trimspace() { return *astring_trimspace(this); } + // internal state + char * m_text; + int m_alloclen; + char m_smallbuf[64]; }; -#endif - #endif /* __ASTRING_H__ */ diff --git a/src/lib/util/corefile.c b/src/lib/util/corefile.c index 4c4b0673d3d..4d760f95d24 100644 --- a/src/lib/util/corefile.c +++ b/src/lib/util/corefile.c @@ -883,7 +883,7 @@ int CLIB_DECL core_fprintf(core_file *f, const char *fmt, ...) assumptions about path separators -------------------------------------------------*/ -astring *core_filename_extract_base(astring *result, const char *name, int strip_extension) +astring &core_filename_extract_base(astring &result, const char *name, bool strip_extension) { /* find the start of the name */ const char *start = name + strlen(name); @@ -891,11 +891,11 @@ astring *core_filename_extract_base(astring *result, const char *name, int strip start--; /* copy the rest into an astring */ - astring_cpyc(result, start); + result.cpy(start); /* chop the extension if present */ if (strip_extension) - astring_substr(result, 0, astring_rchr(result, 0, '.')); + result.substr(0, result.rchr(0, '.')); return result; } diff --git a/src/lib/util/corefile.h b/src/lib/util/corefile.h index 8c8aeb50de6..ff88efa004a 100644 --- a/src/lib/util/corefile.h +++ b/src/lib/util/corefile.h @@ -149,7 +149,7 @@ int CLIB_DECL core_fprintf(core_file *f, const char *fmt, ...) ATTR_PRINTF(2,3); /* ----- filename utilities ----- */ /* extract the base part of a filename (remove extensions and paths) */ -astring *core_filename_extract_base(astring *result, const char *name, int strip_extension); +astring &core_filename_extract_base(astring &result, const char *name, bool strip_extension = false); /* true if the given filename ends with a particular extension */ int core_filename_ends_with(const char *filename, const char *extension); diff --git a/src/lib/util/zippath.c b/src/lib/util/zippath.c index c2041072550..1974fe973ee 100644 --- a/src/lib/util/zippath.c +++ b/src/lib/util/zippath.c @@ -8,6 +8,7 @@ #include #include +#include #include "zippath.h" #include "unzip.h" #include "corestr.h" @@ -18,28 +19,35 @@ TYPE DEFINITIONS ***************************************************************************/ -typedef struct _zippath_returned_directory zippath_returned_directory; -struct _zippath_returned_directory +struct zippath_returned_directory { zippath_returned_directory *next; - char name[1]; + astring name; }; -struct _zippath_directory +class zippath_directory { +public: + zippath_directory() + : returned_parent(false), + directory(NULL), + called_zip_first(false), + zipfile(NULL), + returned_dirlist(NULL) { } + /* common */ - unsigned int returned_parent : 1; + bool returned_parent; osd_directory_entry returned_entry; /* specific to normal directories */ osd_directory *directory; /* specific to ZIP directories */ - unsigned int called_zip_first : 1; + bool called_zip_first; zip_file *zipfile; - astring *zipprefix; + astring zipprefix; zippath_returned_directory *returned_dirlist; }; @@ -99,13 +107,13 @@ static void parse_parent_path(const char *path, int *beginpos, int *endpos) zippath_parent - retrieves the parent directory -------------------------------------------------*/ -astring *zippath_parent(astring *dst, const char *path) +astring &zippath_parent(astring &dst, const char *path) { int pos; parse_parent_path(path, &pos, NULL); /* return the result */ - return (pos >= 0) ? astring_cpych(dst, path, pos + 1) : astring_cpyc(dst, ""); + return (pos >= 0) ? dst.cpy(path, pos + 1) : dst.reset(); } @@ -115,12 +123,12 @@ astring *zippath_parent(astring *dst, const char *path) directory basename -------------------------------------------------*/ -astring *zippath_parent_basename(astring *dst, const char *path) +astring &zippath_parent_basename(astring &dst, const char *path) { int beginpos, endpos; parse_parent_path(path, &beginpos, &endpos); - return astring_cpych(dst, path + beginpos + 1, endpos - beginpos); + return dst.cpy(path + beginpos + 1, endpos - beginpos); } @@ -129,31 +137,29 @@ astring *zippath_parent_basename(astring *dst, const char *path) zippath_combine - combines two paths -------------------------------------------------*/ -astring *zippath_combine(astring *dst, const char *path1, const char *path2) +astring &zippath_combine(astring &dst, const char *path1, const char *path2) { - astring *result; - if (!strcmp(path2, ".")) { - result = astring_cpyc(dst, path1); + dst.cpy(path1); } else if (!strcmp(path2, "..")) { - result = zippath_parent(dst, path1); + zippath_parent(dst, path1); } else if (osd_is_absolute_path(path2)) { - result = astring_cpyc(dst, path2); + dst.cpy(path2); } else if ((path1[0] != '\0') && !is_path_separator(path1[strlen(path1) - 1])) { - result = astring_assemble_3(dst, path1, PATH_SEPARATOR, path2); + dst.cpy(path1).cat(PATH_SEPARATOR).cat(path2); } else { - result = astring_assemble_2(dst, path1, path2); + dst.cpy(path1).cat(path2); } - return result; + return dst; } @@ -200,7 +206,7 @@ static file_error file_error_from_zip_error(zip_error ziperr) from a zip file entry -------------------------------------------------*/ -static file_error create_core_file_from_zip(zip_file *zip, const zip_file_header *header, core_file **file) +static file_error create_core_file_from_zip(zip_file *zip, const zip_file_header *header, core_file *&file) { file_error filerr; zip_error ziperr; @@ -220,7 +226,7 @@ static file_error create_core_file_from_zip(zip_file *zip, const zip_file_header goto done; } - filerr = core_fopen_ram_copy(ptr, header->uncompressed_length, OPEN_FLAG_READ, file); + filerr = core_fopen_ram_copy(ptr, header->uncompressed_length, OPEN_FLAG_READ, &file); if (filerr != FILERR_NONE) goto done; @@ -235,36 +241,30 @@ done: zippath_fopen - opens a zip path file -------------------------------------------------*/ -file_error zippath_fopen(const char *filename, UINT32 openflags, core_file **file, astring *revised_path) +file_error zippath_fopen(const char *filename, UINT32 openflags, core_file *&file, astring &revised_path) { file_error filerr = FILERR_NOT_FOUND; zip_error ziperr; zip_file *zip = NULL; const zip_file_header *header; osd_dir_entry_type entry_type; - astring *mainpath; - astring *subpath; - astring *temp; - astring *temp2; char *alloc_fullpath = NULL; int len; /* first, set up the two types of paths */ - mainpath = astring_cpyc(astring_alloc(), filename); - subpath = astring_alloc(); - temp = astring_alloc(); - temp2 = astring_alloc(); - *file = NULL; + astring mainpath(filename); + astring subpath; + file = NULL; /* loop through */ - while((*file == NULL) && (astring_len(mainpath) > 0) - && ((openflags == OPEN_FLAG_READ) || (astring_len(subpath) == 0))) + while((file == NULL) && (mainpath.len() > 0) + && ((openflags == OPEN_FLAG_READ) || (subpath.len() == 0))) { /* is the mainpath a ZIP path? */ - if (is_zip_file(astring_c(mainpath))) + if (is_zip_file(mainpath)) { /* this file might be a zip file - lets take a look */ - ziperr = zip_file_open(astring_c(mainpath), &zip); + ziperr = zip_file_open(mainpath, &zip); if (ziperr == ZIPERR_NONE) { /* it is a zip file - error if we're not opening for reading */ @@ -274,8 +274,8 @@ file_error zippath_fopen(const char *filename, UINT32 openflags, core_file **fil goto done; } - if (astring_len(subpath) > 0) - header = zippath_find_sub_path(zip, astring_c(subpath), &entry_type); + if (subpath.len() > 0) + header = zippath_find_sub_path(zip, subpath, &entry_type); else header = zip_file_first_file(zip); @@ -291,16 +291,16 @@ file_error zippath_fopen(const char *filename, UINT32 openflags, core_file **fil goto done; /* update subpath, if appropriate */ - if (astring_len(subpath) == 0) - astring_cpyc(subpath, header->filename); + if (subpath.len() == 0) + subpath.cpy(header->filename); /* we're done */ goto done; } } - if (astring_len(subpath) == 0) - filerr = core_fopen(filename, openflags, file); + if (subpath.len() == 0) + filerr = core_fopen(filename, openflags, &file); else filerr = FILERR_NOT_FOUND; @@ -308,56 +308,45 @@ file_error zippath_fopen(const char *filename, UINT32 openflags, core_file **fil if (filerr != FILERR_NONE) { /* go up a directory */ - zippath_parent(temp, astring_c(mainpath)); + astring temp; + zippath_parent(temp, mainpath); /* append to the sub path */ - if (astring_len(subpath) > 0) + if (subpath.len() > 0) { - astring_assemble_3(temp2, astring_c(mainpath) + astring_len(temp), PATH_SEPARATOR, astring_c(subpath)); - astring_cpy(subpath, temp2); + astring temp2; + temp2.cpysubstr(mainpath, temp.len()).cat(PATH_SEPARATOR).cat(subpath); + subpath.cpy(temp2); } else - { - astring_cpyc(subpath, astring_c(mainpath) + astring_len(temp)); - } + subpath.cpysubstr(mainpath, temp.len()); /* get the new main path, truncating path separators */ - len = astring_len(temp); - while((len > 0) && is_zip_file_separator(astring_c(temp)[len - 1])) + len = temp.len(); + while (len > 0 && is_zip_file_separator(temp[len - 1])) len--; - astring_cpych(mainpath, astring_c(temp), len); + mainpath.cpysubstr(temp, 0, len); } } done: - /* store the revised path if appropriate */ - if (revised_path != NULL) + /* store the revised path */ + revised_path.reset(); + if (filerr == FILERR_NONE) { - astring_cpyc(revised_path, ""); + /* cannonicalize mainpath */ + filerr = osd_get_full_path(&alloc_fullpath, mainpath); if (filerr == FILERR_NONE) { - /* cannonicalize mainpath */ - filerr = osd_get_full_path(&alloc_fullpath, astring_c(mainpath)); - if (filerr == FILERR_NONE) - { - if (astring_len(subpath) > 0) - astring_assemble_3(revised_path, alloc_fullpath, PATH_SEPARATOR, astring_c(subpath)); - else - astring_cpyc(revised_path, alloc_fullpath); - } + if (subpath.len() > 0) + revised_path.cpy(alloc_fullpath).cat(PATH_SEPARATOR).cat(subpath); + else + revised_path.cpy(alloc_fullpath); } } if (zip != NULL) zip_file_close(zip); - if (mainpath != NULL) - astring_free(mainpath); - if (subpath != NULL) - astring_free(subpath); - if (temp != NULL) - astring_free(temp); - if (temp2 != NULL) - astring_free(temp2); if (alloc_fullpath != NULL) osd_free(alloc_fullpath); return filerr; @@ -526,32 +515,32 @@ static const zip_file_header *zippath_find_sub_path(zip_file *zipfile, const cha true path and ZIP entry components -------------------------------------------------*/ -static file_error zippath_resolve(const char *path, osd_dir_entry_type *entry_type, - zip_file **zipfile, astring *newpath) +static file_error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, zip_file *&zipfile, astring &newpath) { file_error err; osd_directory_entry *current_entry = NULL; osd_dir_entry_type current_entry_type; - astring *apath = astring_cpyc(astring_alloc(), path); - astring *apath_trimmed = astring_alloc(); - astring *parent = NULL; int went_up = FALSE; int i; + + newpath.reset(); /* be conservative */ - *entry_type = ENTTYPE_NONE; - *zipfile = NULL; + entry_type = ENTTYPE_NONE; + zipfile = NULL; + astring apath(path); + astring apath_trimmed; do { /* trim the path of trailing path separators */ - i = astring_len(apath); - while((i > 1) && is_path_separator(astring_c(apath)[i - 1])) + i = apath.len(); + while (i > 1 && is_path_separator(apath[i - 1])) i--; - apath_trimmed = astring_cpysubstr(apath_trimmed, apath, 0, i); + apath_trimmed.cpysubstr(apath, 0, i); /* stat the path */ - current_entry = osd_stat(astring_c(apath_trimmed)); + current_entry = osd_stat(apath_trimmed); /* did we find anything? */ if (current_entry != NULL) @@ -566,12 +555,11 @@ static file_error zippath_resolve(const char *path, osd_dir_entry_type *entry_ty /* if we have not found the file or directory, go up */ current_entry_type = ENTTYPE_NONE; went_up = TRUE; - parent = zippath_parent(astring_alloc(), astring_c(apath)); - astring_free(apath); - apath = parent; + astring parent; + apath.cpy(zippath_parent(parent, apath)); } } - while((current_entry_type == ENTTYPE_NONE) && (apath != NULL) && !is_root(astring_c(apath))); + while (current_entry_type == ENTTYPE_NONE && !is_root(apath)); /* if we did not find anything, then error out */ if (current_entry_type == ENTTYPE_NONE) @@ -581,16 +569,16 @@ static file_error zippath_resolve(const char *path, osd_dir_entry_type *entry_ty } /* is this file a ZIP file? */ - if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(astring_c(apath_trimmed)) - && (zip_file_open(astring_c(apath_trimmed), zipfile) == ZIPERR_NONE)) + if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(apath_trimmed) + && (zip_file_open(apath_trimmed, &zipfile) == ZIPERR_NONE)) { - i = strlen(path + astring_len(apath)); - while((i > 0) && is_zip_path_separator(path[astring_len(apath) + i - 1])) + i = strlen(path + apath.len()); + while (i > 0 && is_zip_path_separator(path[apath.len() + i - 1])) i--; - astring_cpych(newpath, path + astring_len(apath), i); + newpath.cpy(path + apath.len(), i); /* this was a true ZIP path - attempt to identify the type of path */ - zippath_find_sub_path(*zipfile, astring_c(newpath), ¤t_entry_type); + zippath_find_sub_path(zipfile, newpath, ¤t_entry_type); if (current_entry_type == ENTTYPE_NONE) { err = FILERR_NOT_FOUND; @@ -605,18 +593,14 @@ static file_error zippath_resolve(const char *path, osd_dir_entry_type *entry_ty err = FILERR_NOT_FOUND; goto done; } - astring_cpyc(newpath, path); + newpath.cpy(path); } /* success! */ - *entry_type = current_entry_type; + entry_type = current_entry_type; err = FILERR_NONE; done: - if (apath != NULL) - astring_free(apath); - if (apath_trimmed != NULL) - astring_free(apath_trimmed); return err; } @@ -628,21 +612,18 @@ done: file_error zippath_opendir(const char *path, zippath_directory **directory) { file_error err; - osd_dir_entry_type entry_type; - astring *newpath = astring_alloc(); - zippath_directory *result; /* allocate a directory */ - result = (zippath_directory *) malloc(sizeof(*result)); + zippath_directory *result = new(std::nothrow) zippath_directory; if (result == NULL) { err = FILERR_OUT_OF_MEMORY; goto done; } - memset(result, 0, sizeof(*result)); /* resolve the path */ - err = zippath_resolve(path, &entry_type, &result->zipfile, newpath); + osd_dir_entry_type entry_type; + err = zippath_resolve(path, entry_type, result->zipfile, result->zipprefix); if (err != FILERR_NONE) goto done; @@ -654,12 +635,7 @@ file_error zippath_opendir(const char *path, zippath_directory **directory) } /* was the result a ZIP? */ - if (result->zipfile != NULL) - { - result->zipprefix = newpath; - newpath = NULL; - } - else + if (result->zipfile == NULL) { /* a conventional directory */ result->directory = osd_opendir(path); @@ -671,20 +647,15 @@ file_error zippath_opendir(const char *path, zippath_directory **directory) /* is this path the root? if so, pretend we've already returned the parent */ if (is_root(path)) - result->returned_parent = TRUE; + result->returned_parent = true; } done: - if (((directory == NULL) || (err != FILERR_NONE)) && (result != NULL)) + if ((directory == NULL || err != FILERR_NONE) && result != NULL) { zippath_closedir(result); result = NULL; } - if (newpath != NULL) - { - astring_free(newpath); - newpath = NULL; - } if (directory != NULL) *directory = result; return err; @@ -697,25 +668,20 @@ done: void zippath_closedir(zippath_directory *directory) { - zippath_returned_directory *dirlist; - if (directory->directory != NULL) osd_closedir(directory->directory); if (directory->zipfile != NULL) zip_file_close(directory->zipfile); - if (directory->zipprefix != NULL) - astring_free(directory->zipprefix); - - while(directory->returned_dirlist != NULL) + while (directory->returned_dirlist != NULL) { - dirlist = directory->returned_dirlist; + zippath_returned_directory *dirlist = directory->returned_dirlist; directory->returned_dirlist = directory->returned_dirlist->next; - free(dirlist); + delete dirlist; } - free(directory); + delete directory; } @@ -728,10 +694,10 @@ void zippath_closedir(zippath_directory *directory) static const char *get_relative_path(zippath_directory *directory, const zip_file_header *header) { const char *result = NULL; - int len = astring_len(directory->zipprefix); + int len = directory->zipprefix.len(); if ((len <= strlen(header->filename)) - && !strncmp(astring_c(directory->zipprefix), header->filename, len)) + && !strncmp(directory->zipprefix, header->filename, len)) { result = &header->filename[len]; while(is_zip_file_separator(*result)) @@ -758,7 +724,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory) if (!directory->returned_parent) { /* first thing's first - return parent directory */ - directory->returned_parent = TRUE; + directory->returned_parent = true; memset(&directory->returned_entry, 0, sizeof(directory->returned_entry)); directory->returned_entry.name = ".."; directory->returned_entry.type = ENTTYPE_DIR; @@ -793,7 +759,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory) header = zip_file_first_file(directory->zipfile); else header = zip_file_next_file(directory->zipfile); - directory->called_zip_first = TRUE; + directory->called_zip_first = true; relpath = NULL; } while((header != NULL) && ((relpath = get_relative_path(directory, header)) == NULL)); @@ -817,10 +783,9 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory) if (rdent == NULL) { /* we've found a new directory; add this to returned_dirlist */ - rdent = (zippath_returned_directory *)malloc(sizeof(*rdent) + (separator - relpath)); + rdent = new zippath_returned_directory; rdent->next = directory->returned_dirlist; - memcpy(rdent->name, relpath, (separator - relpath) * sizeof(rdent->name[0])); - rdent->name[separator - relpath] = '\0'; + rdent->name.cpy(relpath, separator - relpath); directory->returned_dirlist = rdent; /* ...and return it */ diff --git a/src/lib/util/zippath.h b/src/lib/util/zippath.h index 2411ad392a3..12c7546ae9a 100644 --- a/src/lib/util/zippath.h +++ b/src/lib/util/zippath.h @@ -14,14 +14,13 @@ #include "corefile.h" #include "astring.h" #include "unzip.h" -#include "astring.h" /*************************************************************************** TYPE DEFINITIONS ***************************************************************************/ -typedef struct _zippath_directory zippath_directory; +class zippath_directory; @@ -32,19 +31,19 @@ typedef struct _zippath_directory zippath_directory; /* ----- path operations ----- */ /* retrieves the parent directory */ -astring *zippath_parent(astring *dst, const char *path); +astring &zippath_parent(astring &dst, const char *path); /* retrieves the parent directory basename */ -astring *zippath_parent_basename(astring *dst, const char *path); +astring &zippath_parent_basename(astring &dst, const char *path); /* combines two paths */ -astring *zippath_combine(astring *dst, const char *path1, const char *path2); +astring &zippath_combine(astring &dst, const char *path1, const char *path2); /* ----- file operations ----- */ /* opens a zip path file */ -file_error zippath_fopen(const char *filename, UINT32 openflags, core_file **file, astring *revised_path); +file_error zippath_fopen(const char *filename, UINT32 openflags, core_file *&file, astring &revised_path); /* ----- directory operations ----- */ diff --git a/src/mame/drivers/jaguar.c b/src/mame/drivers/jaguar.c index 80ac802d689..d447dc3c00c 100644 --- a/src/mame/drivers/jaguar.c +++ b/src/mame/drivers/jaguar.c @@ -515,14 +515,12 @@ static MACHINE_RESET( jaguar ) static emu_file *jaguar_nvram_fopen( running_machine &machine, UINT32 openflags) { device_image_interface *image = dynamic_cast(machine.device("cart")); - astring *fname; file_error filerr; emu_file *file; if (image->exists()) { - fname = astring_assemble_4( astring_alloc(), machine.system().name, PATH_SEPARATOR, image->basename_noext(), ".nv"); - filerr = mame_fopen( SEARCHPATH_NVRAM, astring_c( fname), openflags, &file); - astring_free( fname); + astring fname(machine.system().name, PATH_SEPARATOR, image->basename_noext(), ".nv"); + filerr = mame_fopen( SEARCHPATH_NVRAM, fname, openflags, &file); return (filerr == FILERR_NONE) ? file : NULL; } else diff --git a/src/osd/sdl/sdlmain.c b/src/osd/sdl/sdlmain.c index 3abe680dd7b..53ba8a49593 100644 --- a/src/osd/sdl/sdlmain.c +++ b/src/osd/sdl/sdlmain.c @@ -706,7 +706,7 @@ osd_font sdl_osd_interface::font_open(const char *_name, int &height) /* handle bdf fonts in the core */ if (name.len() > 4) - if (name.toupper().substr(name.len()-4,4) == ".BDF" ) + if (name.makeupper().substr(name.len()-4,4) == ".BDF" ) return NULL; font_name = CFStringCreateWithCString( NULL, _name, kCFStringEncodingUTF8 ); diff --git a/src/tools/regrep.c b/src/tools/regrep.c index d5180ce3ad2..f0995d47c88 100644 --- a/src/tools/regrep.c +++ b/src/tools/regrep.c @@ -180,15 +180,15 @@ static int CLIB_DECL compare_file(const void *file0ptr, const void *file1ptr); static summary_file *sort_file_list(void); /* HTML helpers */ -static core_file *create_file_and_output_header(const astring *filename, const astring *templatefile, const astring *title); -static void output_footer_and_close_file(core_file *file, const astring *templatefile, const astring *title); +static core_file *create_file_and_output_header(astring &filename, astring &templatefile, astring &title); +static void output_footer_and_close_file(core_file *file, astring &templatefile, astring &title); /* report generators */ -static void output_report(const astring *dirname, const astring *tempheader, const astring *tempfooter, summary_file *filelist); +static void output_report(astring &dirname, astring &tempheader, astring &tempfooter, summary_file *filelist); static int compare_screenshots(summary_file *curfile); -static int generate_png_diff(const summary_file *curfile, const astring *destdir, const char *destname); -static void create_linked_file(const astring *dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, const char *pngfile, const astring *tempheader, const astring *tempfooter); -static void append_driver_list_table(const char *header, const astring *dirname, core_file *indexfile, const summary_file *listhead, const astring *tempheader, const astring *tempfooter); +static int generate_png_diff(const summary_file *curfile, astring &destdir, const char *destname); +static void create_linked_file(astring &dirname, const summary_file *curfile, const summary_file *prevfile, const summary_file *nextfile, const char *pngfile, astring &tempheader, astring &tempfooter); +static void append_driver_list_table(const char *header, astring &dirname, core_file *indexfile, const summary_file *listhead, astring &tempheader, astring &tempfooter); @@ -250,7 +250,6 @@ INLINE int get_unique_index(const summary_file *curfile, int index) int main(int argc, char *argv[]) { - astring *dirname = NULL, *tempfilename = NULL, *tempheader = NULL, *tempfooter = NULL; UINT32 bufsize; void *buffer; int listnum; @@ -262,31 +261,33 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage:\nregrep