file_error core_fload(const char *filename, dynamic_buffer &data) { core_file *file = NULL; file_error err; UINT64 size; /* attempt to open the file */ err = core_fopen(filename, OPEN_FLAG_READ, &file); if (err != FILERR_NONE) return err; /* get the size */ size = core_fsize(file); if ((UINT32)size != size) { core_fclose(file); return FILERR_OUT_OF_MEMORY; } /* allocate memory */ data.resize(size); /* read the data */ if (core_fread(file, &data[0], size) != size) { core_fclose(file); data.clear(); return FILERR_FAILURE; } /* close the file and return data */ core_fclose(file); return FILERR_NONE; }
void device_image_interface::clear() { if (m_mame_file) { m_mame_file = nullptr; m_file = nullptr; } else { if (m_file) { core_fclose(m_file); m_file = nullptr; } } m_image_name.clear(); m_readonly = false; m_created = false; m_longname.clear(); m_manufacturer.clear(); m_year.clear(); m_basename.clear(); m_basename_noext.clear(); m_filetype.clear(); m_full_software_name.clear(); m_software_info_ptr = nullptr; m_software_part_ptr = nullptr; m_software_list_name.clear(); }
void emu_file::close() { // close files and free memory if (m__7zfile != nullptr) _7z_file_close(m__7zfile); m__7zfile = nullptr; if (m_zipfile != nullptr) zip_file_close(m_zipfile); m_zipfile = nullptr; if (m_file != nullptr) core_fclose(m_file); m_file = nullptr; m__7zdata.clear(); m_zipdata.clear(); if (m_remove_on_close) osd_rmfile(m_fullpath.c_str()); m_remove_on_close = false; // reset our hashes and path as well m_hashes.reset(); m_fullpath.clear(); }
static void output_footer_and_close_file(core_file *file, std::string &templatefile, std::string &title) { std::string modified(templatefile); strreplace(modified, "<!--TITLE-->", title.c_str()); core_fwrite(file, modified.c_str(), modified.length()); core_fclose(file); }
static void output_footer_and_close_file(core_file *file, astring &templatefile, astring &path) { astring modified(templatefile); modified.replace(0, "<!--PATH-->", path.cstr()); core_fwrite(file, modified.cstr(), modified.len()); core_fclose(file); }
void stream_close(imgtool_stream *s) { assert(s != NULL); switch(s->imgtype) { case IMG_FILE: if (s->u.file != NULL) { core_fclose(s->u.file); s->u.file = NULL; } break; case IMG_MEM: if (s->u.buffer != NULL) { free(s->u.buffer); s->u.buffer = NULL; } break; default: assert(0); break; } free((void *) s); }
floppy_image_format_t *floppy_image_device::identify(std::string filename) { core_file *fd; std::string revised_path; file_error err = zippath_fopen(filename.c_str(), OPEN_FLAG_READ, fd, revised_path); if(err) { seterror(IMAGE_ERROR_INVALIDIMAGE, "Unable to open the image file"); return 0; } io_generic io; io.file = fd; io.procs = &corefile_ioprocs_noclose; io.filler = 0xff; int best = 0; floppy_image_format_t *best_format = 0; for(floppy_image_format_t *format = fif_list; format; format = format->next) { int score = format->identify(&io, form_factor); if(score > best) { best = score; best_format = format; } } core_fclose(fd); return best_format; }
static void output_footer_and_close_file(core_file *file, astring &templatefile, astring &title) { astring modified(templatefile); modified.replace(0, "<!--TITLE-->", title.c_str()); core_fwrite(file, modified.c_str(), modified.len()); core_fclose(file); }
void device_image_interface::clear() { if (m_mame_file) { global_free(m_mame_file); m_mame_file = NULL; m_file = NULL; } else { if (m_file) { core_fclose(m_file); m_file = NULL; } } m_image_name.reset(); m_readonly = false; m_created = false; m_longname.reset(); m_manufacturer.reset(); m_year.reset(); m_basename.reset(); m_basename_noext.reset(); m_filetype.reset(); m_full_software_name = NULL; m_software_info_ptr = NULL; m_software_part_ptr = NULL; m_software_list_name = NULL; }
void legacy_image_device_base::clear() { if (m_mame_file) { mame_fclose(m_mame_file); m_mame_file = NULL; m_file = NULL; } else { if (m_file) { core_fclose(m_file); m_file = NULL; } } m_name.reset(); m_writeable = FALSE; m_created = FALSE; m_longname.reset(); m_manufacturer.reset(); m_year.reset(); m_playable.reset(); m_extrainfo.reset(); m_basename_noext.reset(); m_filetype.reset(); m_full_software_name = NULL; m_software_info_ptr = NULL; m_software_part_ptr = NULL; }
void emu_file::close() { // close files and free memory if (m__7zfile != NULL) _7z_file_close(m__7zfile); m__7zfile = NULL; if (m_zipfile != NULL) zip_file_close(m_zipfile); m_zipfile = NULL; if (m_file != NULL) core_fclose(m_file); m_file = NULL; m__7zdata.reset(); m_zipdata.reset(); if (m_remove_on_close) osd_rmfile(m_fullpath); m_remove_on_close = false; // reset our hashes and path as well m_hashes.reset(); m_fullpath.reset(); }
static astring *find_include_file(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) { astring *srcincpath = astring_dup(curpath->path); core_file *testfile; 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) { astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex); /* handle .. by removing a chunk from the incpath */ if (astring_cmpc(pathpart, "..") == 0) { int sepindex_part = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]); if (sepindex_part != -1) astring_substr(srcincpath, 0, sepindex_part); } /* otherwise, append a path separator and the pathpart */ else astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart); /* 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); /* see if we can open it */ if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE) { /* close the file */ core_fclose(testfile); return srcincpath; } /* free our include path */ astring_free(srcincpath); } return NULL; }
static void output_footer_and_close_file(core_file *file, const astring *templatefile, const astring *title) { astring *modified; modified = astring_dup(templatefile); astring_replacec(modified, 0, "<!--TITLE-->", astring_c(title)); core_fwrite(file, astring_c(modified), astring_len(modified)); astring_free(modified); core_fclose(file); }
static bool check_file(astring &srcincpath) { // see if we can open it core_file *testfile; if (core_fopen(srcincpath, OPEN_FLAG_READ, &testfile) == FILERR_NONE) { // close the file core_fclose(testfile); return true; } return false; }
int device_image_interface::reopen_for_write(const char *path) { if(m_file) core_fclose(m_file); file_error filerr = FILERR_NOT_FOUND; image_error_t err = IMAGE_ERROR_FILENOTFOUND; astring revised_path; /* attempt to open the file for writing*/ filerr = zippath_fopen(path, OPEN_FLAG_READ|OPEN_FLAG_WRITE|OPEN_FLAG_CREATE, m_file, revised_path); /* did the open succeed? */ switch(filerr) { case FILERR_NONE: /* success! */ m_readonly = 0; m_created = 1; err = IMAGE_ERROR_SUCCESS; break; case FILERR_NOT_FOUND: case FILERR_ACCESS_DENIED: /* file not found (or otherwise cannot open); continue */ err = IMAGE_ERROR_FILENOTFOUND; break; case FILERR_OUT_OF_MEMORY: /* out of memory */ err = IMAGE_ERROR_OUTOFMEMORY; break; case FILERR_ALREADY_OPEN: /* this shouldn't happen */ err = IMAGE_ERROR_ALREADYOPEN; break; case FILERR_FAILURE: case FILERR_TOO_MANY_FILES: case FILERR_INVALID_DATA: default: /* other errors */ err = IMAGE_ERROR_INTERNAL; break; } /* if successful, set the file name */ if (filerr == FILERR_NONE) set_image_filename(revised_path); return err; }
int main(int argc, char *argv[]) { // validate arguments if (argc < 3) { fprintf(stderr, "Usage:\n%s <input.png> [<input2.png> [...]] <output.bdc>\n", argv[0]); return 1; } const char *bdcname = argv[argc - 1]; // iterate over input files static render_font font; bool error = false; // create font temporary file error = render_font_create_temporaryfile(font, bdcname); for (int curarg = 1; curarg < argc - 1; curarg++) { // load the png file const char *pngname = argv[curarg]; core_file *file; file_error filerr = core_fopen(pngname, OPEN_FLAG_READ, &file); if (filerr != FILERR_NONE) { fprintf(stderr, "Error %d attempting to open PNG file\n", filerr); error = true; break; } bitmap_argb32 bitmap; png_error pngerr = png_read_bitmap(file, bitmap); core_fclose(file); if (pngerr != PNGERR_NONE) { fprintf(stderr, "Error %d reading PNG file\n", pngerr); error = true; break; } // parse the PNG into characters error = bitmap_to_chars(bitmap, font); if (error) break; // write out the resulting font error = render_font_save_cached(font, bdcname, 0); if (error) break; } // cleanup after ourselves return error ? 1 : 0; }
static void output_footer_and_close_file(core_file *file) { core_fprintf(file, "\n" "\t</div>\n" "\t</div>\n" "\t</div>\n" "</body>" "\n" "</html>\n" ); core_fclose(file); }
static bool render_font_create_temporaryfile(render_font &font, const char *filename) { total_numchars = 0; m_chartable.clear(); core_file *file; astring tmp_filename(filename, ".tmp"); file_error filerr = core_fopen(tmp_filename.cstr(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, &file); if (filerr != FILERR_NONE) return true; core_fclose(file); return false; }
file_error core_fload(const char *filename, void **data, UINT32 *length) { core_file *file = NULL; file_error err; UINT64 size; /* attempt to open the file */ err = core_fopen(filename, OPEN_FLAG_READ, &file); if (err != FILERR_NONE) return err; /* get the size */ size = core_fsize(file); if ((UINT32)size != size) { core_fclose(file); return FILERR_OUT_OF_MEMORY; } /* allocate memory */ *data = osd_malloc(size); if (length != NULL) *length = (UINT32)size; /* read the data */ if (core_fread(file, *data, size) != size) { core_fclose(file); free(*data); return FILERR_FAILURE; } /* close the file and return data */ core_fclose(file); return FILERR_NONE; }
static bool find_include_file(astring &srcincpath, int srcrootlen, const astring &srcfile, const astring &filename) { // iterate over include paths and find the file for (include_path *curpath = incpaths; curpath != NULL; curpath = curpath->next) { // 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; while ((sepindex = filename.chr(lastsepindex, '/')) != -1) { astring pathpart(filename, lastsepindex, sepindex - lastsepindex); // handle .. by removing a chunk from the incpath if (pathpart == "..") { int sepindex_part = srcincpath.rchr(0, PATH_SEPARATOR[0]); if (sepindex_part != -1) srcincpath.substr(0, sepindex_part); } // otherwise, append a path separator and the pathpart else srcincpath.cat(PATH_SEPARATOR).cat(pathpart); // advance past the previous index lastsepindex = sepindex + 1; } // now append the filename srcincpath.cat(PATH_SEPARATOR).catsubstr(filename, lastsepindex, -1); // see if we can open it core_file *testfile; if (core_fopen(srcincpath, OPEN_FLAG_READ, &testfile) == FILERR_NONE) { // close the file core_fclose(testfile); return true; } } return false; }
void cdrom_close(cdrom_file *file) { if (file == NULL) return; /* free the cache */ if (file->cache) free(file->cache); if (file->chd == NULL) { for (int i = 0; i < file->cdtoc.numtrks; i++) { core_fclose(file->fhandle[i]); } } free(file); }
file_error core_fopen(const char *filename, UINT32 openflags, core_file **file) { file_error filerr = FILERR_NOT_FOUND; /* allocate the file itself */ *file = (core_file *)malloc(sizeof(**file)); if (*file == NULL) return FILERR_OUT_OF_MEMORY; memset(*file, 0, sizeof(**file)); /* attempt to open the file */ filerr = osd_open(filename, openflags, &(*file)->file, &(*file)->length); (*file)->openflags = openflags; /* handle errors and return */ if (filerr != FILERR_NONE) { core_fclose(*file); *file = NULL; } return filerr; }
imgtool_stream *stream_open(const char *fname, int read_or_write) { file_error filerr; const char *ext; imgtool_stream *imgfile = NULL; static const UINT32 write_modes[] = { OPEN_FLAG_READ, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, OPEN_FLAG_READ | OPEN_FLAG_WRITE, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE }; core_file *f = NULL; char *buf = NULL; int len, i; imgtool_stream *s = NULL; char c; /* maybe we are just a ZIP? */ ext = strrchr(fname, '.'); if (ext && !mame_stricmp(ext, ".zip")) return stream_open_zip(fname, NULL, read_or_write); filerr = core_fopen(fname, write_modes[read_or_write], &f); if (filerr != FILERR_NONE) { if (!read_or_write) { len = strlen(fname); /* can't open the file; try opening ZIP files with other names */ buf = (char*)malloc(len + 1); if (!buf) goto error; strcpy(buf, fname); for(i = len-1; !s && (i >= 0); i--) { if ((buf[i] == '\\') || (buf[i] == '/')) { c = buf[i]; buf[i] = '\0'; s = stream_open_zip(buf, buf + i + 1, read_or_write); buf[i] = c; } } free(buf); buf = NULL; if (s) return s; } /* ah well, it was worth a shot */ goto error; } imgfile = (imgtool_stream *)malloc(sizeof(struct _imgtool_stream)); if (!imgfile) goto error; /* Normal file */ memset(imgfile, 0, sizeof(*imgfile)); imgfile->imgtype = IMG_FILE; imgfile->position = 0; imgfile->filesize = core_fsize(f); imgfile->write_protect = read_or_write ? 0 : 1; imgfile->u.file = f; imgfile->name = fname; return imgfile; error: if (imgfile != NULL) free((void *) imgfile); if (f != NULL) core_fclose(f); if (buf) free(buf); return (imgtool_stream *) NULL; }
static int generate_png_diff(const summary_file *curfile, const astring *destdir, const char *destname) { bitmap_t *bitmaps[MAX_COMPARES] = { NULL }; astring *srcimgname = astring_alloc(); astring *dstfilename = astring_alloc(); astring *tempname = astring_alloc(); bitmap_t *finalbitmap = NULL; int width, height, maxwidth; int bitmapcount = 0; int listnum, bmnum; core_file *file = NULL; file_error filerr; png_error pngerr; int error = -1; int starty; /* generate the common source filename */ astring_printf(dstfilename, "%s" PATH_SEPARATOR "%s", astring_c(destdir), destname); astring_printf(srcimgname, "snap" PATH_SEPARATOR "%s" PATH_SEPARATOR "final.png", curfile->name); /* open and load all unique bitmaps */ for (listnum = 0; listnum < list_count; listnum++) if (curfile->matchbitmap[listnum] == listnum) { astring_printf(tempname, "%s" PATH_SEPARATOR "%s", lists[listnum].dir, astring_c(srcimgname)); /* open the source image */ filerr = core_fopen(astring_c(tempname), OPEN_FLAG_READ, &file); if (filerr != FILERR_NONE) goto error; /* load the source image */ pngerr = png_read_bitmap(file, &bitmaps[bitmapcount++]); core_fclose(file); if (pngerr != PNGERR_NONE) goto error; } /* if there's only one unique bitmap, skip it */ if (bitmapcount <= 1) goto error; /* determine the size of the final bitmap */ height = width = 0; maxwidth = bitmaps[0]->width; for (bmnum = 1; bmnum < bitmapcount; bmnum++) { int curwidth; /* determine the maximal width */ maxwidth = MAX(maxwidth, bitmaps[bmnum]->width); curwidth = bitmaps[0]->width + BITMAP_SPACE + maxwidth + BITMAP_SPACE + maxwidth; width = MAX(width, curwidth); /* add to the height */ height += MAX(bitmaps[0]->height, bitmaps[bmnum]->height); if (bmnum != 1) height += BITMAP_SPACE; } /* allocate the final bitmap */ finalbitmap = bitmap_alloc(width, height, BITMAP_FORMAT_ARGB32); if (finalbitmap == NULL) goto error; /* now copy and compare each set of bitmaps */ starty = 0; for (bmnum = 1; bmnum < bitmapcount; bmnum++) { bitmap_t *bitmap1 = bitmaps[0]; bitmap_t *bitmap2 = bitmaps[bmnum]; int curheight = MAX(bitmap1->height, bitmap2->height); int x, y; /* iterate over rows in these bitmaps */ for (y = 0; y < curheight; y++) { UINT32 *src1 = (y < bitmap1->height) ? BITMAP_ADDR32(bitmap1, y, 0) : NULL; UINT32 *src2 = (y < bitmap2->height) ? BITMAP_ADDR32(bitmap2, y, 0) : NULL; UINT32 *dst1 = BITMAP_ADDR32(finalbitmap, starty + y, 0); UINT32 *dst2 = BITMAP_ADDR32(finalbitmap, starty + y, bitmap1->width + BITMAP_SPACE); UINT32 *dstdiff = BITMAP_ADDR32(finalbitmap, starty + y, bitmap1->width + BITMAP_SPACE + maxwidth + BITMAP_SPACE); /* now iterate over columns */ for (x = 0; x < maxwidth; x++) { int pix1 = -1, pix2 = -2; if (src1 != NULL && x < bitmap1->width) pix1 = dst1[x] = src1[x]; if (src2 != NULL && x < bitmap2->width) pix2 = dst2[x] = src2[x]; dstdiff[x] = (pix1 != pix2) ? 0xffffffff : 0xff000000; } } /* update the starting Y position */ starty += BITMAP_SPACE + MAX(bitmap1->height, bitmap2->height); } /* write the final PNG */ filerr = core_fopen(astring_c(dstfilename), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, &file); if (filerr != FILERR_NONE) goto error; pngerr = png_write_bitmap(file, NULL, finalbitmap, 0, NULL); core_fclose(file); if (pngerr != PNGERR_NONE) goto error; /* if we get here, we are error free */ error = 0; error: if (finalbitmap != NULL) bitmap_free(finalbitmap); for (bmnum = 0; bmnum < bitmapcount; bmnum++) if (bitmaps[bmnum] != NULL) bitmap_free(bitmaps[bmnum]); if (error) osd_rmfile(astring_c(dstfilename)); astring_free(dstfilename); astring_free(srcimgname); astring_free(tempname); return error; }
static void corefile_closeproc(void *file) { core_fclose((core_file*)file); }
static int compare_screenshots(summary_file *curfile) { bitmap_argb32 bitmaps[MAX_COMPARES]; int unique[MAX_COMPARES]; int numunique = 0; int listnum; /* iterate over all files and load their bitmaps */ for (listnum = 0; listnum < list_count; listnum++) if (curfile->status[listnum] == STATUS_SUCCESS) { astring fullname; file_error filerr; core_file *file; /* get the filename for the image */ fullname.printf("%s" PATH_SEPARATOR "snap" PATH_SEPARATOR "%s" PATH_SEPARATOR "final.png", lists[listnum].dir, curfile->name); /* open the file */ filerr = core_fopen(fullname.c_str(), OPEN_FLAG_READ, &file); /* if that failed, look in the old location */ if (filerr != FILERR_NONE) { /* get the filename for the image */ fullname.printf("%s" PATH_SEPARATOR "snap" PATH_SEPARATOR "_%s.png", lists[listnum].dir, curfile->name); /* open the file */ filerr = core_fopen(fullname.c_str(), OPEN_FLAG_READ, &file); } /* if that worked, load the file */ if (filerr == FILERR_NONE) { png_read_bitmap(file, bitmaps[listnum]); core_fclose(file); } } /* now find all the different bitmap types */ for (listnum = 0; listnum < list_count; listnum++) { curfile->matchbitmap[listnum] = 0xff; if (bitmaps[listnum].valid()) { bitmap_argb32 &this_bitmap = bitmaps[listnum]; /* compare against all unique bitmaps */ int compnum; for (compnum = 0; compnum < numunique; compnum++) { /* if the sizes are different, we differ; otherwise start off assuming we are the same */ bitmap_argb32 &base_bitmap = bitmaps[unique[compnum]]; bool bitmaps_differ = (this_bitmap.width() != base_bitmap.width() || this_bitmap.height() != base_bitmap.height()); /* compare scanline by scanline */ for (int y = 0; y < this_bitmap.height() && !bitmaps_differ; y++) { UINT32 *base = &base_bitmap.pix32(y); UINT32 *curr = &this_bitmap.pix32(y); /* scan the scanline */ int x; for (x = 0; x < this_bitmap.width(); x++) if (*base++ != *curr++) break; bitmaps_differ = (x != this_bitmap.width()); } /* if we matched, remember which listnum index we matched, and stop */ if (!bitmaps_differ) { curfile->matchbitmap[listnum] = unique[compnum]; break; } /* if different from the first unique entry, adjust the status */ if (bitmaps_differ && compnum == 0) curfile->status[listnum] = STATUS_SUCCESS_DIFFERENT; } /* if we're unique, add ourselves to the list */ if (compnum >= numunique) { unique[numunique++] = listnum; curfile->matchbitmap[listnum] = listnum; continue; } } } /* if all screenshots matched, we're good */ if (numunique == 1) return BUCKET_GOOD; /* if the last screenshot matched the first unique one, we're good but changed */ if (curfile->matchbitmap[listnum - 1] == unique[0]) return BUCKET_GOOD_BUT_CHANGED_SCREENSHOTS; /* otherwise we're just changed */ return BUCKET_CHANGED; }
static int generate_png_diff(const summary_file *curfile, astring &destdir, const char *destname) { bitmap_argb32 bitmaps[MAX_COMPARES]; astring srcimgname; astring dstfilename; astring tempname; bitmap_argb32 finalbitmap; int width, height, maxwidth; int bitmapcount = 0; int listnum, bmnum; core_file *file = NULL; file_error filerr; png_error pngerr; int error = -1; int starty; /* generate the common source filename */ dstfilename.printf("%s" PATH_SEPARATOR "%s", destdir.c_str(), destname); srcimgname.printf("snap" PATH_SEPARATOR "%s" PATH_SEPARATOR "final.png", curfile->name); /* open and load all unique bitmaps */ for (listnum = 0; listnum < list_count; listnum++) if (curfile->matchbitmap[listnum] == listnum) { tempname.printf("%s" PATH_SEPARATOR "%s", lists[listnum].dir, srcimgname.c_str()); /* open the source image */ filerr = core_fopen(tempname.c_str(), OPEN_FLAG_READ, &file); if (filerr != FILERR_NONE) goto error; /* load the source image */ pngerr = png_read_bitmap(file, bitmaps[bitmapcount++]); core_fclose(file); if (pngerr != PNGERR_NONE) goto error; } /* if there's only one unique bitmap, skip it */ if (bitmapcount <= 1) goto error; /* determine the size of the final bitmap */ height = width = 0; maxwidth = bitmaps[0].width(); for (bmnum = 1; bmnum < bitmapcount; bmnum++) { int curwidth; /* determine the maximal width */ maxwidth = MAX(maxwidth, bitmaps[bmnum].width()); curwidth = bitmaps[0].width() + BITMAP_SPACE + maxwidth + BITMAP_SPACE + maxwidth; width = MAX(width, curwidth); /* add to the height */ height += MAX(bitmaps[0].height(), bitmaps[bmnum].height()); if (bmnum != 1) height += BITMAP_SPACE; } /* allocate the final bitmap */ finalbitmap.allocate(width, height); /* now copy and compare each set of bitmaps */ starty = 0; for (bmnum = 1; bmnum < bitmapcount; bmnum++) { bitmap_argb32 &bitmap1 = bitmaps[0]; bitmap_argb32 &bitmap2 = bitmaps[bmnum]; int curheight = MAX(bitmap1.height(), bitmap2.height()); int x, y; /* iterate over rows in these bitmaps */ for (y = 0; y < curheight; y++) { UINT32 *src1 = (y < bitmap1.height()) ? &bitmap1.pix32(y) : NULL; UINT32 *src2 = (y < bitmap2.height()) ? &bitmap2.pix32(y) : NULL; UINT32 *dst1 = &finalbitmap.pix32(starty + y, 0); UINT32 *dst2 = &finalbitmap.pix32(starty + y, bitmap1.width() + BITMAP_SPACE); UINT32 *dstdiff = &finalbitmap.pix32(starty + y, bitmap1.width() + BITMAP_SPACE + maxwidth + BITMAP_SPACE); /* now iterate over columns */ for (x = 0; x < maxwidth; x++) { int pix1 = -1, pix2 = -2; if (src1 != NULL && x < bitmap1.width()) pix1 = dst1[x] = src1[x]; if (src2 != NULL && x < bitmap2.width()) pix2 = dst2[x] = src2[x]; dstdiff[x] = (pix1 != pix2) ? 0xffffffff : 0xff000000; } } /* update the starting Y position */ starty += BITMAP_SPACE + MAX(bitmap1.height(), bitmap2.height()); } /* write the final PNG */ filerr = core_fopen(dstfilename.c_str(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, &file); if (filerr != FILERR_NONE) goto error; pngerr = png_write_bitmap(file, NULL, finalbitmap, 0, NULL); core_fclose(file); if (pngerr != PNGERR_NONE) goto error; /* if we get here, we are error free */ error = 0; error: if (error) osd_rmfile(dstfilename.c_str()); return error; }
static bool find_include_file(astring &srcincpath, int srcrootlen, int dstrootlen, astring &srcfile, astring &dstfile, astring &filename) { // iterate over include paths and find the file for (include_path *curpath = incpaths; curpath != NULL; curpath = curpath->next) { // a '.' include path is specially treated if (curpath->path == ".") srcincpath.cpysubstr(srcfile, 0, srcfile.rchr(0, PATH_SEPARATOR[0])); else srcincpath.cpysubstr(srcfile, 0, srcrootlen + 1).cat(curpath->path); // append the filename piecemeal to account for directories int lastsepindex = 0; int sepindex; while ((sepindex = filename.chr(lastsepindex, '/')) != -1) { // handle .. by removing a chunk from the incpath astring pathpart(filename, lastsepindex, sepindex - lastsepindex); if (pathpart == "..") { sepindex = srcincpath.rchr(0, PATH_SEPARATOR[0]); if (sepindex != -1) srcincpath.substr(0, sepindex); } // otherwise, append a path separator and the pathpart else srcincpath.cat(PATH_SEPARATOR).cat(pathpart); // advance past the previous index lastsepindex = sepindex + 1; } // now append the filename srcincpath.cat(PATH_SEPARATOR).catsubstr(filename, lastsepindex, -1); // see if we can open it core_file *testfile; if (core_fopen(srcincpath, OPEN_FLAG_READ, &testfile) == FILERR_NONE) { // close the file core_fclose(testfile); // find the longest matching directory substring between the include and source file lastsepindex = 0; while ((sepindex = srcincpath.chr(lastsepindex, PATH_SEPARATOR[0])) != -1) { // get substrings up to the current directory astring tempfile(srcfile, 0, sepindex); astring tempinc(srcincpath, 0, sepindex); // if we don't match, stop if (tempfile != tempinc) break; lastsepindex = sepindex + 1; } // chop off the common parts of the paths astring tempfile(srcfile, lastsepindex, -1); srcincpath.substr(lastsepindex, -1).replacechr(PATH_SEPARATOR[0], '/'); // for each directory left in the filename, we need to prepend a "../" while ((sepindex = tempfile.chr(0, PATH_SEPARATOR[0])) != -1) { tempfile.substr(sepindex + 1, -1); srcincpath.ins(0, "../"); } srcincpath.cat(".html"); // free the strings and return the include path return true; } } return false; }
BOOL LoadDIB(const char *filename, HGLOBAL *phDIB, HPALETTE *pPal, int pic_type) { file_error filerr; core_file *file = NULL; BOOL success = FALSE; const char *dir_name; const char *zip_name; astring *fname; void *buffer = NULL; if (pPal != NULL ) { DeletePalette(pPal); } switch (pic_type) { case TAB_SCREENSHOT: dir_name = GetImgDir(); zip_name = "snap"; break; case TAB_FLYER: dir_name = GetFlyerDir(); zip_name = "flyers"; break; case TAB_CABINET: dir_name = GetCabinetDir(); zip_name = "cabinets"; break; case TAB_MARQUEE: dir_name = GetMarqueeDir(); zip_name = "marquees"; break; case TAB_TITLE: dir_name = GetTitlesDir(); zip_name = "titles"; break; case TAB_CONTROL_PANEL: dir_name = GetControlPanelDir(); zip_name = "cpanel"; break; case TAB_PCB : dir_name = GetPcbDir(); zip_name = "pcb"; break; case BACKGROUND: dir_name = GetBgDir(); zip_name = "bkground"; break; default : // in case a non-image tab gets here, which can happen return FALSE; } //Add handling for the displaying of all the different supported snapshot patterntypes //%g fname = astring_assemble_2(astring_alloc(), filename, ".png"); filerr = OpenDIBFile(dir_name, zip_name, astring_c(fname), &file, &buffer); astring_free(fname); if (filerr != FILERR_NONE) { //%g/%i fname = astring_assemble_3(astring_alloc(), filename, PATH_SEPARATOR, "0000.png"); filerr = OpenDIBFile(dir_name, zip_name, astring_c(fname), &file, &buffer); astring_free(fname); } if (filerr != FILERR_NONE) { //%g%i fname = astring_assemble_2(astring_alloc(), filename, "0000.png"); filerr = OpenDIBFile(dir_name, zip_name, astring_c(fname), &file, &buffer); astring_free(fname); } if (filerr != FILERR_NONE) { //%g/%g fname = astring_assemble_4(astring_alloc(), filename, PATH_SEPARATOR, filename, ".png"); filerr = OpenDIBFile(dir_name, zip_name, astring_c(fname), &file, &buffer); astring_free(fname); } if (filerr != FILERR_NONE) { //%g/%g%i fname = astring_assemble_4(astring_alloc(), filename, PATH_SEPARATOR, filename, "0000.png"); filerr = OpenDIBFile(dir_name, zip_name, astring_c(fname), &file, &buffer); astring_free(fname); } if (filerr == FILERR_NONE) { success = png_read_bitmap_gui(file, phDIB, pPal); core_fclose(file); } // free the buffer if we have to if (buffer != NULL) { free(buffer); } return success; }
static int output_file(file_type type, int srcrootlen, int dstrootlen, astring &srcfile, astring &dstfile, bool link_to_file, astring &tempheader, astring &tempfooter) { // extract a normalized subpath astring srcfile_subpath; normalized_subpath(srcfile_subpath, srcfile, srcrootlen + 1); fprintf(stderr, "Processing %s\n", srcfile_subpath.cstr()); // set some defaults bool color_quotes = false; const char *comment_start = ""; const char *comment_start_esc = ""; const char *comment_end = ""; const char *comment_end_esc = ""; const char *comment_inline = ""; const char *comment_inline_esc = ""; const char *token_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_#"; const token_entry *token_table = dummy_token_table; // based on the file type, set the comment info switch (type) { case FILE_TYPE_C: color_quotes = true; comment_start = comment_start_esc = "/*"; comment_end = comment_end_esc = "*/"; comment_inline = comment_inline_esc = "//"; token_table = c_token_table; break; case FILE_TYPE_MAKE: color_quotes = true; comment_inline = comment_inline_esc = "#"; break; case FILE_TYPE_XML: color_quotes = true; comment_start = "<!--"; comment_start_esc = "<!--"; comment_end = "-->"; comment_end_esc = "-->"; break; default: case FILE_TYPE_TEXT: break; } // make the token lookup table bool is_token[256]; memset(is_token, 0, sizeof(is_token)); for (int toknum = 0; token_chars[toknum] != 0; toknum++) is_token[(UINT8)token_chars[toknum]] = true; // open the source file core_file *src; if (core_fopen(srcfile, OPEN_FLAG_READ, &src) != FILERR_NONE) { fprintf(stderr, "Unable to read file '%s'\n", srcfile.cstr()); return 1; } // open the output file core_file *dst = create_file_and_output_header(dstfile, tempheader, srcfile_subpath); if (dst == NULL) { fprintf(stderr, "Unable to write file '%s'\n", dstfile.cstr()); core_fclose(src); return 1; } // output the directory navigation core_fprintf(dst, "<h3>Viewing File: "); output_path_as_links(dst, srcfile_subpath, false, link_to_file); core_fprintf(dst, "</h3>"); // start with some tags core_fprintf(dst, "\t<pre class=\"source\">\n"); // iterate over lines in the source file int linenum = 1; bool in_comment = false; char srcline[4096]; while (core_fgets(srcline, ARRAY_LENGTH(srcline), src) != NULL) { // start with the line number astring dstline; dstline.catprintf("<span class=\"linenum\">%5d</span> ", linenum++); // iterate over characters in the source line bool escape = false; bool in_quotes = false; bool in_inline_comment = false; bool last_token_was_include = false; bool last_was_token = false; bool quotes_are_linked = false; UINT8 curquote = 0; int curcol = 0; for (char *srcptr = srcline; *srcptr != 0; ) { UINT8 ch = *srcptr++; // track whether or not we are within an extended (C-style) comment if (!in_quotes && !in_inline_comment) { if (!in_comment && ch == comment_start[0] && strncmp(srcptr - 1, comment_start, strlen(comment_start)) == 0) { dstline.catprintf("<span class=\"comment\">%s", comment_start_esc); curcol += strlen(comment_start); srcptr += strlen(comment_start) - 1; ch = 0; in_comment = true; } else if (in_comment && ch == comment_end[0] && strncmp(srcptr - 1, comment_end, strlen(comment_end)) == 0) { dstline.catprintf("%s</span>", comment_end_esc); curcol += strlen(comment_end); srcptr += strlen(comment_end) - 1; ch = 0; in_comment = false; } } // track whether or not we are within an inline (C++-style) comment if (!in_quotes && !in_comment && !in_inline_comment && ch == comment_inline[0] && strncmp(srcptr - 1, comment_inline, strlen(comment_inline)) == 0) { dstline.catprintf("<span class=\"comment\">%s", comment_inline_esc); curcol += strlen(comment_inline); srcptr += strlen(comment_inline) - 1; ch = 0; in_inline_comment = true; } // if this is the start of a new token, see if we want to color it if (!in_quotes && !in_comment && !in_inline_comment && !last_was_token && is_token[ch]) { const token_entry *curtoken; char *temp = srcptr; int toklength; // find the end of the token while (*temp != 0 && is_token[(UINT8)*temp]) temp++; toklength = temp - (srcptr - 1); // scan the token table last_token_was_include = false; for (curtoken = token_table; curtoken->token != NULL; curtoken++) if (strncmp(srcptr - 1, curtoken->token, toklength) == 0 && strlen(curtoken->token) == toklength) { dstline.catprintf("<span class=\"%s\">%s</span>", curtoken->color, curtoken->token); curcol += strlen(curtoken->token); srcptr += strlen(curtoken->token) - 1; ch = 0; // look for include tokens specially if (type == FILE_TYPE_C && strcmp(curtoken->token, "#include") == 0) last_token_was_include = true; break; } } last_was_token = is_token[ch]; // if we hit a tab, expand it if (ch == 0x09) { // compute how many spaces int spaces = 4 - curcol % 4; while (spaces--) { dstline.cat(' '); curcol++; } } // otherwise, copy the source character else if (ch != 0x0a && ch != 0x0d && ch != 0) { // track opening quotes if (!in_comment && !in_inline_comment && !in_quotes && (ch == '"' || ch == '\'')) { if (color_quotes) dstline.catprintf("<span class=\"string\">%c", ch); else dstline.cat(ch); in_quotes = true; curquote = ch; // handle includes if (last_token_was_include) { char *endquote = strchr(srcptr, ch); if (endquote != NULL) { astring filename(srcptr, endquote - srcptr); astring target; if (find_include_file(target, srcrootlen, dstrootlen, srcfile, dstfile, filename)) { dstline.catprintf("<a href=\"%s\">", target.cstr()); quotes_are_linked = true; } } } } // track closing quotes else if (!in_comment && !in_inline_comment && in_quotes && (ch == curquote) && !escape) { if (quotes_are_linked) dstline.catprintf("</a>"); if (color_quotes) dstline.catprintf("%c</span>", ch); else dstline.cat(ch); in_quotes = false; curquote = 0; quotes_are_linked = false; } // else just output the current character else if (ch == '&') dstline.catprintf("&"); else if (ch == '<') dstline.catprintf("<"); else if (ch == '>') dstline.catprintf(">"); else dstline.cat(ch); curcol++; } // Update escape state if (in_quotes) escape = (ch == '\\' && type == FILE_TYPE_C) ? !escape : false; } // finish inline comments if (in_inline_comment) { dstline.catprintf("</span>"); in_inline_comment = false; } // append a break and move on dstline.catprintf("\n"); core_fputs(dst, dstline); } // close tags core_fprintf(dst, "\t</pre>\n"); // close the file output_footer_and_close_file(dst, tempfooter, srcfile_subpath); core_fclose(src); return 0; }