int backup_save_file(void) { FILE *fp; fp = fopen(BACKUP_FILE, "w"); if (fp!=NULL) { mxmlSaveFile(backup_tree, fp, xml_format_cb); fclose(fp); return 0; } return -1; }
int xmldb_close(void) { do { fp = fopen(XML_DB, "w"); } while (fp == NULL && errno == EINTR); if (fp == NULL) { log_error(LOG_ERROR, "xmldb_close"); return -1; } mxmlSaveFile(xml_root, fp, MXML_TEXT_CALLBACK); fclose(fp); return 0; }
static int l_mxml_write(lua_State *L) { FILE *fp; mxml_node_t *node; node = l_checkMXML(L, 1); fp = _l_openFileFP(L, 2, "w"); mxmlSaveFile(node, fp, MXML_NO_CALLBACK); fclose(fp); return 0; }
/** * Saves the XML internal to the actual File * * @return void */ void save_xml_tree() { pthread_mutex_lock(service_xml_file->mutex); service_xml_file->fp = fopen(XML_FILE_NAME, "w"); if(service_xml_file->fp == NULL) { pthread_mutex_unlock(service_xml_file->mutex); printf("Impossible to open the XML file\n"); return HPD_E_FAILED_TO_OPEN_FILE; } mxmlSaveFile(service_xml_file->xml_tree, service_xml_file->fp, MXML_NO_CALLBACK); fclose(service_xml_file->fp); pthread_mutex_unlock(service_xml_file->mutex); }
bool IAPointsSerializer::guardarXMLIAPoints(string nombreFichero) { if (_xml != nullptr) { mxmlSetWrapMargin(0); _fp = fopen(nombreFichero.c_str(), "w"); if (_fp) { mxmlSaveFile(_xml, _fp, retornoCarro_Callback); // retornoCarro_Callback, será llamado por mxmlSaveFile por cada etiqueta escrita fclose(_fp); // así hacemos más legible el fichero xml en caso de necesitar editarlo a mano. } return true; } return false; }
/** * Saves an area to an XML file. * @param area The area to save. * @param filename The name of the file to write to. */ void FileProcessor::saveArea(Area& area, string filename) { LOG("Saving area['%s'] to '%s'.", area.getTag().c_str(), filename.c_str()); //mxml_node_t *xml = mxmlNewXML("1.0"); mxml_node_t *xml = mxmlNewElement(NULL, "?xml version=\"1.0\" encoding=\"UTF-8\"?"); mxml_node_t *area_node = mxmlNewElement(xml, "area"); saveArea_Tag(area_node, &area); mxmlElementSetAttrf(area_node,"width", "%d", area.getWidth()); mxmlElementSetAttrf(area_node,"height", "%d", area.getHeight()); saveArea_Objects(area_node, area); saveArea_Tiles(area_node, area); FILE *fp = fopen(filename.c_str(), "w"); mxmlSaveFile(xml, fp, whitespace_cb); fclose(fp); }
static int PrintResult (int result) { char buffer[8]; sprintf(buffer, "%d", result); mxml_node_t *xml; mxml_node_t *data; mxml_node_t *node; xml = mxmlNewXML("1.0"); data = mxmlNewElement(xml, "privacymask"); node = mxmlNewElement(data, "res"); mxmlElementSetAttr(node, "ul", buffer); printf("Content-type: application/xml\n\n"); mxmlSaveFile(xml, stdout, MXML_NO_CALLBACK); mxmlDelete(xml); return 0; }
int save_game(char * path, settings * game_settings) { FILE * fp; mxml_node_t * tree; mxml_node_t * game_node; mxml_node_t * board_node; cord c; char row_name[6] = "row_x"; char row[9] = { 0 }; fp = fopen(path, "w"); if (fp == NULL) return FALSE; tree = mxmlNewXML("1.0"); game_node = mxmlNewElement(tree, "game"); mxmlNewInteger(mxmlNewElement(game_node, "game_mode"), game_settings->mode); mxmlNewOpaque(mxmlNewElement(game_node, "user_color"), color_string(game_settings->color)); mxmlNewOpaque(mxmlNewElement(game_node, "next_turn"), color_string(game_settings->next)); if (game_settings->minimax_depth == BEST_DIFFICULTY) mxmlNewOpaque(mxmlNewElement(game_node, "user_color"), "best"); else mxmlNewInteger(mxmlNewElement(game_node, "difficulty"), game_settings->minimax_depth); board_node = mxmlNewElement(game_node, "board"); for (int y = 8; y > 0; y--) { for (int x = 0; x < 8; x++) { c.x = x; c.y = y - 1; row[x] = (board_piece(game_settings->board, c) == EMPTY) ? '_' : board_piece(game_settings->board, c); } row_name[4] = '0' + y; mxmlNewOpaque(mxmlNewElement(board_node, row_name), row); } mxmlSaveFile(tree, fp, MXML_NO_CALLBACK); fclose(fp); mxmlDelete(tree); return TRUE; }
int BCF_Write(bcf_t* bcf, const char* filename) { FILE *fp; int i; mxml_node_t *baroboData, *addresses, *dongles, *iter; if(filename == NULL) { filename = bcf->filename; } fp = fopen(filename, "w"); if(fp == NULL) { fprintf(stderr, "Error opening file %s for writing.\n", filename); return -1; } /* Write everything */ bcf->root = mxmlNewXML("1.0"); baroboData = mxmlNewElement(bcf->root, "BaroboData"); addresses = mxmlNewElement(baroboData, "addresses"); mxmlElementSetAttrf( addresses, "num", "%d", bcf->num); dongles = mxmlNewElement(baroboData, "dongles"); mxmlElementSetAttrf( dongles, "num", "%d", bcf->numDongles); for(i = 0; i < bcf->num; i++) { iter = mxmlNewElement(addresses, "child"); mxmlNewText(iter, 0, bcf->entries[i]); } for(i = 0; i < bcf->numDongles; i++) { iter = mxmlNewElement(dongles, "dongle"); mxmlNewText(iter, 0, bcf->dongles[i]); } mxmlSaveFile(bcf->root, fp, MXML_NO_CALLBACK); fclose(fp); return 0; }
int write_xml( const char* filename, mxml_node_t* xml_node, int force ) { FILE* file; if( (file = fopen(filename, "r")) ){ if(!force){ fprintf(stdout, "This file already exists, do you want to overwrite it? y/n\n> "); char answer = getc(stdin); if(answer != 'y'){ fclose(file); return 0; } } fclose(file); } if( (file = fopen(filename, "w")) == NULL ){ perror("Could not open file for writing"); return 0; } mxmlSaveFile(xml_node, file, xml_whitespace_cb); fclose(file); return 1; }
int main (int argc, char *argv[]) { FILE *fp; /*File to write*/ mxml_node_t *xml; mxml_node_t *graph; mxml_node_t *graph_id; mxml_node_t *vertices_list; mxml_node_t *edges_list; mxml_node_t *vertices; mxml_node_t *vertex; mxml_node_t *vertex_id; mxml_node_t *outedges; mxml_node_t *inedges; mxml_node_t *Vertex_attributes; mxml_node_t *vertex_attribute; mxml_node_t *vertex_attribute_name; mxml_node_t *vertex_attribute_value; mxml_node_t *vertex_attribute_value_type; mxml_node_t *edges; mxml_node_t *edge; mxml_node_t *edge_id; mxml_node_t *startV; mxml_node_t *endV; mxml_node_t *edge_attributes; mxml_node_t *edge_attribute; mxml_node_t *edge_attribute_name; mxml_node_t *edge_attribute_value; mxml_node_t *edge_attribute_value_type; xml = mxmlNewXML("1.0"); graph = mxmlNewElement(xml, "graph"); graph_id = mxmlNewElement(graph, "graph_id"); mxmlNewText(graph_id, 1, g->id); vertices_list = mxmlNewElement(graph, "vertices_list"); mxmlNewText(vertices_list, 1, g->vertexIdList); edges_list = mxmlNewElement(graph, "edges_list"); mxmlNewText(edges_list, 1, g->edgeIdList); vertices = mxmlNewElement(graph, "vertices"); GList* listV=NULL; GList* listE=NULL; int lv = g_list_length(g->vertexIdList); int n = 0; for(n; n<lv; n++) { VertexType* v = g_list_nth_data(listV,n); vertex = mxmlNewElement(vertices, "vertex"); vertex_id=mxmlNewElemet(vertex,"vertex_id"); mxmlNewText(vertex_id, 1, v->id); outedges = mxmlNewElement(vertex,"outedges"); mxmlNewText(outedges, 1, v->outedges); inedges = mxmlNewElement(vertex,"inedges"); mxmlNewText(inedges, 1, v->inedges); vertex_attributes = mxmlNewElement(vertex,"vertex_attributes"); vertex_attribute = mxmlNewElement(vertex_attributes,"vertex_attribute"); GList* v_attr=g_hash_table_get_keys(v->attributes); int a=g_list_length(v); int x=0; for(x;x<a;x++) { vertex_attribute_name=mxmlNewElement(vertex_attribute,"vertex_attribute_name"); mxmlNewText(vertex_attribute_name, 1,g_list_nth_data(v_attr,x)); vertex_attribute_name=mxmlNewElement(vertex_attribute, "vertex_attribute_value"); mxmlNewElement(vertex_attribute_value, 1, vertex_get_attribute_value(v, g_list_nth_data(v_attr,x))); } } int le = g_list_length(g->edgeIdList); int m=0; for(m; m<le; m++) { EdgeType* e= g_list_nth_data(listE,m); edge =mxmlNewElement(edges,"edge"); edge_id=mxmlNewElement(edge, "edge_id"); mxmlNewText(edge_id,1,e->id); startV = mxmlNewElement(edge, "startV"); mxmlNewText(startV,1,e->start); endV = mxmlNewElement(edge,"endV"); mxmlNewText(endV,1,e->end); edge_attributes = mxmlNewElement(edge, "edge_attributes"); edge_attribute = mxmlNewElement(edge_attributes, "edge_attribute"); GList* e_attr= g_hash_table_get_keys(e->attributes); int b=g_list_length(e); int y=0; for (y; y<b;y++) { edge_attribute_name=mxmlNewElement(edge_attribute,"edge_attribute_name"); mxmlNewText(edge_attribute_name,1, g_list_nth_data(e_attr,y)); edge_attribute_name=mxmlNewElement(edge_attribute,"edge_attribute_value"); mxmlNewElement(edge_attribute_value,1, vertex_get_attribute_value(e, g_list_nth_data(e_attr,y))); } } fp = fopen("filename.xml", "w"); mxmlSaveFile(xml, fp, MXML_NO_CALLBACK); fclose(fp); }
int main (int argc, char *argv[]) { Log log; FILE *inFp = 0; FILE *outFp = 0; mxml_node_t *outTree = 0; // hold the xml input to ICAT mxml_node_t *inTree = 0; // Hold the parameter list try { // Check if my assumption of type size are correct. // ************************************************ if(sizeof(short) != 2 || sizeof(int) != 4 ) log.set("Compiler Test", "The integer sizes are not as expected", "(short 2 bytes; int 4 bytes)").printLevel(NXING_LOG_WARNING); if(sizeof(float) != 4 ||sizeof(double) != 8) log.set("Compiler Test", "The float sizes are not as expected", "(float 4 bytes; double 8 bytes)").printLevel(NXING_LOG_WARNING); char mappingFl[NXING_BIG_SIZE] = ""; char nexusFl[NXING_BIG_SIZE] = ""; char outputFl[NXING_BIG_SIZE] = ""; if(argc < 3) { throw log.set("main", "Not enough input parameters!", "Needs 2 input files, mapping file and the NeXus file.", "And one output file.", NXING_ERR_WRONG_INPUT); } else { if(argv[1] != 0) strcpy(mappingFl, argv[1]); if(argv[2] != 0) strcpy(nexusFl, argv[2]); if(argv[3] != 0) strcpy(outputFl, argv[3]); else strcpy(outputFl, "output.xml"); } log.set("main", "input - mapping", mappingFl).printLevel(NXING_LOG_NORMAL); log.set("main", "input - nexus ", nexusFl).printLevel(NXING_LOG_NORMAL); log.set("main", "input - output ", outputFl).printLevel(NXING_LOG_NORMAL); // Read input XML Parameters. // **************************' #ifndef MXML_WRAP mxmlSetWrapMargin(0); #endif inFp = fopen(mappingFl, "r"); if( inFp == 0 ) throw log.set("main", "Can't open the parameter file!", mappingFl, "", NXING_ERR_CANT_OPEN_PARAM); mxml_node_t *inNode; // hold the node to read from. inTree = mxmlLoadFile(NULL, inFp, MXML_TEXT_CALLBACK); fclose(inFp); inFp = 0; if(inTree != 0) log.set("main", "The mapping file has been read!", mappingFl).printLevel(NXING_LOG_ALL); // Create XML output to ICAT. // ************************** outFp = fopen(outputFl, "w"); if( outFp == 0 ) throw log.set("main", "Can't open the output file!", outputFl, "", NXING_ERR_CANT_OPEN_OUTPUT); if(inTree->type == MXML_ELEMENT && (strncmp(inTree->value.element.name, "?xml", 4) == 0)){ outTree = mxmlNewElement(MXML_NO_PARENT, inTree->value.element.name); } log.set("main", "Output Created, first tag added!", inTree->value.element.name).printLevel(NXING_LOG_DEBUG); // // Open the neXus file. // ******************** NxClass nx(nexusFl); //if(nx.isNotOK) throw log.set("Can't open the neXus file!", nexusFl, nx.status); log.set("main", "NeXµs file read!", nexusFl, "", nx.status).printLevel(NXING_LOG_DEBUG); /* * Parse the parameter file, read the neXus file and populate the XML output. */ log.set("main", "Parsing the mapping file!").printLevel(NXING_LOG_DEBUG); inNode = mxmlWalkNext(inTree, inTree, MXML_DESCEND); parseXml(inNode, &outTree, nx); /* * Save the output file */ mxmlSaveFile(outTree, outFp, whitespace_cb); log.set("main", "Output file Saved!", outputFl).printLevel(NXING_LOG_DEBUG); /* * Close the files */ fclose(outFp); /* * Delete the xml Trees */ mxmlDelete(inTree); mxmlDelete(outTree); exit(0); } catch(Log log) { log.printLevel(NXING_LOG_ERROR); if(inFp != 0) fclose(inFp); if(outFp != 0) fclose(outFp); if(inTree != 0) mxmlDelete(inTree); if(outTree != 0) mxmlDelete(outTree); exit(0); } }
int parse_opt(int argc, char *argv[]) { char cmd[4] = {0}; char key[128] = {0}; char val[128] = {0}; char file[128] = {0}; int num = 0; int flag = 0; int c; while (1) { int option_index = 0; /* * name; has_arg; flag; val; * {"quiet", 0, 0, 0} * * ATTENTION: * when has_arg set to 1, a ':' add in "hc:k:f:v:" accordingly * on cmdline: -q a <==> -qa */ static struct option long_options[] = { {"help" , 0, 0, 'h'}, {"inner", 0, 0, 'i'}, {"cmd" , 1, 0, 'c'}, {"file" , 1, 0, 'f'}, {"key" , 1, 0, 'k'}, {"val" , 1, 0, 'v'}, {"num" , 1, 0, 'n'}, {0 , 0, 0, 0 } }; c = getopt_long(argc, argv, "hic:k:f:v:n:", long_options, &option_index); if (c == -1) break; switch (c) { case 'c': flag |= FLAG_CMD; strncpy(cmd, optarg, 1); if (cmd[0] != 'a' && cmd[0] != 'd' && cmd[0] != 'w' && cmd[0] != 'r') { pritnt_usage(argv, __LINE__); } break; case 'f': flag |= FLAG_FILE; strncpy(file, optarg, sizeof(file)); break; case 'k': flag |= FLAG_KEY; strncpy(key, optarg, sizeof(key)); break; case 'v': flag |= FLAG_VAL; strncpy(val, optarg, sizeof(val)); break; case 'n': flag |= FLAG_NUM; num = atoi(optarg); break; case 'i': flag |= FLAG_INNER; break; case 'h': pritnt_usage(argv, 0); default: pritnt_usage(argv, __LINE__); } } if (!(flag & FLAG_CMD)) { pritnt_usage(argv, __LINE__); } if ((flag & FLAG_NUM) && num < 1) { pritnt_usage(argv, __LINE__); } // xml-file needed if (optind == (argc-1)) { if (0 != access(argv[optind], R_OK)) { eprintf("file %s is not readable\n", argv[optind]); exit(1); } } else { fprintf(stderr, "optind:%d argc-1:%d\n", optind, argc-1); pritnt_usage(argv, __LINE__); } int ret; const char *xmlfile = argv[optind]; FILE *fp_xml = fopen(xmlfile, "r"); return_val_if_fail(fp_xml != NULL, FAILURE); mxml_node_t *tree = NULL; if (cmd[0] == 'r') { tree = mxmlLoadFile(NULL, fp_xml, MXML_OPAQUE_CALLBACK); return_val_if_fail(tree != NULL, FAILURE); } else { tree = mxmlLoadFile(NULL, fp_xml, MXML_TEXT_CALLBACK); return_val_if_fail(tree != NULL, FAILURE); } fclose(fp_xml); if (flag & FLAG_FILE) { int match; FILE *fp; char *line = NULL; size_t len = 0; fp = fopen(file, "r"); if (fp == NULL) { exit(1); } while ((getline(&line, &len, fp)) != -1) { match = sscanf(line, "%d%s%*[ ]%[^\n]", &num, key, val); if ((match == 3 && (cmd[0] == 'a' || cmd[0] == 'w')) || (match == 2 && (cmd[0] == 'd' || cmd[0] == 'r'))) { ret = node_process(cmd[0], tree, key, val, num); if (ret != 0) { pritnt_usage(argv, ret); } } else { eprintf("Retrieved error format line %s\n", line); } } free(line); fclose(fp); } else { ret = node_process(cmd[0], tree, key, val, num); if (ret != 0) { pritnt_usage(argv, ret); } } if (cmd[0] != 'r') { if (flag&FLAG_INNER) { FILE *fp = fopen(argv[optind], "w"); mxmlSaveFile(tree, fp, xml_format_write); } else { char *ptr = mxmlSaveAllocString(tree, xml_format_write); printf("%s\n", ptr); } } return 0; }
int /* O - Exit status */ main(int argc, /* I - Number of command-line args */ char *argv[]) /* I - Command-line args */ { int size; FILE *fpr; char buffer[4096]; mxml_node_t *mtree; mxml_node_t *root; mxml_node_t *child; #if 1 mxml_node_t *value; fpr = fopen("ChineseTest.xml", "r"); if (!fpr) return -1; fseek(fpr, 0, SEEK_END); size = ftell(fpr); fseek(fpr, 0, SEEK_SET); fread(buffer, size, 1, fpr); printf("\n%s\n-------------------------------------\n", buffer); mtree = mxmlLoadString(NULL, buffer, MXML_TEXT_CALLBACK); root = mxmlFindElement(mtree, mtree, NULL, NULL, NULL, MXML_DESCEND_FIRST); if (root) { printf("name : %s\n", root->value.element.name); child = mxmlFindElement(root, root, NULL, NULL, NULL, MXML_DESCEND_FIRST); while (child) { printf("name : %s\n", child->value.element.name); value = child->child; if (MXML_TEXT == value->type) { //printf("value: %s\n", value->value.text.string); while ((NULL != value) && (MXML_TEXT == value->type) && (NULL != value->value.text.string)) { printf(" text.string: [%s]\n", value->value.text.string); value = value->next; } } child = mxmlFindElement(child, child, NULL, NULL, NULL, MXML_NO_DESCEND); } } mxmlDelete(mtree); fclose(fpr); #endif return 0; char chinese[32] = {0}; fpr = fopen("chinese.txt", "r"); if (!fpr) return -1; fseek(fpr, 0, SEEK_END); size = ftell(fpr); fseek(fpr, 0, SEEK_SET); fread(chinese, size, 1, fpr); printf("\nchinese: %s\n", chinese); mtree = mxmlNewElement(MXML_NO_PARENT, "?xml version=\"1.0\" encoding=\"UTF-8\"?"); root = mxmlNewElement(mtree, "message"); child = mxmlNewElement(root, "child"); mxmlNewText(child, 0, chinese); mxmlSaveString(mtree, buffer, sizeof(buffer), MXML_TEXT_CALLBACK); printf("-------------------------------------\n%s\n", buffer); return 0; #if 0 int i; /* Looping var */ FILE *fp; /* File to read */ int fd; /* File descriptor */ mxml_node_t *tree, /* XML tree */ *node; /* Node which should be in test.xml */ mxml_index_t *ind; /* XML index */ char buffer[16384]; /* Save string */ static const char *types[] = /* Strings for node types */ { "MXML_ELEMENT", "MXML_INTEGER", "MXML_OPAQUE", "MXML_REAL", "MXML_TEXT" }; /* * Check arguments... */ if (argc != 2) { fputs("Usage: testmxml filename.xml\n", stderr); return (1); } /* * Test the basic functionality... */ tree = mxmlNewElement(MXML_NO_PARENT, "element"); if (!tree) { fputs("ERROR: No parent node in basic test!\n", stderr); return (1); } if (tree->type != MXML_ELEMENT) { fprintf(stderr, "ERROR: Parent has type %s (%d), expected MXML_ELEMENT!\n", tree->type < MXML_ELEMENT || tree->type > MXML_TEXT ? "UNKNOWN" : types[tree->type], tree->type); mxmlDelete(tree); return (1); } if (strcmp(tree->value.element.name, "element")) { fprintf(stderr, "ERROR: Parent value is \"%s\", expected \"element\"!\n", tree->value.element.name); mxmlDelete(tree); return (1); } mxmlNewInteger(tree, 123); mxmlNewOpaque(tree, "opaque"); mxmlNewReal(tree, 123.4f); mxmlNewText(tree, 1, "text"); mxmlLoadString(tree, "<group type='string'>string string string</group>", MXML_NO_CALLBACK); mxmlLoadString(tree, "<group type='integer'>1 2 3</group>", MXML_INTEGER_CALLBACK); mxmlLoadString(tree, "<group type='real'>1.0 2.0 3.0</group>", MXML_REAL_CALLBACK); mxmlLoadString(tree, "<group>opaque opaque opaque</group>", MXML_OPAQUE_CALLBACK); node = tree->child; if (!node) { fputs("ERROR: No first child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_INTEGER) { fprintf(stderr, "ERROR: First child has type %s (%d), expected MXML_INTEGER!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (node->value.integer != 123) { fprintf(stderr, "ERROR: First child value is %d, expected 123!\n", node->value.integer); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No second child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_OPAQUE) { fprintf(stderr, "ERROR: Second child has type %s (%d), expected MXML_OPAQUE!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (!node->value.opaque || strcmp(node->value.opaque, "opaque")) { fprintf(stderr, "ERROR: Second child value is \"%s\", expected \"opaque\"!\n", node->value.opaque ? node->value.opaque : "(null)"); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No third child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_REAL) { fprintf(stderr, "ERROR: Third child has type %s (%d), expected MXML_REAL!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (node->value.real != 123.4f) { fprintf(stderr, "ERROR: Third child value is %f, expected 123.4!\n", node->value.real); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No fourth child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_TEXT) { fprintf(stderr, "ERROR: Fourth child has type %s (%d), expected MXML_TEXT!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (!node->value.text.whitespace || !node->value.text.string || strcmp(node->value.text.string, "text")) { fprintf(stderr, "ERROR: Fourth child value is %d,\"%s\", expected 1,\"text\"!\n", node->value.text.whitespace, node->value.text.string ? node->value.text.string : "(null)"); mxmlDelete(tree); return (1); } for (i = 0; i < 4; i ++) { node = node->next; if (!node) { fprintf(stderr, "ERROR: No group #%d child node in basic test!\n", i + 1); mxmlDelete(tree); return (1); } if (node->type != MXML_ELEMENT) { fprintf(stderr, "ERROR: Group child #%d has type %s (%d), expected MXML_ELEMENT!\n", i + 1, node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } } /* * Test indices... */ ind = mxmlIndexNew(tree, NULL, NULL); if (!ind) { fputs("ERROR: Unable to create index of all nodes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 5) { fprintf(stderr, "ERROR: Index of all nodes contains %d " "nodes; expected 5!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, "group", NULL)) { fputs("ERROR: mxmlIndexFind for \"group\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, "group", NULL); if (!ind) { fputs("ERROR: Unable to create index of groups!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 4) { fprintf(stderr, "ERROR: Index of groups contains %d " "nodes; expected 4!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexEnum(ind)) { fputs("ERROR: mxmlIndexEnum failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, NULL, "type"); if (!ind) { fputs("ERROR: Unable to create index of type attributes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 3) { fprintf(stderr, "ERROR: Index of type attributes contains %d " "nodes; expected 3!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, NULL, "string")) { fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, "group", "type"); if (!ind) { fputs("ERROR: Unable to create index of elements and attributes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 3) { fprintf(stderr, "ERROR: Index of elements and attributes contains %d " "nodes; expected 3!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, "group", "string")) { fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); /* * Check the mxmlDelete() works properly... */ for (i = 0; i < 8; i ++) { if (tree->child) mxmlDelete(tree->child); else { fprintf(stderr, "ERROR: Child pointer prematurely NULL on child #%d\n", i + 1); mxmlDelete(tree); return (1); } } if (tree->child) { fputs("ERROR: Child pointer not NULL after deleting all children!\n", stderr); return (1); } if (tree->last_child) { fputs("ERROR: Last child pointer not NULL after deleting all children!\n", stderr); return (1); } mxmlDelete(tree); /* * Open the file... */ if (argv[1][0] == '<') tree = mxmlLoadString(NULL, argv[1], type_cb); else if ((fp = fopen(argv[1], "rb")) == NULL) { perror(argv[1]); return (1); } else { /* * Read the file... */ tree = mxmlLoadFile(NULL, fp, type_cb); fclose(fp); } if (!tree) { fputs("Unable to read XML file!\n", stderr); return (1); } if (!strcmp(argv[1], "test.xml")) { /* * Verify that mxmlFindElement() and indirectly mxmlWalkNext() work * properly... */ if ((node = mxmlFindElement(tree, tree, "choice", NULL, NULL, MXML_DESCEND)) == NULL) { fputs("Unable to find first <choice> element in XML tree!\n", stderr); mxmlDelete(tree); return (1); } if ((node = mxmlFindElement(node, tree, "choice", NULL, NULL, MXML_NO_DESCEND)) == NULL) { fputs("Unable to find second <choice> element in XML tree!\n", stderr); mxmlDelete(tree); return (1); } } /* * Print the XML tree... */ mxmlSaveFile(tree, stdout, whitespace_cb); /* * Save the XML tree to a string and print it... */ if (mxmlSaveString(tree, buffer, sizeof(buffer), whitespace_cb) > 0) fputs(buffer, stderr); /* * Delete the tree... */ mxmlDelete(tree); /* * Read from/write to file descriptors... */ if (argv[1][0] != '<') { /* * Open the file again... */ if ((fd = open(argv[1], O_RDONLY | O_BINARY)) < 0) { perror(argv[1]); return (1); } /* * Read the file... */ tree = mxmlLoadFd(NULL, fd, type_cb); close(fd); /* * Create filename.xmlfd... */ snprintf(buffer, sizeof(buffer), "%sfd", argv[1]); if ((fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) < 0) { perror(buffer); mxmlDelete(tree); return (1); } /* * Write the file... */ mxmlSaveFd(tree, fd, whitespace_cb); close(fd); /* * Delete the tree... */ mxmlDelete(tree); } #endif /* * Return... */ return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line args */ char *argv[]) /* I - Command-line args */ { int i; /* Looping var */ FILE *fp; /* File to read */ int fd; /* File descriptor */ mxml_node_t *tree, /* XML tree */ *node; /* Node which should be in test.xml */ mxml_index_t *ind; /* XML index */ char buffer[16384]; /* Save string */ static const char *types[] = /* Strings for node types */ { "MXML_ELEMENT", "MXML_INTEGER", "MXML_OPAQUE", "MXML_REAL", "MXML_TEXT" }; /* * Check arguments... */ if (argc != 2) { fputs("Usage: testmxml filename.xml\n", stderr); return (1); } /* * Test the basic functionality... */ tree = mxmlNewElement(MXML_NO_PARENT, "element"); if (!tree) { fputs("ERROR: No parent node in basic test!\n", stderr); return (1); } if (tree->type != MXML_ELEMENT) { fprintf(stderr, "ERROR: Parent has type %s (%d), expected MXML_ELEMENT!\n", tree->type < MXML_ELEMENT || tree->type > MXML_TEXT ? "UNKNOWN" : types[tree->type], tree->type); mxmlDelete(tree); return (1); } if (strcmp(tree->value.element.name, "element")) { fprintf(stderr, "ERROR: Parent value is \"%s\", expected \"element\"!\n", tree->value.element.name); mxmlDelete(tree); return (1); } mxmlNewInteger(tree, 123); mxmlNewOpaque(tree, "opaque"); mxmlNewReal(tree, 123.4f); mxmlNewText(tree, 1, "text"); mxmlLoadString(tree, "<group type='string'>string string string</group>", MXML_NO_CALLBACK); mxmlLoadString(tree, "<group type='integer'>1 2 3</group>", MXML_INTEGER_CALLBACK); mxmlLoadString(tree, "<group type='real'>1.0 2.0 3.0</group>", MXML_REAL_CALLBACK); mxmlLoadString(tree, "<group>opaque opaque opaque</group>", MXML_OPAQUE_CALLBACK); node = tree->child; if (!node) { fputs("ERROR: No first child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_INTEGER) { fprintf(stderr, "ERROR: First child has type %s (%d), expected MXML_INTEGER!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (node->value.integer != 123) { fprintf(stderr, "ERROR: First child value is %d, expected 123!\n", node->value.integer); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No second child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_OPAQUE) { fprintf(stderr, "ERROR: Second child has type %s (%d), expected MXML_OPAQUE!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (!node->value.opaque || strcmp(node->value.opaque, "opaque")) { fprintf(stderr, "ERROR: Second child value is \"%s\", expected \"opaque\"!\n", node->value.opaque ? node->value.opaque : "(null)"); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No third child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_REAL) { fprintf(stderr, "ERROR: Third child has type %s (%d), expected MXML_REAL!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (node->value.real != 123.4f) { fprintf(stderr, "ERROR: Third child value is %f, expected 123.4!\n", node->value.real); mxmlDelete(tree); return (1); } node = node->next; if (!node) { fputs("ERROR: No fourth child node in basic test!\n", stderr); mxmlDelete(tree); return (1); } if (node->type != MXML_TEXT) { fprintf(stderr, "ERROR: Fourth child has type %s (%d), expected MXML_TEXT!\n", node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } if (!node->value.text.whitespace || !node->value.text.string || strcmp(node->value.text.string, "text")) { fprintf(stderr, "ERROR: Fourth child value is %d,\"%s\", expected 1,\"text\"!\n", node->value.text.whitespace, node->value.text.string ? node->value.text.string : "(null)"); mxmlDelete(tree); return (1); } for (i = 0; i < 4; i ++) { node = node->next; if (!node) { fprintf(stderr, "ERROR: No group #%d child node in basic test!\n", i + 1); mxmlDelete(tree); return (1); } if (node->type != MXML_ELEMENT) { fprintf(stderr, "ERROR: Group child #%d has type %s (%d), expected MXML_ELEMENT!\n", i + 1, node->type < MXML_ELEMENT || node->type > MXML_TEXT ? "UNKNOWN" : types[node->type], node->type); mxmlDelete(tree); return (1); } } /* * Test indices... */ ind = mxmlIndexNew(tree, NULL, NULL); if (!ind) { fputs("ERROR: Unable to create index of all nodes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 5) { fprintf(stderr, "ERROR: Index of all nodes contains %d " "nodes; expected 5!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, "group", NULL)) { fputs("ERROR: mxmlIndexFind for \"group\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, "group", NULL); if (!ind) { fputs("ERROR: Unable to create index of groups!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 4) { fprintf(stderr, "ERROR: Index of groups contains %d " "nodes; expected 4!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexEnum(ind)) { fputs("ERROR: mxmlIndexEnum failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, NULL, "type"); if (!ind) { fputs("ERROR: Unable to create index of type attributes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 3) { fprintf(stderr, "ERROR: Index of type attributes contains %d " "nodes; expected 3!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, NULL, "string")) { fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); ind = mxmlIndexNew(tree, "group", "type"); if (!ind) { fputs("ERROR: Unable to create index of elements and attributes!\n", stderr); mxmlDelete(tree); return (1); } if (ind->num_nodes != 3) { fprintf(stderr, "ERROR: Index of elements and attributes contains %d " "nodes; expected 3!\n", ind->num_nodes); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexReset(ind); if (!mxmlIndexFind(ind, "group", "string")) { fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr); mxmlIndexDelete(ind); mxmlDelete(tree); return (1); } mxmlIndexDelete(ind); /* * Check the mxmlDelete() works properly... */ for (i = 0; i < 8; i ++) { if (tree->child) mxmlDelete(tree->child); else { fprintf(stderr, "ERROR: Child pointer prematurely NULL on child #%d\n", i + 1); mxmlDelete(tree); return (1); } } if (tree->child) { fputs("ERROR: Child pointer not NULL after deleting all children!\n", stderr); return (1); } if (tree->last_child) { fputs("ERROR: Last child pointer not NULL after deleting all children!\n", stderr); return (1); } mxmlDelete(tree); /* * Open the file... */ if (argv[1][0] == '<') tree = mxmlLoadString(NULL, argv[1], type_cb); else if ((fp = fopen(argv[1], "rb")) == NULL) { perror(argv[1]); return (1); } else { /* * Read the file... */ tree = mxmlLoadFile(NULL, fp, type_cb); fclose(fp); } if (!tree) { fputs("Unable to read XML file!\n", stderr); return (1); } if (!strcmp(argv[1], "test.xml")) { /* * Verify that mxmlFindElement() and indirectly mxmlWalkNext() work * properly... */ if ((node = mxmlFindElement(tree, tree, "choice", NULL, NULL, MXML_DESCEND)) == NULL) { fputs("Unable to find first <choice> element in XML tree!\n", stderr); mxmlDelete(tree); return (1); } if ((node = mxmlFindElement(node, tree, "choice", NULL, NULL, MXML_NO_DESCEND)) == NULL) { fputs("Unable to find second <choice> element in XML tree!\n", stderr); mxmlDelete(tree); return (1); } } /* * Print the XML tree... */ mxmlSaveFile(tree, stdout, whitespace_cb); /* * Save the XML tree to a string and print it... */ if (mxmlSaveString(tree, buffer, sizeof(buffer), whitespace_cb) > 0) fputs(buffer, stderr); /* * Delete the tree... */ mxmlDelete(tree); /* * Read from/write to file descriptors... */ if (argv[1][0] != '<') { /* * Open the file again... */ if ((fd = open(argv[1], O_RDONLY | O_BINARY)) < 0) { perror(argv[1]); return (1); } /* * Read the file... */ tree = mxmlLoadFd(NULL, fd, type_cb); close(fd); /* * Create filename.xmlfd... */ snprintf(buffer, sizeof(buffer), "%sfd", argv[1]); if ((fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) < 0) { perror(buffer); mxmlDelete(tree); return (1); } /* * Write the file... */ mxmlSaveFd(tree, fd, whitespace_cb); close(fd); /* * Delete the tree... */ mxmlDelete(tree); } /* * Test SAX methods... */ memset(event_counts, 0, sizeof(event_counts)); if (argv[1][0] == '<') tree = mxmlSAXLoadString(NULL, argv[1], type_cb, sax_cb, NULL); else if ((fp = fopen(argv[1], "rb")) == NULL) { perror(argv[1]); return (1); } else { /* * Read the file... */ tree = mxmlSAXLoadFile(NULL, fp, type_cb, sax_cb, NULL); fclose(fp); } if (!strcmp(argv[1], "test.xml")) { if (event_counts[MXML_SAX_CDATA] != 1) { fprintf(stderr, "MXML_SAX_CDATA seen %d times, expected 1 times!\n", event_counts[MXML_SAX_CDATA]); return (1); } if (event_counts[MXML_SAX_COMMENT] != 1) { fprintf(stderr, "MXML_SAX_COMMENT seen %d times, expected 1 times!\n", event_counts[MXML_SAX_COMMENT]); return (1); } if (event_counts[MXML_SAX_DATA] != 61) { fprintf(stderr, "MXML_SAX_DATA seen %d times, expected 61 times!\n", event_counts[MXML_SAX_DATA]); return (1); } if (event_counts[MXML_SAX_DIRECTIVE] != 1) { fprintf(stderr, "MXML_SAX_DIRECTIVE seen %d times, expected 1 times!\n", event_counts[MXML_SAX_DIRECTIVE]); return (1); } if (event_counts[MXML_SAX_ELEMENT_CLOSE] != 20) { fprintf(stderr, "MXML_SAX_ELEMENT_CLOSE seen %d times, expected 20 times!\n", event_counts[MXML_SAX_ELEMENT_CLOSE]); return (1); } if (event_counts[MXML_SAX_ELEMENT_OPEN] != 20) { fprintf(stderr, "MXML_SAX_ELEMENT_OPEN seen %d times, expected 20 times!\n", event_counts[MXML_SAX_ELEMENT_OPEN]); return (1); } } /* * Return... */ return (0); }
int main_2(int argc, const char * argv[]) { mxml_node_t * xml; /*<?xml ... ?>*/ mxml_node_t * data; /*<data>*/ mxml_node_t * node; /*<node>*/ mxml_node_t * group; /*group*/ /*xml*/ xml = mxmlNewXML("1.0"); /*data*/ data = mxmlNewElement(xml, "data"); /*3个node*/ node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val1"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val2"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val3"); /*group*/ group = mxmlNewElement(data, "group"); /*group 下的三个node*/ node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val4"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val5"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val6"); /*2个node*/ node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val7"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val8"); /*自动折行控制------似乎不顶用!!!!!!*/ mxmlSetWrapMargin(100);/*设置100列折行*/ mxmlSetWrapMargin(0);/*取消自动折行*/ /*保存*/ /*保存到文件*/ FILE * fp; fp = fopen("/Users/zhangliang/Desktop/Project/C/Mini-XML/Mini-XML/testxml2.xml", "w"); mxmlSaveFile(xml, fp, MXML_NO_CALLBACK); fclose(fp); /*保存到字符串中*/ char buffer[4096]; char * ptr; mxmlSaveString(xml, buffer, sizeof(buffer), MXML_NO_CALLBACK); ptr = mxmlSaveAllocString(xml, MXML_NO_CALLBACK); printf("buffer: %s\n", buffer); printf("alloc : %s\n", ptr); free(ptr); return 0; }
int LoadSave_saveRocketXML(const text_t* path, TrackData* trackData) { mxml_node_t* xml; mxml_node_t* tracks; FILE* fp; size_t p; struct sync_data* sync_data = &trackData->syncData; int* bookmarks = trackData->bookmarks; xml = mxmlNewXML("1.0"); // save all bookmarks for (p = 0; p < (size_t)trackData->bookmarkCount; ++p) { mxml_node_t* node; const int bookmark = *bookmarks++; if (bookmark == 0) continue; node = mxmlNewElement(xml, "bookmark"); setElementInt(node, "row", "%d", bookmark); } // save groups that are folded for (p = 0; p < (size_t)trackData->groupCount; ++p) { mxml_node_t* node; Group* group = &trackData->groups[p]; if (!group->folded) continue; node = mxmlNewElement(xml, "group"); mxmlElementSetAttr(node, "name", group->name); } tracks = mxmlNewElement(xml, "tracks"); mxmlElementSetAttr(tracks, "rows", "10000"); setElementInt(tracks, "startRow", "%d", trackData->startRow); setElementInt(tracks, "endRow", "%d", trackData->endRow); setElementInt(tracks, "highlightRowStep", "%d", trackData->highlightRowStep); for (p = 0; p < sync_data->num_tracks; ++p) { int i; const struct sync_track* t = sync_data->tracks[p]; mxml_node_t* track = mxmlNewElement(tracks, "track"); mxmlElementSetAttr(track, "name", t->name); mxmlElementSetAttr(track, "folded", trackData->tracks[p].folded ? "1" : "0"); setElementInt(track, "color", "%08x", trackData->tracks[p].color); for (i = 0; i < (int)t->num_keys; ++i) { mxml_node_t* key = mxmlNewElement(track, "key"); setElementInt(key, "row", "%d", (int)t->keys[i].row); setElementFloat(key, "value", t->keys[i].value); setElementInt(key, "interpolation", "%d", (int)t->keys[i].type); } } #if defined(_WIN32) _wfopen_s(&fp, path, L"wt"); #else fp = fopen(path, "wt"); #endif mxmlSaveFile(xml, fp, whitespaceCallback); fclose(fp); return true; }
void game_save(game* p_game, char* file) { FILE *fp = NULL; fp = fopen(file, "w"); element* p_browse; char buffer[10]; mxml_node_t *mxml_xml; mxml_node_t *mxml_game; mxml_node_t *mxml_unit; mxml_node_t *mxml_vars; mxml_node_t *mxml_i; mxml_xml = mxmlNewXML("1.0"); mxml_game = mxmlNewElement(mxml_xml, "game"); mxml_unit = mxmlNewElement(mxml_game, "unit"); mxmlElementSetAttr(mxml_unit, "src", "./res/entities/units/mana.xml"); mxml_i = mxmlNewElement(mxml_unit, "location"); char* attr_file = calloc(strlen("./res/maps/") + strlen(p_game->context) + 1, sizeof(char)); strcpy(attr_file, "./res/maps/"); strcat(attr_file, p_game->context); mxmlElementSetAttr(mxml_i, "file", attr_file); mxmlElementSetAttr(mxml_i, "name", map_get_current(p_game->p_map, p_game->cur_map)->name); free(attr_file); sprintf(buffer, "%d", p_game->p_unit->p_sprite->x); mxmlElementSetAttr(mxml_i, "x", buffer); sprintf(buffer, "%d", p_game->p_unit->p_sprite->y); mxmlElementSetAttr(mxml_i, "y", buffer); mxml_i = mxmlNewElement(mxml_unit, "HP"); mxmlNewInteger(mxml_i, p_game->p_unit->HP); mxml_i = mxmlNewElement(mxml_unit, "MP"); mxmlNewInteger(mxml_i, p_game->p_unit->MP); mxml_i = mxmlNewElement(mxml_unit, "XP"); mxmlNewInteger(mxml_i, p_game->p_unit->XP); mxml_i = mxmlNewElement(mxml_unit, "gold"); mxmlNewInteger(mxml_i, p_game->p_unit->gold); mxml_vars = mxmlNewElement(mxml_game, "vars"); p_browse = p_game->vars; while(p_browse){ var *temp = (var*)p_browse->data; mxml_i = mxmlNewElement(mxml_vars, "var"); mxmlElementSetAttr(mxml_i, "name", temp->name); mxmlElementSetAttr(mxml_i, "value", temp->value); p_browse = p_browse->next; } mxmlSaveFile(mxml_xml, fp, MXML_NO_CALLBACK); mxmlDelete(mxml_xml); fclose(fp); }
void parse_brlan(char* filename, char *filenameout) { FILE* fp = fopen(filename, "rb"); if(fp == NULL) { printf("Error! Couldn't open %s!\n", filename); exit(1); } fseek(fp, 0, SEEK_END); u32 lengthOfFile = ftell(fp); fseek(fp, 0, SEEK_SET); u8 data[lengthOfFile]; fread(data, lengthOfFile, 1, fp); int i, j; for(i = 0; i < 16; i++) { memset(tag_types_list[i], 0, 24); memset(tag_types_rlmc_list[i], 0, 24); memset(tag_types_rlvc_list[i], 0, 24); } strcpy(tag_types_list[0], "X Translation"); strcpy(tag_types_list[1], "Y Translation"); strcpy(tag_types_list[2], "Z Translation"); strcpy(tag_types_list[3], "X Flip"); strcpy(tag_types_list[4], "Y Flip"); strcpy(tag_types_list[5], "Angle"); strcpy(tag_types_list[6], "X Zoom"); strcpy(tag_types_list[7], "Y Zoom"); strcpy(tag_types_list[8], "Width"); strcpy(tag_types_list[9], "Height"); strcpy(tag_types_list[10], "0x0A"); strcpy(tag_types_list[11], "0x0B"); strcpy(tag_types_list[12], "0x0C"); strcpy(tag_types_list[13], "0x0D"); strcpy(tag_types_list[14], "0x0E"); strcpy(tag_types_list[15], "0x0F"); strcpy(tag_types_rlmc_list[0], "0x00"); strcpy(tag_types_rlmc_list[1], "0x01"); strcpy(tag_types_rlmc_list[2], "0x02"); strcpy(tag_types_rlmc_list[3], "0x03"); strcpy(tag_types_rlmc_list[4], "Blackcolor R"); strcpy(tag_types_rlmc_list[5], "Blackcolor G"); strcpy(tag_types_rlmc_list[6], "Blackcolor B"); strcpy(tag_types_rlmc_list[7], "Blackcolor A"); strcpy(tag_types_rlmc_list[8], "Whitecolor R"); strcpy(tag_types_rlmc_list[9], "Whitecolor G"); strcpy(tag_types_rlmc_list[10], "Whitecolor B"); strcpy(tag_types_rlmc_list[11], "Whitecolor A"); strcpy(tag_types_rlmc_list[12], "0x0C"); strcpy(tag_types_rlmc_list[13], "0x0D"); strcpy(tag_types_rlmc_list[14], "0x0E"); strcpy(tag_types_rlmc_list[15], "0x0F"); strcpy(tag_types_rlvc_list[0], "Top Left R"); strcpy(tag_types_rlvc_list[1], "Top Left G"); strcpy(tag_types_rlvc_list[2], "Top Left B"); strcpy(tag_types_rlvc_list[3], "Top Left A"); strcpy(tag_types_rlvc_list[4], "Top Right R"); strcpy(tag_types_rlvc_list[5], "Top Right G"); strcpy(tag_types_rlvc_list[6], "Top Right B"); strcpy(tag_types_rlvc_list[7], "Top Right A"); strcpy(tag_types_rlvc_list[8], "Bottom Left R"); strcpy(tag_types_rlvc_list[9], "Bottom Left G"); strcpy(tag_types_rlvc_list[10], "Bottom Left B"); strcpy(tag_types_rlvc_list[11], "Bottom Left A"); strcpy(tag_types_rlvc_list[12], "Bottom Right R"); strcpy(tag_types_rlvc_list[13], "Bottom Right G"); strcpy(tag_types_rlvc_list[14], "Bottom Right B"); strcpy(tag_types_rlvc_list[15], "Bottom Right A"); BRLAN_fileoffset = 0; brlan_header header; BRLAN_ReadDataFromMemoryX(&header, data, sizeof(brlan_header)); BRLAN_fileoffset = short_swap_bytes(header.pai1_offset); brlan_pai1_universal universal; BRLAN_ReadDataFromMemoryX(&universal, data, sizeof(brlan_pai1_universal)); int pai1_header_type; brlan_pai1_header_type1 pai1_header1; brlan_pai1_header_type2 pai1_header2; brlan_pai1_header_type2 pai1_header; if((be32(universal.flags) & (1 << 25)) >= 1) { pai1_header_type = 2; BRLAN_ReadDataFromMemory(&pai1_header2, data, sizeof(brlan_pai1_header_type2)); } else { pai1_header_type = 1; BRLAN_ReadDataFromMemory(&pai1_header1, data, sizeof(brlan_pai1_header_type1)); } CreateGlobal_pai1(&pai1_header, pai1_header1, pai1_header2, pai1_header_type); FILE *xmlFile; xmlFile = fopen(filenameout, "w"); mxml_node_t *xml; mxml_node_t *xmlan; xml = mxmlNewXML("1.0"); xmlan = mxmlNewElement(xml, "xmlan"); mxmlElementSetAttrf(xmlan, "version", "%d.%d.%d%s", BENZIN_VERSION_MAJOR, BENZIN_VERSION_MINOR, BENZIN_VERSION_BUILD, BENZIN_VERSION_OTHER); mxmlElementSetAttrf(xmlan, "framesize", "%lu", (long unsigned int)short_swap_bytes(pai1_header.framesize)); mxmlElementSetAttrf(xmlan, "flags", "%02x", pai1_header.flags); int timgs = short_swap_bytes(pai1_header.num_timgs); BRLAN_fileoffset = short_swap_bytes(header.pai1_offset) + sizeof(brlan_pai1_header_type1); if ( pai1_header_type == 2 ) BRLAN_fileoffset += 4; int tableoff = BRLAN_fileoffset; int currtableoff = BRLAN_fileoffset; mxml_node_t *timg; for( i = 0; i < timgs; i++) { u32 curr_timg_off = 0; BRLAN_ReadDataFromMemory(&curr_timg_off, data, 4); char timgname[256]; memset(timgname, 0, 256); int z = tableoff + be32(curr_timg_off); for( j = 0; data[z] != 0; timgname[j++] = data[z], z++); { timg = mxmlNewElement(xmlan, "timg"); mxmlElementSetAttrf(timg, "name", "%s", timgname); } currtableoff += 4; } int tagcount = short_swap_bytes(pai1_header.num_entries); u32 taglocations[tagcount]; BRLAN_fileoffset = be32(pai1_header.entry_offset) + short_swap_bytes(header.pai1_offset); BRLAN_ReadDataFromMemory(taglocations, data, tagcount * sizeof(u32)); for( i = 0; i < tagcount; i++) { brlan_entry brlanEntry; tag_header tagHeader; BRLAN_fileoffset = be32(taglocations[i]) + short_swap_bytes(header.pai1_offset); u32 brlanEntryOffset = BRLAN_fileoffset; BRLAN_ReadDataFromMemory(&brlanEntry, data, sizeof(brlan_entry)); mxml_node_t *pane; pane = mxmlNewElement(xmlan, "pane"); mxmlElementSetAttrf(pane, "name", "%s", brlanEntry.name); mxmlElementSetAttrf(pane, "type", "%u", brlanEntry.is_material); u32 entrylocations[brlanEntry.num_tags]; BRLAN_ReadDataFromMemory(entrylocations, data, brlanEntry.num_tags * sizeof(u32)); for ( j = 0; j < brlanEntry.num_tags; j++) { BRLAN_CreateXMLTag(tagHeader, data, brlanEntryOffset + be32(entrylocations[j]), pane); } } mxmlSaveFile(xml, xmlFile, whitespace_cb); mxmlDelete(xml); fclose(xmlFile); fclose(fp); }
int uitsCreate () { mxml_node_t *xml = NULL; FILE *payloadFP; char *payloadXMLString; char *mediaHashValue = NULL; vprintf("Create UITS payload ...\n"); /* make sure that all required parameters are non-null */ uitsCheckRequiredParams("create"); vprintf("Creating UITS payload from comand-line options ... \n"); if (clMediaHashValue) { // if media hash value passed on command line, set it mediaHashValue = clMediaHashValue; } else { /* Calculate the media hash value for the audio file */ dprintf("about to uitsAudioGetMediaHash"); fflush (stdout); mediaHashValue = uitsAudioGetMediaHash(audioFileName); dprintf("done uitsAudioGetMediaHash"); fflush (stdout); /* base 64 encode the media hash, if requested */ if (gpB64MediaHashFlag) { vprintf("Base 64 Encoding Media Hash ...\n"); mediaHashValue = uitsBase64Encode(mediaHashValue, strlen(mediaHashValue), TRUE); } } /* Set the element value in the uits metadata array */ err = uitsSetMetadataValue ("Media", mediaHashValue, uitsMetadataDesc); uitsHandleErrorINT(payloadModuleName, "uitsCreateMediaHashElement", err, OK, ERR_PAYLOAD, "Couldn't set metadata value for Media hash\n"); /* create the XML in an mxml data structure from the metadata array */ xml = uitsCreatePayloadXML (UITS_XML, uitsMetadataDesc, uitsSignatureDesc); uitsHandleErrorPTR(payloadModuleName, "uitsCreate", xml, ERR_PAYLOAD, "Error: Couldn't create XML payload\n"); /* convert the mxml data structure into a string containing the XML */ payloadXMLString = uitsMXMLToXMLString(xml); // TO BE IMPLEMENTED: if no metadata file specified and no command-line options specified, try to read from standard in // validate the xml payload that was created vprintf("Validating payload ...\n"); mediaHashNoVerifyFlag = TRUE; // dont' verify the media hash on create err = uitsVerifyPayloadXML (xml, payloadXMLString, XSDFileName, mediaHashNoVerifyFlag, uitsSignatureDesc); uitsHandleErrorINT(payloadModuleName, "uitsCreate", err, OK, ERR_PAYLOAD, "Error: Couldn't validate XML payload\n"); // mxmlSaveFile(xml, stdout, MXML_NO_CALLBACK); // no whitespace // write the output to a separate payload file or insert it into the audio file if (embedFlag) { vprintf("Embedding payload and writing audio file: %s ...\n", payloadFileName); /* read the audio file, embed the XML into it, write the new audio file */ err = uitsAudioEmbedPayload (audioFileName, payloadFileName, payloadXMLString, numPadBytes); uitsHandleErrorINT(payloadModuleName, "uitsCreate", err, OK, ERR_PAYLOAD, "Couldn't embed payload into audio file\n"); } else { vprintf("Writing standalone UITS payload to file: %s ...\n", payloadFileName); payloadFP = fopen(payloadFileName, "wb"); uitsHandleErrorPTR(payloadModuleName, "uitsCreate", payloadFP, ERR_FILE, "Error: Couldn't open payload file\n"); err = mxmlSaveFile(xml, payloadFP, MXML_NO_CALLBACK); uitsHandleErrorINT(payloadModuleName, "uitsCreate", err, 0, ERR_FILE, "Error: Couldn't open save xml to file\n"); fclose(payloadFP); } vprintf("Success\n"); return (OK); }