static scamper_file_t *file_open(int fd, char *fn, char mode, int type) { scamper_file_t *sf; int (*open_func)(scamper_file_t *); if(mode == 'r') open_func = file_open_read; else if(mode == 'w') open_func = file_open_write; else if(mode == 'a') open_func = file_open_append; else return NULL; if((sf = (scamper_file_t *)malloc_zero(sizeof(scamper_file_t))) == NULL) { return NULL; } sf->type = type; sf->fd = fd; if(fn != NULL && (sf->filename = strdup(fn)) == NULL) { return NULL; } if(open_func(sf) == -1) { scamper_file_close(sf); return NULL; } return sf; }
/** * mio_new_file_full: * @filename: Filename to open, passed as-is to @open_func as the first argument * @mode: Mode in which open the file, passed as-is to @open_func as the second * argument * @open_func: A function with the fopen() semantic to use to open the file * @close_func: A function with the fclose() semantic to close the file when * the #MIO object is destroyed, or %NULL not to close the #FILE * object * * Creates a new #MIO object working on a file, from a filename and an opening * function. See also mio_new_file(). * * This function is generally overkill and mio_new_file() should often be used * instead, but it allows to specify a custom function to open a file, as well * as a close function. The former is useful e.g. if you need to wrap fopen() * for some reason (like filename encoding conversion for example), and the * latter allows you both to match your custom open function and to choose * whether the underlying #FILE object should or not be closed when mio_free() * is called on the returned object. * * Free-function: mio_free() * * Returns: A new #MIO on success, or %NULL on failure. */ MIO *mio_new_file_full (const char *filename, const char *mode, MIOFOpenFunc open_func, MIOFCloseFunc close_func) { MIO *mio; /* we need to create the MIO object first, because we may not be able to close * the opened file if the user passed NULL as the close function, which means * that everything must succeed if we've opened the file successfully */ mio = xMalloc (1, MIO); if (mio) { FILE *fp = open_func (filename, mode); if (! fp) { eFree (mio); mio = NULL; } else { mio->type = MIO_TYPE_FILE; mio->impl.file.fp = fp; mio->impl.file.close_func = close_func; mio->refcount = 1; mio->udata.d = NULL; mio->udata.f = NULL; } } return mio; }
/** * mio_new_file_full: * @filename: Filename to open, passed as-is to @open_func as the first argument * @mode: Mode in which open the file, passed as-is to @open_func as the second * argument * @open_func: A function with the fopen() semantic to use to open the file * @close_func: A function with the fclose() semantic to close the file when * the #MIO object is destroyed, or %NULL not to close the #FILE * object * * Creates a new #MIO object working on a file, from a filename and an opening * function. See also mio_new_file(). * * This function is generally overkill and mio_new_file() should often be used * instead, but it allows to specify a custom function to open a file, as well * as a close function. The former is useful e.g. if you need to wrap fopen() * for some reason (like filename encoding conversion for example), and the * latter allows you both to match your custom open function and to choose * whether the underlying #FILE object should or not be closed when mio_free() * is called on the returned object. * * Free-function: mio_free() * * Returns: A new #MIO on success, or %NULL on failure. */ MIO * mio_new_file_full (const gchar *filename, const gchar *mode, MIOFOpenFunc open_func, MIOFCloseFunc close_func) { MIO *mio; /* we need to create the MIO object first, because we may not be able to close * the opened file if the user passed NULL as the close function, which means * that everything must succeed if we've opened the file successfully */ mio = g_slice_alloc (sizeof *mio); if (mio) { FILE *fp = open_func (filename, mode); if (! fp) { g_slice_free1 (sizeof *mio, mio); mio = NULL; } else { mio->type = MIO_TYPE_FILE; mio->impl.file.fp = fp; mio->impl.file.close_func = close_func; /* function table filling */ FILE_SET_VTABLE (mio); } } return mio; }
static void body (LexState *ls, expdesc *e, int needself, int line) { /* body -> `(' parlist `)' chunk END */ FuncState new_fs; open_func(ls, &new_fs); try { new_fs.f->linedefined = line; checknext(ls, '('); if (needself) { new_localvarliteral(ls, "self", 0); adjustlocalvars(ls, 1); } parlist(ls); checknext(ls, ')'); chunk(ls); new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); } catch( ... ) { close_func(ls); // We do not need the proto anymore. new_fs.f->DereferenceGC( ls->L ); throw; } close_func(ls); pushclosure(ls, &new_fs, e); // Terminate the FuncState again. new_fs.f->DereferenceGC( ls->L ); }
static void body (LexState *ls, expdesc *e, int needself, int line) { /* body -> `(' parlist `)' chunk END */ FuncState new_fs; open_func(ls, &new_fs); new_fs.f->lineDefined = line; check(ls, '('); if (needself) create_local(ls, "self"); parlist(ls); check(ls, ')'); chunk(ls); check_match(ls, TK_END, TK_FUNCTION, line); close_func(ls); pushclosure(ls, &new_fs, e); }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState funcstate; lexstate.buff = buff; TString *inputNameString = luaS_new(L, name); try { luaX_setinput(L, &lexstate, z, inputNameString ); open_func(&lexstate, &funcstate); try { funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); } catch( ... ) { close_func(&lexstate); // We do not need the proto anymore. funcstate.f->DereferenceGC( L ); throw; } close_func(&lexstate); // The result is funcstate.f, which is a referenced proto. } catch( ... ) { // When done parsing, we should clear up the string. inputNameString->DereferenceGC( L ); throw; } inputNameString->DereferenceGC( L ); lua_assert(funcstate.next == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.fsList.IsEmpty() == true); return funcstate.f; }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState funcstate; lexstate.buff = buff; luaX_setinput(L, &lexstate, z, luaS_new(L, name)); open_func(&lexstate, &funcstate); funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); close_func(&lexstate); lua_assert(funcstate.prev == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.fs == NULL); return funcstate.f; }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) { struct LexState lexstate; struct FuncState funcstate; lexstate.buff = buff; lexstate.nestlevel = 0; luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z))); open_func(&lexstate, &funcstate); next(&lexstate); /* read first token */ chunk(&lexstate); check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected"); close_func(&lexstate); lua_assert(funcstate.prev == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.nestlevel == 0); return funcstate.f; }
static void body (LexState *ls, expdesc *e, int needself, int line) { /* body -> `(' parlist `)' chunk END */ #ifdef LUA_UTILITIES_NET char szFuncName1[256]; char szFuncName2[256]; #endif FuncState new_fs; open_func(ls, &new_fs); new_fs.f->linedefined = line; #ifdef LUA_UTILITIES_NET szFuncName1[0] = 0; szFuncName2[0] = 0; TryGetFunctionName(ls->t.seminfo.ts, szFuncName1, 256); #endif checknext(ls, '('); if (needself) { new_localvarliteral(ls, "self", 0); adjustlocalvars(ls, 1); } parlist(ls); checknext(ls, ')'); chunk(ls); #ifdef LUA_UTILITIES_NET if (szFuncName1[0] == 0) TryGetFunctionName(ls->t.seminfo.ts, szFuncName2, 256); #endif new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); close_func(ls); #ifdef LUA_UTILITIES_NET // Use the correct function name based on values obtained if (szFuncName1[0] == 0) GetFunction(szFuncName2, ls, new_fs.f); else GetFunction(szFuncName1, ls, new_fs.f); #endif pushclosure(ls, &new_fs, e); }
static void body (LexState *ls, expdesc *e, int needself, int line) { /* body -> `(' parlist `)' chunk END */ FuncState new_fs; open_func(ls, &new_fs); new_fs.f->linedefined = line; checknext(ls, '('); if (needself) { new_localvarliteral(ls, "self", 0); adjustlocalvars(ls, 1); } parlist(ls); checknext(ls, ')'); chunk(ls); new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); close_func(ls); pushclosure(ls, &new_fs, e); }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState *pfuncstate = (struct FuncState*)malloc(sizeof(struct FuncState)); Proto *res; lexstate.buff = buff; luaX_setinput(L, &lexstate, z, luaS_new(L, name)); open_func(&lexstate, pfuncstate); pfuncstate->f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); close_func(&lexstate); lua_assert(pfuncstate->prev == NULL); lua_assert(pfuncstate->f->nups == 0); lua_assert(lexstate.fs == NULL); res = pfuncstate->f; free(pfuncstate); return res; }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState funcstate; TString *tname = luaS_new(L, name); setsvalue2s(L, L->top, tname); /* protect name */ incr_top(L); lexstate.buff = buff; luaX_setinput(L, &lexstate, z, tname); open_func(&lexstate, &funcstate); funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); close_func(&lexstate); L->top--; /* remove 'name' from stack */ lua_assert(funcstate.prev == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.fs == NULL); return funcstate.f; }
/** \brief Update widget with data from CTR image \a path * * \param[in] path path to CRT file */ void crt_preview_widget_update(const gchar *path) { FILE *fd; crt_header_t header; crt_chip_header_t chip; gchar buffer[1024]; #ifdef HAVE_DEBUG_GTK3UI int packets = 0; #endif /* * Guard against non C64/C128 carts * Once we implement CRT headers for VIC20 and others, this needs to be * removed. */ debug_gtk3("Got path '%s'.", path); if (machine_class != VICE_MACHINE_C64 && machine_class != VICE_MACHINE_C64SC && machine_class != VICE_MACHINE_C128) { debug_gtk3("Machine class != c64/c128, skipping."); return; } fd = open_func(path, &header); if (fd == NULL) { debug_gtk3("failed to open crt image"); gtk_label_set_text(GTK_LABEL(crtid_label), "<unknown>"); gtk_label_set_text(GTK_LABEL(crtname_label), "<unknown>"); gtk_label_set_text(GTK_LABEL(exrom_label), "<unknown>"); gtk_label_set_text(GTK_LABEL(game_label), "<unknown>"); return; } /* type */ g_snprintf(buffer, sizeof(buffer), "%d", (int)header.type); gtk_label_set_text(GTK_LABEL(crtid_label), buffer); /* name */ gtk_label_set_text(GTK_LABEL(crtname_label), header.name); /* exrom */ gtk_label_set_text(GTK_LABEL(exrom_label), romstate[header.exrom ? 1 : 0]); /* game */ gtk_label_set_text(GTK_LABEL(game_label), romstate[header.game ? 1 : 0]); /* clear CHIP packet table */ chip_packets_clear(); while (!feof(fd)) { long int pos; uint32_t skip; debug_gtk3("reading packet #%d.", packets++); if (chip_func(&chip, fd) != 0) { debug_gtk3("couldn't read further CHIP packets, exiting."); break; } skip = chip.size; debug_gtk3("chip packet contents:"); debug_gtk3(" skip = %lu", (long unsigned)chip.skip); debug_gtk3(" load = %u", chip.start); debug_gtk3(" size = %u", chip.size); debug_gtk3(" bank = %u", chip.bank); chip_packet_add(chip.type, chip.start, chip.size, chip.bank); pos = ftell(fd) + skip; debug_gtk3("next chip packet offset = %lx", (unsigned long)pos); if (fseek(fd, pos, SEEK_SET) != 0) { debug_gtk3("OEPS"); break; } } debug_gtk3("read %d CHIP packets.", packets); fclose(fd); }