int core_fputs(core_file *f, const char *s) { char convbuf[1024]; char *pconvbuf = convbuf; int count = 0; /* is this the beginning of the file? if so, write a byte order mark */ if (f->offset == 0 && !(f->openflags & OPEN_FLAG_NO_BOM)) { *pconvbuf++ = (char)0xef; *pconvbuf++ = (char)0xbb; *pconvbuf++ = (char)0xbf; } /* convert '\n' to platform dependant line endings */ while (*s != 0) { if (*s == '\n') { if (CRLF == 1) /* CR only */ *pconvbuf++ = 13; else if (CRLF == 2) /* LF only */ *pconvbuf++ = 10; else if (CRLF == 3) /* CR+LF */ { *pconvbuf++ = 13; *pconvbuf++ = 10; } } else *pconvbuf++ = *s; s++; /* if we overflow, break into chunks */ if (pconvbuf >= convbuf + ARRAY_LENGTH(convbuf) - 10) { count += core_fwrite(f, convbuf, pconvbuf - convbuf); pconvbuf = convbuf; } } /* final flush */ if (pconvbuf != convbuf) count += core_fwrite(f, convbuf, pconvbuf - convbuf); return count; }
UINT32 emu_file::write(const void *buffer, UINT32 length) { // write the data if we can if (m_file != NULL) return core_fwrite(m_file, buffer, length); return 0; }
static void write_data(core_file &file, UINT8 *base, UINT8 *end) { UINT32 bytes_written = core_fwrite(&file, base, end - base); if (bytes_written != end - base) { fprintf(stderr, "Error writing to destination file\n"); throw; } }
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); }
UINT32 stream_write(imgtool_stream *s, const void *buf, UINT32 sz) { void *new_buffer; UINT32 result = 0; switch(s->imgtype) { case IMG_MEM: if (!s->write_protect) { /* do we have to expand the buffer? */ if (s->filesize < s->position + sz) { /* try to expand the buffer */ if (s->u.buffer) free(s->u.buffer); new_buffer = malloc(s->position + sz); if (new_buffer) { s->u.buffer = (UINT8*)new_buffer; s->filesize = s->position + sz; } } /* do we have to limit sz? */ if (sz > (s->filesize - s->position)) sz = (UINT32) (s->filesize - s->position); memcpy(s->u.buffer + s->position, buf, sz); result = sz; } break; case IMG_FILE: core_fseek(s->u.file, s->position, SEEK_SET); result = core_fwrite(s->u.file, buf, sz); break; default: assert(0); break; } /* advance the file pointer */ s->position += result; /* did we grow the file */ if (s->position > s->filesize) s->filesize = s->position; return result; }
static core_file *create_file_and_output_header(astring &filename, astring &templatefile, astring &path) { // create the indexfile core_file *file; if (core_fopen(filename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, &file) != FILERR_NONE) return NULL; // print a header astring modified(templatefile); modified.replace(0, "<!--PATH-->", path.cstr()); core_fwrite(file, modified.cstr(), modified.len()); // return the file return file; }
static core_file *create_file_and_output_header(astring &filename, astring &templatefile, astring &title) { core_file *file; /* create the indexfile */ if (core_fopen(filename.c_str(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, &file) != FILERR_NONE) return NULL; /* print a header */ astring modified(templatefile); modified.replace("<!--TITLE-->", title.c_str()); core_fwrite(file, modified.c_str(), modified.len()); /* return the file */ return file; }
static core_file *create_file_and_output_header(const astring *filename, const astring *templatefile, const astring *title) { astring *modified; core_file *file; /* create the indexfile */ if (core_fopen(astring_c(filename), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, &file) != FILERR_NONE) return NULL; /* print a header */ modified = astring_dup(templatefile); astring_replacec(modified, 0, "<!--TITLE-->", astring_c(title)); core_fwrite(file, astring_c(modified), astring_len(modified)); /* return the file */ astring_free(modified); return file; }
static size_t corefile_writeproc(void *file, const void *buffer, size_t length) { return core_fwrite((core_file*)file, buffer, length); }
static bool render_font_save_cached(render_font &font, const char *filename, UINT32 hash) { // attempt to open the file core_file *file; file_error filerr = core_fopen(filename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, &file); if (filerr != FILERR_NONE) return true; core_file *tmp_file; astring tmp_filename(filename, ".tmp"); filerr = core_fopen(tmp_filename.cstr(), OPEN_FLAG_WRITE | OPEN_FLAG_READ, &tmp_file); if (filerr != FILERR_NONE) return true; core_fseek(tmp_file, 0, SEEK_END); try { // determine the number of characters int numchars = 0; for (int chnum = 0; chnum < 65536; chnum++) if (font.chars[chnum].width > 0) numchars++; total_numchars += numchars; // write the header dynamic_buffer tempbuffer(65536); UINT8 *dest = &tempbuffer[0]; *dest++ = 'f'; *dest++ = 'o'; *dest++ = 'n'; *dest++ = 't'; *dest++ = hash >> 24; *dest++ = hash >> 16; *dest++ = hash >> 8; *dest++ = hash & 0xff; *dest++ = font.height >> 8; *dest++ = font.height & 0xff; *dest++ = font.yoffs >> 8; *dest++ = font.yoffs & 0xff; *dest++ = total_numchars >> 24; *dest++ = total_numchars >> 16; *dest++ = total_numchars >> 8; *dest++ = total_numchars & 0xff; write_data(*file, tempbuffer, dest); // write the empty table to the beginning of the file //m_chartable.resize_keep(total_numchars * CACHED_CHAR_SIZE + 1); //write_data(*file, &m_chartable[0], &m_chartable[total_numchars * CACHED_CHAR_SIZE]); // loop over all characters int tableindex = total_numchars - numchars; for (int chnum = 0; chnum < 65536; chnum++) { render_font_char &ch = font.chars[chnum]; if (ch.width > 0) { // write out a bit-compressed bitmap if we have one if (ch.bitmap != NULL) { // write the data to the tempbuffer dest = tempbuffer; UINT8 accum = 0; UINT8 accbit = 7; // bit-encode the character data for (int y = 0; y < ch.bmheight; y++) { int desty = y + font.height + font.yoffs - ch.yoffs - ch.bmheight; const UINT32 *src = (desty >= 0 && desty < font.height) ? &ch.bitmap->pix32(desty) : NULL; for (int x = 0; x < ch.bmwidth; x++) { if (src != NULL && src[x] != 0) accum |= 1 << accbit; if (accbit-- == 0) { *dest++ = accum; accum = 0; accbit = 7; } } } // flush any extra if (accbit != 7) *dest++ = accum; // write the data write_data(*tmp_file, tempbuffer, dest); // free the bitmap and texture global_free(ch.bitmap); ch.bitmap = NULL; } // compute the table entry dest = &m_chartable[tableindex++ * CACHED_CHAR_SIZE]; *dest++ = chnum >> 8; *dest++ = chnum & 0xff; *dest++ = ch.width >> 8; *dest++ = ch.width & 0xff; *dest++ = ch.xoffs >> 8; *dest++ = ch.xoffs & 0xff; *dest++ = ch.yoffs >> 8; *dest++ = ch.yoffs & 0xff; *dest++ = ch.bmwidth >> 8; *dest++ = ch.bmwidth & 0xff; *dest++ = ch.bmheight >> 8; *dest++ = ch.bmheight & 0xff; ch.width = 0; } } // seek back to the beginning and rewrite the table core_fseek(file, CACHED_HEADER_SIZE, SEEK_SET); write_data(*file, &m_chartable[0], &m_chartable[total_numchars * CACHED_CHAR_SIZE]); // mamep:copy from temporary file UINT32 filesize = (UINT32)core_fsize(tmp_file); tempbuffer.resize(filesize); tempbuffer.clear(); core_fseek(tmp_file, 0, SEEK_SET); core_fread(tmp_file, tempbuffer, filesize); core_fwrite(file, tempbuffer, filesize); // all done core_fclose(file); core_fclose(tmp_file); return false; } catch (...) { core_fclose(file); core_fclose(tmp_file); osd_rmfile(filename); osd_rmfile(tmp_filename.cstr()); return true; } }