static int write_transition(int32 from, int32 to, int32 prob, int32 opdf_index, FILE *fp) { fwrite_long (fp, from); fwrite_long (fp, to); fwrite_long (fp, prob); fwrite_long (fp, opdf_index); return S3_SUCCESS; }
static void gtm_wr_init(const char *fname) { rt_count = tr_count = 0; track_disp_all(NULL, NULL, count_track_waypts); route_disp_all(NULL, NULL, count_route_waypts); file_out = gbfopen_le(fname, "wb", MYNAME); /* little endian */ /* Header */ fwrite_integer(file_out, 211); fwrite_fixedstring(file_out, "TrackMaker", 10); fwrite_byte(file_out, 0); fwrite_byte(file_out, 0); fwrite_byte(file_out, 8); fwrite_byte(file_out, 0); fwrite_byte(file_out, 0); fwrite_byte(file_out, 0); fwrite_byte(file_out, 0); fwrite_long(file_out, 0); fwrite_long(file_out, 16777215); fwrite_long(file_out, waypt_count() ? 4 : 0); /* num waypoint styles */ fwrite_long(file_out, 0); fwrite_long(file_out, waypt_count()); /* num waypoints */ fwrite_long(file_out, tr_count); fwrite_long(file_out, rt_count); fwrite_single(file_out, 0); /* maxlon */ fwrite_single(file_out, 0); /* minlon */ fwrite_single(file_out, 0); /* maxlat */ fwrite_single(file_out, 0); /* minlat */ fwrite_long(file_out, 0); fwrite_long(file_out, track_count()); /* num tracklog styles */ fwrite_single(file_out, 0); fwrite_single(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_bool(file_out, 0); fwrite_string(file_out, "Times New Roman"); fwrite_string(file_out, ""); fwrite_string(file_out, ""); fwrite_string(file_out, ""); /* User Grid and Datum */ fwrite_null(file_out, 34); fwrite_integer(file_out, 217); /* WGS84 */ fwrite_null(file_out, 22); }
static void write_trk_style(const route_head *trk) { fwrite_string(file_out, trk->rte_name); fwrite_byte(file_out, 1); fwrite_long(file_out, 0); fwrite_single(file_out, 0); fwrite_byte(file_out, 0); fwrite_integer(file_out, 0); }
static int write_sdm(float32 **tmat, FILE *fp) { int32 n_arc; fwrite_long (fp, TIED_DIST); /* what type of file it is (trans only) */ /* might also be BIG_HMM */ fwrite_long (fp, S2_N_CODEWORD); /* number of codewords??? */ fwrite_long (fp, S2_N_STATE-1); /* n_omatrix */ /* * Output pdfs would go here, but instead go in *.{ccode,xcode,...} */ fwrite_long (fp, S2_N_STATE); /* n_states */ fwrite_long (fp, 1); /* n_initial (states) */ fwrite_long (fp, 0); /* list of initial states {0} */ fwrite_long (fp, 1); /* n_final */ fwrite_long (fp, (S2_N_STATE-1)); /* list of final states {S2_N_STATE-1} */ n_arc = 14; fwrite_long (fp, n_arc); /* number of arcs */ /* * Dump these in the same order as s2 did. */ write_transition (0, 0, LOG(tmat[0][0]), 0, fp); write_transition (0, 1, LOG(tmat[0][1]), 0, fp); write_transition (1, 1, LOG(tmat[1][1]), 1, fp); write_transition (1, 2, LOG(tmat[1][2]), 1, fp); write_transition (2, 2, LOG(tmat[2][2]), 2, fp); write_transition (2, 3, LOG(tmat[2][3]), 2, fp); write_transition (3, 3, LOG(tmat[3][3]), 3, fp); write_transition (3, 4, LOG(tmat[3][4]), 3, fp); write_transition (4, 4, LOG(tmat[4][4]), 4, fp); write_transition (4, 5, LOG(tmat[4][5]), 4, fp); write_transition (0, 2, LOG(tmat[0][2]), 0, fp); write_transition (1, 3, LOG(tmat[1][3]), 1, fp); write_transition (2, 4, LOG(tmat[2][4]), 2, fp); write_transition (3, 5, LOG(tmat[3][5]), 3, fp); return S3_SUCCESS; }
static void write_waypt(const waypoint *wpt) { fwrite_double(file_out, wpt->latitude); fwrite_double(file_out, wpt->longitude); fwrite_fixedstring(file_out, wpt->shortname, 10); fwrite_string(file_out, wpt->description); fwrite_integer(file_out, icon_from_descr(wpt->icon_descr)); fwrite_byte(file_out, 3); if (wpt->creation_time) fwrite_long(file_out, wpt->creation_time-EPOCH89DIFF); else fwrite_long(file_out, 0); fwrite_integer(file_out, 0); if (wpt->altitude == unknown_alt) fwrite_single(file_out, unknown_alt_gtm); else fwrite_single(file_out, wpt->altitude); fwrite_integer(file_out, 0); }
/** * ti9x_file_write_backup: * @filename: name of backup file where to write. * @content: the file content to write. * * Write content to a backup file. * * Return value: an error code, 0 otherwise. **/ int ti9x_file_write_backup(const char *filename, Ti9xBackup *content) { FILE *f; if (filename == NULL || content == NULL) { tifiles_critical("%s: an argument is NULL", __FUNCTION__); return ERR_INVALID_FILE; } f = g_fopen(filename, "wb"); if (f == NULL) { tifiles_info("Unable to open this file: %s", filename); return ERR_FILE_OPEN; } if (fwrite_8_chars(f, tifiles_calctype2signature(content->model)) < 0) goto tfwb; if (fwrite(fsignature, 1, 2, f) < 2) goto tfwb; if (fwrite_8_chars(f, "") < 0) goto tfwb; if (fwrite_n_bytes(f, 40, (uint8_t *)content->comment) < 0) goto tfwb; if (fwrite_word(f, 1) < 0) goto tfwb; if (fwrite_long(f, 0x52) < 0) goto tfwb; if (fwrite_8_chars(f, content->rom_version) < 0) goto tfwb; if (fwrite_word(f, content->type) < 0) goto tfwb; if (fwrite_word(f, 0) < 0) goto tfwb; if (fwrite_long(f, content->data_length + 0x52 + 2) < 0) goto tfwb; if (fwrite_word(f, 0x5aa5) < 0) goto tfwb; if (fwrite(content->data_part, 1, content->data_length, f) < content->data_length) goto tfwb; content->checksum = tifiles_checksum(content->data_part, content->data_length); if (fwrite_word(f, content->checksum) < 0) goto tfwb; fclose(f); return 0; tfwb: // release on exit tifiles_critical("%s: error writing file %s", __FUNCTION__, filename); fclose(f); return ERR_FILE_IO; }
static void write_trk_waypt(const waypoint *wpt) { fwrite_double(file_out, wpt->latitude); fwrite_double(file_out, wpt->longitude); fwrite_long(file_out, wpt->creation_time-EPOCH89DIFF); fwrite_byte(file_out, start_new); if (wpt->altitude == unknown_alt) fwrite_single(file_out, unknown_alt_gtm); else fwrite_single(file_out, wpt->altitude); start_new = 0; }
static void write_rte_waypt(const waypoint *wpt) { fwrite_double(file_out, wpt->latitude); fwrite_double(file_out, wpt->longitude); fwrite_fixedstring(file_out, wpt->shortname, 10); fwrite_string(file_out, wpt->description); fwrite_string(file_out, rte_active->rte_name); fwrite_integer(file_out, icon_from_descr(wpt->icon_descr)); fwrite_byte(file_out, 3); fwrite_byte(file_out, start_new); fwrite_long(file_out, 0); fwrite_integer(file_out, 0); if (wpt->altitude == unknown_alt) fwrite_single(file_out, unknown_alt_gtm); else fwrite_single(file_out, wpt->altitude); fwrite_integer(file_out, 0); start_new = 0; }
/** * ti9x_file_write_flash: * @filename: name of flash file where to write. * @content: the file content to write. * * Write content to a flash file (os or app). * * Return value: an error code, 0 otherwise. **/ int ti9x_file_write_flash(const char *fname, Ti9xFlash *head, char **real_fname) { FILE *f; Ti9xFlash *content = head; char *filename; if (head == NULL) { tifiles_critical("%s: head is NULL", __FUNCTION__); return ERR_INVALID_FILE; } if (fname) { filename = g_strdup(fname); if (filename == NULL) { return ERR_MALLOC; } } else { VarEntry ve; for (content = head; content != NULL; content = content->next) { if (content->data_type == TI89_AMS || content->data_type == TI89_APPL) { break; } } if (content == NULL) { tifiles_critical("%s: content is NULL", __FUNCTION__); return ERR_BAD_FILE; } strncpy(ve.name, content->name, sizeof(ve.name) - 1); ve.name[sizeof(ve.name) - 1] = 0; ve.type = content->data_type; filename = tifiles_build_filename(content->model, &ve); if (real_fname != NULL) { *real_fname = g_strdup(filename); } } f = g_fopen(filename, "wb"); if (f == NULL) { tifiles_info("Unable to open this file: %s", filename); g_free(filename); return ERR_FILE_OPEN; } for (content = head; content != NULL; content = content->next) { if (fwrite_8_chars(f, "**TIFL**") < 0) goto tfwf; if (fwrite_byte(f, content->revision_major) < 0) goto tfwf; if (fwrite_byte(f, content->revision_minor) < 0) goto tfwf; if (fwrite_byte(f, content->flags) < 0) goto tfwf; if (fwrite_byte(f, content->object_type) < 0) goto tfwf; if (fwrite_byte(f, content->revision_day) < 0) goto tfwf; if (fwrite_byte(f, content->revision_month) < 0) goto tfwf; if (fwrite_word(f, content->revision_year) < 0) goto tfwf; if (fwrite_byte(f, (uint8_t) strlen(content->name)) < 0) goto tfwf; if (fwrite_8_chars(f, content->name) < 0) goto tfwf; if (fwrite_n_chars(f, 23, "") < 0) goto tfwf; if (fwrite_byte(f, content->device_type) < 0) goto tfwf; if (fwrite_byte(f, content->data_type) < 0) goto tfwf; if (fwrite_n_chars(f, 23, "") < 0) goto tfwf; if (fwrite_byte(f, content->hw_id) < 0) goto tfwf; if (fwrite_long(f, content->data_length) < 0) goto tfwf; if (fwrite(content->data_part, 1, content->data_length, f) < content->data_length) goto tfwf; } g_free(filename); fclose(f); return 0; tfwf: // release on exit tifiles_critical("%s: error writing file %s", __FUNCTION__, filename); g_free(filename); fclose(f); return ERR_FILE_IO; }
/** * ti9x_file_write_regular: * @filename: name of single/group file where to write or NULL. * @content: the file content to write. * @real_filename: pointer address or NULL. Must be freed if needed when no longer needed. * * Write one (or several) variable(s) into a single (group) file. If filename is set to NULL, * the function build a filename from varname and allocates resulting filename in %real_fname. * %filename and %real_filename can be NULL but not both ! * * %real_filename must be freed when no longer used. * * Return value: an error code, 0 otherwise. **/ int ti9x_file_write_regular(const char *fname, Ti9xRegular *content, char **real_fname) { FILE *f; unsigned int i; char *filename = NULL; uint32_t offset = 0x52; int **table; unsigned int num_folders; char default_folder[FLDNAME_MAX]; char fldname[FLDNAME_MAX], varname[VARNAME_MAX]; if (content->entries == NULL) { tifiles_warning("%s: skipping content with NULL content->entries", __FUNCTION__); return 0; } if (fname != NULL) { filename = g_strdup(fname); if (filename == NULL) { return ERR_MALLOC; } } else { if (content->entries[0]) { filename = tifiles_build_filename(content->model_dst, content->entries[0]); } else { tifiles_warning("%s: asked to build a filename from null content->entries[0], bailing out", __FUNCTION__); if (real_fname != NULL) { *real_fname = NULL; } return 0; } if (real_fname != NULL) { *real_fname = g_strdup(filename); } } // build the table of folder & variable entries table = tifiles_create_table_of_entries((FileContent *)content, &num_folders); if (table == NULL) { g_free(filename); return ERR_MALLOC; } f = g_fopen(filename, "wb"); if (f == NULL) { tifiles_info( "Unable to open this file: %s", filename); tifiles_free_table_of_entries(table); g_free(filename); return ERR_FILE_OPEN; } // write header if (fwrite_8_chars(f, tifiles_calctype2signature(content->model)) < 0) goto tfwr; if (fwrite(fsignature, 1, 2, f) < 2) goto tfwr; if (content->num_entries == 1) // folder entry for single var is placed here { strncpy(content->default_folder, content->entries[0]->folder, sizeof(content->default_folder) - 1); content->default_folder[sizeof(content->default_folder) - 1] = 0; } ticonv_varname_to_tifile_sn(content->model, content->default_folder, default_folder, sizeof(default_folder), -1); if (fwrite_8_chars(f, default_folder) < 0) goto tfwr; if (fwrite_n_bytes(f, 40, (uint8_t *)content->comment) < 0) goto tfwr; if (content->num_entries > 1) { if (fwrite_word(f, (uint16_t) (content->num_entries + num_folders)) < 0) goto tfwr; offset += 16 * (content->num_entries + num_folders - 1); } else { if (fwrite_word(f, 1) < 0) goto tfwr; } // write table of entries for (i = 0; table[i] != NULL; i++) { VarEntry *fentry; int j, idx = table[i][0]; fentry = content->entries[idx]; if (fentry == NULL) { tifiles_warning("%s: skipping null content entry %d", __FUNCTION__, i); continue; } if (content->num_entries > 1) // single var does not have folder entry { if (fwrite_long(f, offset) < 0) goto tfwr; ticonv_varname_to_tifile_sn(content->model, fentry->folder, fldname, sizeof(fldname), -1); if (fwrite_8_chars(f, fldname) < 0) goto tfwr; if (fwrite_byte(f, (uint8_t)tifiles_folder_type(content->model)) < 0) goto tfwr; if (fwrite_byte(f, 0x00) < 0) goto tfwr; for (j = 0; table[i][j] != -1; j++); if (fwrite_word(f, (uint16_t) j) < 0) goto tfwr; } for (j = 0; table[i][j] != -1; j++) { int idx2 = table[i][j]; VarEntry *entry = content->entries[idx2]; uint8_t attr = ATTRB_NONE; if (fwrite_long(f, offset) < 0) goto tfwr; ticonv_varname_to_tifile_sn(content->model, entry->name, varname, sizeof(varname), entry->type); if (fwrite_8_chars(f, varname) < 0) goto tfwr; if (fwrite_byte(f, entry->type) < 0) goto tfwr; attr = (entry->attr == ATTRB_ARCHIVED) ? 3 : entry->attr; if (fwrite_byte(f, attr) < 0) goto tfwr; if (fwrite_word(f, 0) < 0) goto tfwr; offset += entry->size + 4 + 2; } } if (fwrite_long(f, offset) < 0) goto tfwr; if (fwrite_word(f, 0x5aa5) < 0) goto tfwr; // write data for (i = 0; table[i] != NULL; i++) { int j; for (j = 0; table[i][j] != -1; j++) { int idx = table[i][j]; VarEntry *entry = content->entries[idx]; uint16_t sum; if (fwrite_long(f, 0) < 0) goto tfwr; if (fwrite(entry->data, 1, entry->size, f) < entry->size) goto tfwr; sum = tifiles_checksum(entry->data, entry->size); if (fwrite_word(f, sum) < 0) goto tfwr; } } tifiles_free_table_of_entries(table); g_free(filename); fclose(f); return 0; tfwr: // release on exit tifiles_critical("%s: error writing file %s", __FUNCTION__, filename); tifiles_free_table_of_entries(table); g_free(filename); fclose(f); return ERR_FILE_IO; }
static int write_dhmm(float32 **tmat, float32 ***mixw, FILE *fp) { int32 n_arc; uint32 i, j, k; int32 tmp; fwrite_long (fp, COUNT_F); /* what type of file it is (trans only) */ /* might also be BIG_HMM */ fwrite_long (fp, S2_N_CODEWORD); /* number of codewords??? */ fwrite_long (fp, S2_N_STATE-1); /* n_omatrix */ /* * Output pdfs */ for (j = 0; j < 4; j++) { for (i = 0; i < S2_N_STATE-1; i++) { for (k = 0; k < S2_N_CODEWORD; k++) { tmp = LOG(mixw[i][j][k]); fwrite_long(fp, tmp); } } } fwrite_long (fp, S2_N_STATE); /* n_states */ fwrite_long (fp, 1); /* n_initial (states) */ fwrite_long (fp, 0); /* list of initial states {0} */ fwrite_long (fp, 1); /* n_final */ fwrite_long (fp, (S2_N_STATE-1)); /* list of final states {S2_N_STATE-1} */ n_arc = 14; fwrite_long (fp, n_arc); /* number of arcs */ /* * Dump these in the same order as s2 did. */ write_transition (0, 0, LOG(tmat[0][0]), 0, fp); write_transition (0, 1, LOG(tmat[0][1]), 0, fp); write_transition (1, 1, LOG(tmat[1][1]), 1, fp); write_transition (1, 2, LOG(tmat[1][2]), 1, fp); write_transition (2, 2, LOG(tmat[2][2]), 2, fp); write_transition (2, 3, LOG(tmat[2][3]), 2, fp); write_transition (3, 3, LOG(tmat[3][3]), 3, fp); write_transition (3, 4, LOG(tmat[3][4]), 3, fp); write_transition (4, 4, LOG(tmat[4][4]), 4, fp); write_transition (4, 5, LOG(tmat[4][5]), 4, fp); /* skip arcs */ write_transition (0, 2, LOG(tmat[0][2]), 0, fp); write_transition (1, 3, LOG(tmat[1][3]), 1, fp); write_transition (2, 4, LOG(tmat[2][4]), 2, fp); write_transition (3, 5, LOG(tmat[3][5]), 3, fp); return S3_SUCCESS; }