void Processor::record_open() { frefid_t fref = glk_fileref_create_by_prompt(fileusage_Transcript, filemode_Write); if ((rfp = glk_stream_open_file(fref, filemode_Write)) != nullptr) ostream_record = true; else print_string("Cannot open file\n"); }
void Processor::replay_open() { frefid_t fref = glk_fileref_create_by_prompt(fileusage_Transcript, filemode_Read); if ((pfp = glk_stream_open_file(fref, filemode_Read)) != nullptr) istream_replay = true; else print_string("Cannot open file\n"); }
strid_t frotzopenprompt(int flag) { frefid_t fref; strid_t stm; glui32 gusage = flag2usage(flag); glui32 gmode = flag2mode(flag); fref = glk_fileref_create_by_prompt(gusage, gmode, 0); if (fref == NULL) return NULL; stm = glk_stream_open_file(fref, gmode, 0); if (flag == FILE_SCRIPT) { if (script_fref) glk_fileref_destroy(script_fref); script_fref = glk_fileref_create_from_fileref(gusage, fref, 0); script_exists = (script_fref != NULL); } glk_fileref_destroy(fref); return stm; }
static void SaveGame(void) { strid_t file; frefid_t ref; int ct; char buf[128]; ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame, filemode_Write, 0); if(ref == NULL) return; file = glk_stream_open_file(ref, filemode_Write, 0); glk_fileref_destroy(ref); if(file == NULL) return; for(ct=0;ct<16;ct++) { snprintf(buf, sizeof buf, "%d %d\n",Counters[ct],RoomSaved[ct]); glk_put_string_stream(file, buf); } snprintf(buf, sizeof buf, "%ld %d %hd %d %d %hd\n",BitFlags, (BitFlags&(1<<DARKBIT))?1:0, MyLoc,CurrentCounter,SavedRoom,GameHeader.LightTime); glk_put_string_stream(file, buf); for(ct=0;ct<=GameHeader.NumItems;ct++) { snprintf(buf, sizeof buf, "%hd\n",(short)Items[ct].Location); glk_put_string_stream(file, buf); } glk_stream_close(file, NULL); Output("Saved.\n"); }
static void LoadGame(void) { strid_t file; frefid_t ref; char buf[128]; int ct=0; short lo; short DarkFlag; ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame, filemode_Read, 0); if(ref == NULL) return; file = glk_stream_open_file(ref, filemode_Read, 0); glk_fileref_destroy(ref); if(file == NULL) return; for(ct=0;ct<16;ct++) { glk_get_line_stream(file, buf, sizeof buf); sscanf(buf, "%d %d",&Counters[ct],&RoomSaved[ct]); } glk_get_line_stream(file, buf, sizeof buf); sscanf(buf, "%ld %hd %hd %d %d %hd\n", &BitFlags,&DarkFlag,&MyLoc,&CurrentCounter,&SavedRoom, &GameHeader.LightTime); /* Backward compatibility */ if(DarkFlag) BitFlags|=(1<<15); for(ct=0;ct<=GameHeader.NumItems;ct++) { glk_get_line_stream(file, buf, sizeof buf); sscanf(buf, "%hd\n",&lo); Items[ct].Location=(unsigned char)lo; } }
void Processor::z_save() { bool success = false; if (zargc != 0) { // Open auxilary file frefid_t ref = glk_fileref_create_by_prompt(fileusage_Data | fileusage_BinaryMode, filemode_Write, 0); if (ref != nullptr) { // Write data strid_t f = glk_stream_open_file(ref, filemode_Write); glk_put_buffer_stream(f, (const char *)zmp + zargs[0], zargs[1]); glk_stream_close(f); success = true; } } else { success = saveGame().getCode() == Common::kNoError; } if (h_version <= V3) branch(success); else store(success); }
void Processor::z_restore() { bool success = false; if (zargc != 0) { frefid_t ref = glk_fileref_create_by_prompt(fileusage_Data | fileusage_BinaryMode, filemode_Read, 0); if (ref != nullptr) { // Write data strid_t f = glk_stream_open_file(ref, filemode_Read); glk_get_buffer_stream(f, (char *)zmp + zargs[0], zargs[1]); glk_stream_close(f); success = true; } } else { success = loadGame().getCode() == Common::kNoError; } int result = success ? 2 : -1; if (h_version <= V3) branch(result); else store(result); }
void profile_quit() { int bucknum; function_t *func; char linebuf[512]; frefid_t profref; strid_t profstr; while (current_frame) { profile_out(); } profref = glk_fileref_create_by_name(fileusage_BinaryMode|fileusage_Data, "profile-raw", 0); if (!profref) fatal_error("Profiler: unable to create profile-raw file"); profstr = glk_stream_open_file(profref, filemode_Write, 0); glk_put_string_stream(profstr, "<profile>\n"); for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++) { char total_buf[20], self_buf[20]; for (func = functions[bucknum]; func; func=func->hash_next) { /* ### sprintf(linebuf, "function %lx called %ld times, total ops %ld, total time %s, self ops %ld, self time %s\n", func->addr, func->call_count, func->total_ops, timeprint(&func->total_time, total_buf), func->self_ops, timeprint(&func->self_time, self_buf)); ### */ sprintf(linebuf, " <function addr=\"%lx\" call_count=\"%ld\" accel_count=\"%ld\" total_ops=\"%ld\" total_time=\"%s\" self_ops=\"%ld\" self_time=\"%s\" />\n", func->addr, func->call_count, func->accel_count, func->total_ops, timeprint(&func->total_time, total_buf), func->self_ops, timeprint(&func->self_time, self_buf)); glk_put_string_stream(profstr, linebuf); } } glk_put_string_stream(profstr, "</profile>\n"); glk_stream_close(profstr, NULL); glulx_free(functions); functions = NULL; }
strid_t frotzopen(char *filename, int flag) { frefid_t fref; strid_t stm; glui32 gusage = flag2usage(flag); glui32 gmode = flag2mode(flag); fref = glk_fileref_create_by_name(gusage, filename, 0); if (!fref) return NULL; stm = glk_stream_open_file(fref, gmode, 0); glk_fileref_destroy(fref); return stm; }
strid_t frotzreopen(int flag) { frefid_t fref; if (flag == FILE_SCRIPT && script_exists) fref = script_fref; else return NULL; strid_t stm; glui32 gusage = flag2usage(flag); glui32 gmode = flag2mode(flag); stm = glk_stream_open_file(fref, gmode, 0); return stm; }
/*======================================================================*/ void save(void) { #ifdef HAVE_GLK frefid_t saveFileRef; strid_t saveFile; saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0); if (saveFileRef == NULL) error(M_SAVEFAILED); saveFile = glk_stream_open_file(saveFileRef, filemode_Write, 0); #else FILE *saveFile; char str[256]; current.location = where(HERO, TRUE); /* First save ? */ if (saveFileName[0] == '\0') { strcpy(saveFileName, adventureName); strcat(saveFileName, ".sav"); } printMessage(M_SAVEWHERE); sprintf(str, "(%s) : ", saveFileName); output(str); #ifdef USE_READLINE readline(str); #else gets(str); #endif if (str[0] == '\0') strcpy(str, saveFileName); col = 1; if ((saveFile = fopen(str, READ_MODE)) != NULL) /* It already existed */ if (!confirm(M_SAVEOVERWRITE)) abortPlayerCommand(); /* Return to player without saying anything */ if ((saveFile = fopen(str, WRITE_MODE)) == NULL) error(M_SAVEFAILED); strcpy(saveFileName, str); #endif saveGame(saveFile); fclose(saveFile); }
/*======================================================================*/ void restore(void) { #ifdef HAVE_GLK frefid_t saveFileRef; strid_t saveFile; saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0); if (saveFileRef == NULL) return; saveFile = glk_stream_open_file(saveFileRef, filemode_Read, 0); if (saveFile == NULL) return; #else char str[1000]; FILE *saveFile; current.location = where(HERO, TRUE); /* First save ? */ if (saveFileName[0] == '\0') { strcpy(saveFileName, adventureName); strcat(saveFileName, ".sav"); } printMessage(M_RESTOREFROM); sprintf(str, "(%s) : ", saveFileName); output(str); #ifdef USE_READLINE readline(str); #else gets(str); #endif col = 1; if (str[0] == '\0') { strcpy(str, saveFileName); } if ((saveFile = fopen(str, READ_MODE)) == NULL) error(M_SAVEMISSING); strcpy(saveFileName, str); /* Save it for future use */ #endif restoreGame(saveFile); fclose(saveFile); }
void Processor::script_open() { h_flags &= ~SCRIPTING_FLAG; frefid_t fref = glk_fileref_create_by_prompt(fileusage_Transcript, filemode_WriteAppend); sfp = glk_stream_open_file(fref, filemode_WriteAppend); if (sfp != nullptr) { sfp->setPosition(0, seekmode_End); h_flags |= SCRIPTING_FLAG; script_valid = true; ostream_script = true; script_width = 0; } else { print_string("Cannot open file\n"); } SET_WORD(H_FLAGS, h_flags); }
long FindResource(char *filename, char *resname) { char resource_in_file[MAX_RES_PATH]; int i, len; int resfileversion, rescount; unsigned int startofdata; long resposition, reslength; #if defined (GLK) frefid_t fref; #endif /* Previously, resource positions were written as 24 bits, which meant that a given resource couldn't start after 16,777,216 bytes or be more than that length. The new resource file format (designated by 'r') corrects this. */ int res_32bits = true; resource_file = NULL; strcpy(loaded_filename, filename); strcpy(loaded_resname, resname); if (!strcmp(filename, "")) strcpy(loaded_filename, resname); /* See if the file is supposed to be in a resourcefile to begin with */ if (!strcmp(filename, "")) goto NotinResourceFile; /* Open the resourcefile */ strupr(filename); #if !defined (GLK) /* stdio implementation */ if (!(resource_file = TrytoOpen(filename, "rb", "games"))) if (!(resource_file = TrytoOpen(filename, "rb", "object"))) { var[system_status] = STAT_NOFILE; return 0; } #else /* Glk implementation */ fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, filename, 0); if (glk_fileref_does_file_exist(fref)) resource_file = glk_stream_open_file(fref, filemode_Read, 0); else resource_file = NULL; glk_fileref_destroy(fref); if (!resource_file) { var[system_status] = STAT_NOFILE; return 0; } #endif /* Read the resourcefile header */ /* if (fgetc(resource_file)!='R') goto ResfileError; */ i = fgetc(resource_file); if (i=='r') res_32bits = true; else if (i=='R') res_32bits = false; else goto ResfileError; resfileversion = fgetc(resource_file); rescount = fgetc(resource_file); rescount += fgetc(resource_file)*256; startofdata = fgetc(resource_file); startofdata += (unsigned int)fgetc(resource_file)*256; if (ferror(resource_file)) goto ResfileError; /* Now skim through the list of resources in the resourcefile to see if we have a match */ for (i=1; i<=rescount; i++) { len = fgetc(resource_file); if (ferror(resource_file)) goto ResfileError; if (!(fgets(resource_in_file, len+1, resource_file))) goto ResfileError; resposition = (long)fgetc(resource_file); resposition += (long)fgetc(resource_file)*256L; resposition += (long)fgetc(resource_file)*65536L; if (res_32bits) { resposition += (long)fgetc(resource_file)*16777216L; } reslength = (long)fgetc(resource_file); reslength += (long)fgetc(resource_file)*256L; reslength += (long)fgetc(resource_file)*65536L; if (res_32bits) { reslength += (long)fgetc(resource_file)*16777216L; } if (ferror(resource_file)) goto ResfileError; if (!strcmp(resname, resource_in_file)) { if (fseek(resource_file, (long)startofdata+resposition, SEEK_SET)) goto ResfileError; return reslength; } } ResfileError: var[system_status] = STAT_NORESOURCE; #if defined (DEBUGGER) SwitchtoDebugger(); sprintf(debug_line, "Unable to find \"%s\" in \"%s\"", resname, filename); DebugMessageBox("Resource Error", debug_line); SwitchtoGame(); #endif fclose(resource_file); resource_file = NULL; /* If we get here, we've either been unable to find the named resource in the given resourcefile, or no resourcefile was given */ NotinResourceFile: #if !defined (GLK) /* stdio implementation */ if (!(resource_file = TrytoOpen(resname, "rb", "resource"))) if (!(resource_file = TrytoOpen(resname, "rb", "source"))) { if (!strcmp(filename, "")) var[system_status] = STAT_NOFILE; else var[system_status] = STAT_NORESOURCE; return 0; } #else /* Glk implementation */ fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, resname, 0); if (glk_fileref_does_file_exist(fref)) resource_file = glk_stream_open_file(fref, filemode_Read, 0); else resource_file = NULL; glk_fileref_destroy(fref); if (!resource_file) { if (!strcmp(filename, "")) var[system_status] = STAT_NOFILE; else var[system_status] = STAT_NORESOURCE; return 0; } #endif /* resource_file here refers to a resource in an individual on-disk file, not a consolidated resource file */ fseek(resource_file, 0, SEEK_END); reslength = ftell(resource_file); fseek(resource_file, 0, SEEK_SET); if (ferror(resource_file)) { fclose(resource_file); resource_file = NULL; return false; } return reslength; }
void gidispatch_call(glui32 funcnum, glui32 numargs, gluniversal_t *arglist) { gidispatch_function_t *gidispatch_function; char *prototype; int argument = 0; int slot = 0; gidispatch_function = gidispatch_get_function_by_id(funcnum); prototype = gidispatch_prototype(funcnum); printf("DEBUG: dispatch call name=%s, prototype=%s, numargs=%u -- ", gidispatch_function->name, prototype, (unsigned int)numargs); if (strcmp(prototype, "4IuIuIuIs") == 0) { printf("%u, %u, %u, %d\n", arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); } else if (strcmp(prototype, "3IuIu:Iu") == 0) { printf("%u, %u, returning a glui32\n", arglist[0].uint, arglist[1].uint); } else if (strcmp(prototype, "3Qa<Iu:Qa") == 0) { printf("win at %p, outref to a glui32, returning a winid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3Qc<Iu:Qc") == 0) { printf("fileref at %p, outref to a glui32, returning a frefid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1Qa:") == 0) { printf("win at %p\n", arglist[0].opaqueref); } else if (strcmp(prototype, "6QaIuIuIuIu:Qa") == 0) { printf("win at %p, %u, %u, %u, %u, returning a winid_t\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4IuIuIuIs:") == 0) { printf("%u, %u, %u, %d\n", arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); } else if (strcmp(prototype, "1Qb:") == 0) { printf("stream at %p\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1Iu:") == 0) { printf("%u\n", arglist[0].uint); } else if (strcmp(prototype, "2Qb<[2IuIu]:") == 0) { printf("stream at %p, some struct stuff here\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3IuIuIu:") == 0) { printf("%u, %u, %u\n", arglist[0].uint, arglist[1].uint, arglist[2].uint); } else if (strcmp(prototype, "1:Qb") == 0) { printf("returning a strid_t\n"); } else if (strcmp(prototype, "4&+#!IuIuIu:Qb") == 0) { printf("retained, nonnull, array of glui32 at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "2Qc:Iu") == 0) { printf("fileref at %p, returning a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1<+[4IuQaIuIu]:") == 0) { printf("some struct stuff here, nonnull\n"); } else if (strcmp(prototype, "1:Qb") == 0) { printf("returning a strid_t\n"); } else if (strcmp(prototype, "2Qb:Is") == 0) { printf("stream at %p, returning a glsi32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "2Qc:Iu") == 0) { printf("fileref at %p, returning a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3Qa&+#!CnIu:") == 0) { printf("win at %p, retained, nonnull, array of char at %p for length %u, %u\n", arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "3Qb<Iu:Qb") == 0) { printf("stream at %p, outref to a glui32, returning a strid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "4&+#!CnIuIu:Qb") == 0) { printf("retained, nonnull, array of char at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4&+#!IuIuIu:Qb") == 0) { printf("retained, nonnull, array of glui32 at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4IuSIu:Qc") == 0) { printf("%u, %s, %u, returning a frefid_t\n", arglist[0].uint, arglist[1].charstr, arglist[2].uint); } else if (strcmp(prototype, "4QcIuIu:Qb") == 0) { printf("fileref at %p, %u, %u, returning a strid_t\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); } else if (strcmp(prototype, "3Qa<Iu<Iu:") == 0) { printf("win at %p, outref to a glui32, outref to a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "6QaIuIsIsIuIu:") == 0) { printf("win at %p, %u, %d, %d, %u, %u\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); } else if (strcmp(prototype, "4Iu<Iu<Iu:Iu") == 0) { printf("%u, outref to a glui32, outref to a glui32, returning a glui32\n", arglist[0].uint); } else { printf("unhandled prototype\n"); } switch (funcnum) { case 0x0004: /* gestalt */ arglist[3].uint = glk_gestalt(arglist[0].uint, arglist[1].uint); break; case 0x0005: /* gestalt_ext */ if (arglist[2].ptrflag) { arglist[6].uint = glk_gestalt_ext(arglist[0].uint, arglist[1].uint, arglist[3].array, arglist[4].uint); } else { arglist[4].uint = glk_gestalt_ext(arglist[0].uint, arglist[1].uint, NULL, 0); } break; case 0x0020: /* window_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_window_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_window_iterate(arglist[0].opaqueref, NULL); break; case 0x0023: /* window_open */ arglist[6].opaqueref = glk_window_open(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint, arglist[4].uint); break; case 0x0025: /* window_get_size */ { int ix = 1; glui32 *ptr1, *ptr2; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; glk_window_get_size(arglist[0].opaqueref, ptr1, ptr2); } break; case 0x0028: /* window_get_type */ arglist[2].uint = glk_window_get_type(arglist[0].opaqueref); break; case 0x002A: /* window_clear */ glk_window_clear(arglist[0].opaqueref); break; case 0x002B: /* window_move_cursor */ glk_window_move_cursor(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x002F: /* set_window */ glk_set_window(arglist[0].opaqueref); break; case 0x0040: /* stream_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_stream_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_stream_iterate(arglist[0].opaqueref, NULL); break; case 0x0042: /* stream_open_file */ arglist[4].opaqueref = glk_stream_open_file(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x0043: /* stream_open_memory */ if (arglist[0].ptrflag) arglist[6].opaqueref = glk_stream_open_memory(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].opaqueref = glk_stream_open_memory(NULL, 0, arglist[1].uint, arglist[2].uint); break; case 0x0044: /* stream_close */ if (arglist[1].ptrflag) { stream_result_t dat; glk_stream_close(arglist[0].opaqueref, &dat); arglist[2].uint = dat.readcount; arglist[3].uint = dat.writecount; } else { glk_stream_close(arglist[0].opaqueref, NULL); } break; case 0x0047: /* stream_set_current */ glk_stream_set_current(arglist[0].opaqueref); break; case 0x0048: /* stream_get_current */ arglist[1].opaqueref = glk_stream_get_current(); break; case 0x0061: /* fileref_create_by_name */ arglist[4].opaqueref = glk_fileref_create_by_name(arglist[0].uint, arglist[1].charstr, arglist[2].uint); break; case 0x0062: /* fileref_create_by_prompt */ arglist[4].opaqueref = glk_fileref_create_by_prompt(arglist[0].uint, arglist[1].uint, arglist[2].uint); break; case 0x0067: /* fileref_does_file_exist */ arglist[2].uint = glk_fileref_does_file_exist(arglist[0].opaqueref); break; case 0x0086: /* set_style */ glk_set_style(arglist[0].uint); break; case 0x0087: /* set_style_stream */ glk_set_style_stream(arglist[0].opaqueref, arglist[1].uint); break; case 0x0090: /* get_char_stream */ arglist[2].sint = glk_get_char_stream(arglist[0].opaqueref); break; case 0x00B0: /* stylehint_set */ glk_stylehint_set(arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); break; case 0x00C0: /* select */ if (arglist[0].ptrflag) { event_t dat; glk_select(&dat); arglist[1].uint = dat.type; arglist[2].opaqueref = dat.win; arglist[3].uint = dat.val1; arglist[4].uint = dat.val2; } else { glk_select(NULL); } break; case 0x00D0: /* request_line_event */ if (arglist[1].ptrflag) glk_request_line_event(arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); else glk_request_line_event(arglist[0].opaqueref, NULL, 0, arglist[2].uint); break; case 0x00D2: /* request_char_event */ glk_request_char_event(arglist[0].opaqueref); break; case 0x00E0: /* image_get_info */ { int ix = 1; glui32 *ptr1, *ptr2; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; ix++; arglist[ix].uint = glk_image_get_info(arglist[0].uint, ptr1, ptr2); } break; case 0x00E1: /* image_draw */ arglist[5].uint = glk_image_draw(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint); break; case 0x00E2: /* image_draw_scaled */ arglist[7].uint = glk_image_draw_scaled(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); break; case 0x00EA: /* window_fill_rect */ glk_window_fill_rect(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); break; case 0x0128: /* put_char_uni */ glk_put_char_uni(arglist[0].uint); break; case 0x0139: /* stream_open_memory_uni */ if (arglist[0].ptrflag) arglist[6].opaqueref = glk_stream_open_memory_uni(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].opaqueref = glk_stream_open_memory_uni(NULL, 0, arglist[1].uint, arglist[2].uint); break; default: printf("Unhandled call to %s via dispatch\n", gidispatch_function->name); #if 0 case 0x0001: /* exit */ glk_exit(); break; case 0x0002: /* set_interrupt_handler */ /* cannot be invoked through dispatch layer */ break; case 0x0003: /* tick */ glk_tick(); break; case 0x0021: /* window_get_rock */ arglist[2].uint = glk_window_get_rock(arglist[0].opaqueref); break; case 0x0022: /* window_get_root */ arglist[1].opaqueref = glk_window_get_root(); break; case 0x0024: /* window_close */ if (arglist[1].ptrflag) { stream_result_t dat; glk_window_close(arglist[0].opaqueref, &dat); arglist[2].uint = dat.readcount; arglist[3].uint = dat.writecount; } else { glk_window_close(arglist[0].opaqueref, NULL); } break; case 0x0026: /* window_set_arrangement */ glk_window_set_arrangement(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].opaqueref); break; case 0x0027: /* window_get_arrangement */ { int ix = 1; glui32 *ptr1, *ptr2; winid_t *ptr3; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr3 = NULL; } else { ix++; ptr3 = (winid_t *)(&(arglist[ix].opaqueref)); } ix++; glk_window_get_arrangement(arglist[0].opaqueref, ptr1, ptr2, ptr3); } break; case 0x0029: /* window_get_parent */ arglist[2].opaqueref = glk_window_get_parent(arglist[0].opaqueref); break; case 0x002C: /* window_get_stream */ arglist[2].opaqueref = glk_window_get_stream(arglist[0].opaqueref); break; case 0x002D: /* window_set_echo_stream */ glk_window_set_echo_stream(arglist[0].opaqueref, arglist[1].opaqueref); break; case 0x002E: /* window_get_echo_stream */ arglist[2].opaqueref = glk_window_get_echo_stream(arglist[0].opaqueref); break; case 0x0030: /* window_get_sibling */ arglist[2].opaqueref = glk_window_get_sibling(arglist[0].opaqueref); break; case 0x0041: /* stream_get_rock */ arglist[2].uint = glk_stream_get_rock(arglist[0].opaqueref); break; case 0x0045: /* stream_set_position */ glk_stream_set_position(arglist[0].opaqueref, arglist[1].sint, arglist[2].uint); break; case 0x0046: /* stream_get_position */ arglist[2].uint = glk_stream_get_position(arglist[0].opaqueref); break; case 0x0048: /* stream_get_current */ arglist[1].opaqueref = glk_stream_get_current(); break; case 0x0060: /* fileref_create_temp */ arglist[3].opaqueref = glk_fileref_create_temp(arglist[0].uint, arglist[1].uint); break; case 0x0063: /* fileref_destroy */ glk_fileref_destroy(arglist[0].opaqueref); break; case 0x0064: /* fileref_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_fileref_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_fileref_iterate(arglist[0].opaqueref, NULL); break; case 0x0065: /* fileref_get_rock */ arglist[2].uint = glk_fileref_get_rock(arglist[0].opaqueref); break; case 0x0066: /* fileref_delete_file */ glk_fileref_delete_file(arglist[0].opaqueref); break; case 0x0068: /* fileref_create_from_fileref */ arglist[4].opaqueref = glk_fileref_create_from_fileref(arglist[0].uint, arglist[1].opaqueref, arglist[2].uint); break; case 0x0080: /* put_char */ glk_put_char(arglist[0].uch); break; case 0x0081: /* put_char_stream */ glk_put_char_stream(arglist[0].opaqueref, arglist[1].uch); break; case 0x0082: /* put_string */ glk_put_string(arglist[0].charstr); break; case 0x0083: /* put_string_stream */ glk_put_string_stream(arglist[0].opaqueref, arglist[1].charstr); break; case 0x0084: /* put_buffer */ if (arglist[0].ptrflag) glk_put_buffer(arglist[1].array, arglist[2].uint); else glk_put_buffer(NULL, 0); break; case 0x0085: /* put_buffer_stream */ if (arglist[1].ptrflag) glk_put_buffer_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else glk_put_buffer_stream(arglist[0].opaqueref, NULL, 0); break; case 0x0091: /* get_line_stream */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_line_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_line_stream(arglist[0].opaqueref, NULL, 0); break; case 0x0092: /* get_buffer_stream */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_buffer_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_buffer_stream(arglist[0].opaqueref, NULL, 0); break; case 0x00A0: /* char_to_lower */ arglist[2].uch = glk_char_to_lower(arglist[0].uch); break; case 0x00A1: /* char_to_upper */ arglist[2].uch = glk_char_to_upper(arglist[0].uch); break; case 0x00B1: /* stylehint_clear */ glk_stylehint_clear(arglist[0].uint, arglist[1].uint, arglist[2].uint); break; case 0x00B2: /* style_distinguish */ arglist[4].uint = glk_style_distinguish(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x00B3: /* style_measure */ if (arglist[3].ptrflag) arglist[6].uint = glk_style_measure(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, &(arglist[4].uint)); else arglist[5].uint = glk_style_measure(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, NULL); break; case 0x00C1: /* select_poll */ if (arglist[0].ptrflag) { event_t dat; glk_select_poll(&dat); arglist[1].uint = dat.type; arglist[2].opaqueref = dat.win; arglist[3].uint = dat.val1; arglist[4].uint = dat.val2; } else { glk_select_poll(NULL); } break; case 0x00D1: /* cancel_line_event */ if (arglist[1].ptrflag) { event_t dat; glk_cancel_line_event(arglist[0].opaqueref, &dat); arglist[2].uint = dat.type; arglist[3].opaqueref = dat.win; arglist[4].uint = dat.val1; arglist[5].uint = dat.val2; } else { glk_cancel_line_event(arglist[0].opaqueref, NULL); } break; case 0x00D3: /* cancel_char_event */ glk_cancel_char_event(arglist[0].opaqueref); break; case 0x00D4: /* request_mouse_event */ glk_request_mouse_event(arglist[0].opaqueref); break; case 0x00D5: /* cancel_mouse_event */ glk_cancel_mouse_event(arglist[0].opaqueref); break; case 0x00D6: /* request_timer_events */ glk_request_timer_events(arglist[0].uint); break; #ifdef GLK_MODULE_IMAGE case 0x00E8: /* window_flow_break */ glk_window_flow_break(arglist[0].opaqueref); break; case 0x00E9: /* window_erase_rect */ glk_window_erase_rect(arglist[0].opaqueref, arglist[1].sint, arglist[2].sint, arglist[3].uint, arglist[4].uint); break; case 0x00EB: /* window_set_background_color */ glk_window_set_background_color(arglist[0].opaqueref, arglist[1].uint); break; #endif /* GLK_MODULE_IMAGE */ #ifdef GLK_MODULE_SOUND case 0x00F0: /* schannel_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_schannel_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_schannel_iterate(arglist[0].opaqueref, NULL); break; case 0x00F1: /* schannel_get_rock */ arglist[2].uint = glk_schannel_get_rock(arglist[0].opaqueref); break; case 0x00F2: /* schannel_create */ arglist[2].opaqueref = glk_schannel_create(arglist[0].uint); break; case 0x00F3: /* schannel_destroy */ glk_schannel_destroy(arglist[0].opaqueref); break; case 0x00F8: /* schannel_play */ arglist[3].uint = glk_schannel_play(arglist[0].opaqueref, arglist[1].uint); break; case 0x00F9: /* schannel_play_ext */ arglist[5].uint = glk_schannel_play_ext(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint); break; case 0x00FA: /* schannel_stop */ glk_schannel_stop(arglist[0].opaqueref); break; case 0x00FB: /* schannel_set_volume */ glk_schannel_set_volume(arglist[0].opaqueref, arglist[1].uint); break; case 0x00FC: /* sound_load_hint */ glk_sound_load_hint(arglist[0].uint, arglist[1].uint); break; #endif /* GLK_MODULE_SOUND */ #ifdef GLK_MODULE_HYPERLINKS case 0x0100: /* set_hyperlink */ glk_set_hyperlink(arglist[0].uint); break; case 0x0101: /* set_hyperlink_stream */ glk_set_hyperlink_stream(arglist[0].opaqueref, arglist[1].uint); break; case 0x0102: /* request_hyperlink_event */ glk_request_hyperlink_event(arglist[0].opaqueref); break; case 0x0103: /* cancel_hyperlink_event */ glk_cancel_hyperlink_event(arglist[0].opaqueref); break; #endif /* GLK_MODULE_HYPERLINKS */ #ifdef GLK_MODULE_UNICODE case 0x0120: /* buffer_to_lower_case_uni */ if (arglist[0].ptrflag) arglist[5].uint = glk_buffer_to_lower_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint); else arglist[3].uint = glk_buffer_to_lower_case_uni(NULL, 0, arglist[1].uint); break; case 0x0121: /* buffer_to_upper_case_uni */ if (arglist[0].ptrflag) arglist[5].uint = glk_buffer_to_upper_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint); else arglist[3].uint = glk_buffer_to_upper_case_uni(NULL, 0, arglist[1].uint); break; case 0x0122: /* buffer_to_title_case_uni */ if (arglist[0].ptrflag) arglist[6].uint = glk_buffer_to_title_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].uint = glk_buffer_to_title_case_uni(NULL, 0, arglist[1].uint, arglist[2].uint); break; case 0x0129: /* put_string_uni */ glk_put_string_uni(arglist[0].unicharstr); break; case 0x012A: /* put_buffer_uni */ if (arglist[0].ptrflag) glk_put_buffer_uni(arglist[1].array, arglist[2].uint); else glk_put_buffer_uni(NULL, 0); break; case 0x012B: /* put_char_stream_uni */ glk_put_char_stream_uni(arglist[0].opaqueref, arglist[1].uint); break; case 0x012C: /* put_string_stream_uni */ glk_put_string_stream_uni(arglist[0].opaqueref, arglist[1].unicharstr); break; case 0x012D: /* put_buffer_stream_uni */ if (arglist[1].ptrflag) glk_put_buffer_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else glk_put_buffer_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0130: /* get_char_stream_uni */ arglist[2].sint = glk_get_char_stream_uni(arglist[0].opaqueref); break; case 0x0131: /* get_buffer_stream_uni */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_buffer_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_buffer_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0132: /* get_line_stream_uni */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_line_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_line_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0138: /* stream_open_file_uni */ arglist[4].opaqueref = glk_stream_open_file_uni(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x0140: /* request_char_event_uni */ glk_request_char_event_uni(arglist[0].opaqueref); break; case 0x0141: /* request_line_event_uni */ if (arglist[1].ptrflag) glk_request_line_event_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); else glk_request_line_event_uni(arglist[0].opaqueref, NULL, 0, arglist[2].uint); break; #endif /* GLK_MODULE_UNICODE */ #endif /* 0 */ } }
static z_file *glkint_openfile(char *filename, int filetype, int fileaccess) { frefid_t fileref = NULL; strid_t str = NULL; glui32 usage, fmode; z_file *result; int implementation = -1; TRACE_LOG("openfile: %s\n", filename); if (filename == NULL) return NULL; if (filetype == FILETYPE_SAVEGAME) { usage = fileusage_SavedGame | fileusage_BinaryMode; implementation = FILE_IMPLEMENTATION_GLK; } else if (filetype == FILETYPE_TRANSCRIPT) { usage = fileusage_Transcript | fileusage_TextMode; implementation = FILE_IMPLEMENTATION_GLK; } else if (filetype == FILETYPE_INPUTRECORD) { usage = fileusage_InputRecord | fileusage_TextMode; implementation = FILE_IMPLEMENTATION_GLK; } else if (filetype == FILETYPE_DATA) { usage = fileusage_Data | fileusage_BinaryMode; implementation = FILE_IMPLEMENTATION_STDIO; } else if (filetype == FILETYPE_TEXT) { usage = fileusage_Data | fileusage_TextMode; implementation = FILE_IMPLEMENTATION_STDIO; } else return NULL; if (implementation == FILE_IMPLEMENTATION_GLK) { if (fileaccess == FILEACCESS_READ) fmode = filemode_Read; else if (fileaccess == FILEACCESS_WRITE) fmode = filemode_Write; else if (fileaccess == FILEACCESS_APPEND) fmode = filemode_WriteAppend; else return NULL; fileref = glk_fileref_create_by_name(usage, filename, 0); if (!fileref) return NULL; TRACE_LOG("new open file: %s\n", filename); str = glk_stream_open_file(fileref, fmode, 2); /* Dispose of the fileref, whether the stream opened successfully * or not. */ glk_fileref_destroy(fileref); if (!str) { TRACE_LOG("Couldn't GLK-open: %s\n", filename); return NULL; } if ((result = malloc(sizeof(z_file))) == NULL) return NULL; result = zfile_from_glk_strid(str, filename, filetype, fileaccess); } else if (implementation == FILE_IMPLEMENTATION_STDIO) { if ((result = z_filesys_interface_c.openfile( filename, filetype, fileaccess)) != NULL) result->implementation = FILE_IMPLEMENTATION_STDIO; } else return NULL; return result; }
void profile_quit() { int bucknum; function_t *func; char linebuf[512]; strid_t profstr; if (!profiling_active) return; while (current_frame) { profile_out(0); } if (profiling_stream) { profstr = profiling_stream; } else if (profiling_filename) { frefid_t profref = glk_fileref_create_by_name(fileusage_BinaryMode|fileusage_Data, profiling_filename, 0); if (!profref) fatal_error_2("Profiler: unable to create profile output fileref", profiling_filename); profstr = glk_stream_open_file(profref, filemode_Write, 0); } else { fatal_error("Profiler: no profile output handle!"); } glk_put_string_stream(profstr, "<profile>\n"); for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++) { char total_buf[20], self_buf[20]; callcount_t *cc; for (func = functions[bucknum]; func; func=func->hash_next) { /* ### sprintf(linebuf, "function %lx called %ld times, total ops %ld, total time %s, self ops %ld, self time %s\n", func->addr, func->call_count, func->total_ops, timeprint(&func->total_time, total_buf), func->self_ops, timeprint(&func->self_time, self_buf)); ### */ sprintf(linebuf, " <function addr=\"%lx\" call_count=\"%ld\" accel_count=\"%ld\" total_ops=\"%ld\" total_time=\"%s\" self_ops=\"%ld\" self_time=\"%s\" max_depth=\"%ld\" max_stack_use=\"%ld\" />\n", (unsigned long)func->addr, (long)func->call_count, (long)func->accel_count, (long)func->total_ops, timeprint(&func->total_time, total_buf), (long)func->self_ops, timeprint(&func->self_time, self_buf), (long)func->max_depth, (long)func->max_stack_use); glk_put_string_stream(profstr, linebuf); for (cc = func->outcalls; cc; cc = cc->next) { sprintf(linebuf, " <calls fromaddr=\"%lx\" toaddr=\"%lx\" count=\"%ld\" />\n", (unsigned long)func->addr, (unsigned long)cc->toaddr, (long)cc->count); glk_put_string_stream(profstr, linebuf); } } } glk_put_string_stream(profstr, "</profile>\n"); glk_stream_close(profstr, NULL); /* ### Ought to free the function structures, not just the hash array. */ glulx_free(functions); functions = NULL; }