/******************************************************************************* * TestParser * * This is a test function to verify that the library is properly opening and * reading xml files. ******************************************************************************/ DLLIMPORT void TestParser( char *Filename ) { scew_parser *parser; scew_element *params; int elementCount = 0; int i; char Buffer[1024]; parser = scew_parser_create (); if (parser != NULL) { scew_parser_load_file (parser, Filename); params = scew_tree_root (scew_parser_tree (parser)); elementCount = scew_element_count (params); sprintf(Buffer, "Root has %u children\n", elementCount); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); for (i = 0; i < elementCount; i++) { scew_element *newParams = scew_element_by_index (params, i); sprintf(Buffer, "This child has %u subchildren\n", scew_element_count( newParams )); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); sprintf(Buffer, "Child %i name='%s'\n", i, scew_element_name (scew_element_by_index (params, i))); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); } scew_parser_free( parser ); } }
arcus_atlas *atlas_from_file(const char *filename) { scew_reader *reader; scew_parser *parser; scew_tree *tree; arcus_atlas *ret; reader = scew_reader_file_create(filename); if (!reader) { set_error_string("atlas_from_file: cannot open/read file"); return NULL; } parser = scew_parser_create(); if (!parser) { set_error_string("atlas_from_file: cannot create a parser (scew bug?)"); scew_reader_close(reader); scew_reader_free(reader); return NULL; } tree = scew_parser_load(parser, reader); if (!tree) { set_error_string("atlas_from_file: cannot create tree from parser and reader (invalid XML?)"); scew_parser_free(parser); scew_reader_close(reader); scew_reader_free(reader); return NULL; } ret = atlas_from_xml(scew_tree_root(tree)); scew_tree_free(tree); scew_parser_free(parser); scew_reader_close(reader); scew_reader_free(reader); return ret; }
int main(int argc, char *argv[]) { scew_parser *parser = NULL; if (init() != 0) { return 1; } if (argc == 2) { if (exists(argv[1])) { parser = scew_parser_create(); scew_parser_ignore_whitespaces(parser, 1); scew_parser_load_file(parser, argv[1]); maptree = scew_parser_tree(parser); } else { allegro_message("Cannot load file: %s", argv[1]); close_program = 1; } } else if (argc == 4) { maptree = new_map(0, MAP_WIN.w / TILE_W, atoi(argv[1]), 1, argv[2]); map_offset = atoi(argv[1]) * TILE_H - EDITOR_WIN_H; } else { /* print usage */ allegro_message ("Usage:\n\"editor.exe <filename>\" to load a file\n \"editor.exe <height_in_tiles> <default_tile_name> <newmapfile>\" to create a new map"); close_program = 1; } while (!close_program) { /* get input */ parse_mouse(); get_grid_item(); if (keypressed()) { parse_keyboard(); } /* blit virtual screen */ clear_to_color(vscreen, DEAD_COLOR); draw_select_lists(vscreen); draw_ctrl_box(vscreen); update_screen(); } if (argc == 2) { write_map(argv[1], maptree); scew_parser_free(parser); } else if (argc == 4) { write_map(argv[3], maptree); scew_tree_free(maptree); } destroy_bitmap(vscreen); cleanup(); return 0; }
/* load_sprite: load all the sprites in SPRITE_INDEX and their graphics */ void load_sprites(void) { size_t i, y; char bmpname[NAME_LN]; scew_tree *t = NULL; scew_parser *p = NULL; scew_element *e = NULL; scew_element *r = NULL; SPRITE *s = NULL; /* setup the parser */ p = scew_parser_create(); scew_parser_ignore_whitespaces(p, 1); if (!scew_parser_load_file(p, SPRITE_INDEX)) { printf("%s\n", scew_error_expat_string(scew_error_code())); return; } t = scew_parser_tree(p); r = scew_tree_root(t); s = create_sprite(); /* read all the sprites from SPRITE_INDEX */ /* parse the SPRITE_INDEX file and outline the list with their names */ e = scew_element_next(r, e); while (e) { strncpy(s->long_name, (char *) scew_element_name(e), LONG_NAME_LN); s->x = s->y = 0; sprite_list = append_sprite_list(s, sprite_list, &sprite_list_sz); e = scew_element_next(r, e); } /* load all their graphics and the sprite data */ gfx_list = complete_sprite_list(sprite_list, sprite_list_sz, gfx_list, &gfx_list_sz); /* create a list_element for each of them and put them in a list */ y = SPRITE_WIN.y; for (i = 0; i < sprite_list_sz; i++) { sprintf(bmpname, "%s0", sprite_list[i].name); spr_elem_lst = add_list_element(rec(SPRITE_WIN.x, y, LIST_ELEM_W, LIST_ELEM_H), 0, DEFAULT_COLOR, SEL_COLOR, sprite_list[i].name, search_gfx_list(bmpname, gfx_list, gfx_list_sz), spr_elem_lst, &spr_elem_lst_sz); y += LIST_ELEM_H; } /* cleanup */ scew_element_free(r); destroy_sprite(s); scew_element_free(e); scew_parser_free(p); }
/******************************************************************************* * Function: Create_Scew_Parser_From_File * * This function returns a void * representing a pointer to a valid * scew_element structure, which is the next child element of the _parent * parameter, or if _parent is NULL, then the next sibling of the _element * parameter. * * Parameters: * Filename: A char * to a NULL terminated character array, which represents * the filename of the XML file to open, and with which to * initialize the parser created in this function. This filename * may also contain a path if necessary, otherwise the function * will attempt to open a file by the name of the filename in the * current directory of the currently running process. * * Returns: * void *: Representing a pointer to a valid scew_parser structure, which * meets the constraints of the input parameters, or NULL on error. * ******************************************************************************/ DLLIMPORT void *Create_Scew_Parser_From_File(char *Filename) { scew_parser *parser; void *ret_val = NULL; parser = scew_parser_create (); if (parser != NULL) { if (scew_parser_load_file (parser, Filename) > 0 ) ret_val = (void *)parser; else { scew_parser_free( parser ); parser = NULL; } } return ret_val; }
/******************************************************************************* * Function: Create_Scew_Parser_From_String * * This function returns a void * representing a pointer to a valid * scew_element structure, which is the next child element of the _parent * parameter, or if _parent is NULL, then the next sibling of the _element * parameter. * * Parameters: * str: A char * to a NULL terminated character array, which represents * the XML string with which to initialize the parser created in * this function. * * strLen: The length of str. * * Returns: * void *: Representing a pointer to a valid scew_parser structure, which * meets the constraints of the input parameters, or NULL on error. * ******************************************************************************/ DLLIMPORT void *Create_Scew_Parser_From_String( char * str, int strLen ) { scew_parser *parser; void *ret_val = NULL; parser = scew_parser_create(); if( NULL != parser ) { if( 0 < scew_parser_load_buffer( parser, str, strLen ) ) ret_val = (void *)parser; else { scew_parser_free( parser ); parser = NULL; } } return ret_val; }
/** * @brief Open given path-file and read data from it to member variables. * @param [in] path Path to project file. * @param [out] sError Error string if error happened. * @return TRUE if reading succeeded, FALSE if error happened. */ BOOL ProjectFile::Read(LPCTSTR path, String *sError) { BOOL loaded = FALSE; scew_tree* tree = NULL; scew_parser* parser = NULL; parser = scew_parser_create(); scew_parser_ignore_whitespaces(parser, 1); scew_reader *reader = NULL; FILE * fp = _tfopen(path, _T("r")); if (fp) { reader = scew_reader_fp_create(fp); if (reader) { tree = scew_parser_load (parser, reader); if (tree) { scew_element * root = GetRootElement(tree); if (root) { // Currently our content is paths, so expect // having paths in valid project file! if (GetPathsData(root)) loaded = TRUE; }; } } scew_tree_free(tree); scew_reader_free(reader); /* Frees the SCEW parser */ scew_parser_free(parser); fclose(fp); } return loaded; }
int main (int argc, char *argv[]) { scew_reader *reader = NULL; scew_parser *parser = NULL; scew_tree *tree = NULL; scew_writer *writer = NULL; scew_printer *printer = NULL; #if defined(_MSC_VER) && defined(XML_UNICODE_WCHAR_T) /* Change stdout to Unicode before writing anything. */ _setmode(_fileno(stdout), _O_U16TEXT); #endif /* _MSC_VER && XML_UNICODE_WCHAR_T */ if (argc < 2) { scew_printf (_XT("Usage: scew_print file.xml\n")); return EXIT_FAILURE; } /* Creates an SCEW parser. This is the first function to call. */ parser = scew_parser_create (); scew_parser_ignore_whitespaces (parser, SCEW_TRUE); /* Loads an XML file. */ reader = scew_reader_file_create (argv[1]); if (reader == NULL) { scew_error code = scew_error_code (); scew_printf (_XT("Unable to load file (error #%d: %s)\n"), code, scew_error_string (code)); return EXIT_FAILURE; } tree = scew_parser_load (parser, reader); if (tree == NULL) { scew_error code = scew_error_code (); scew_printf (_XT("Unable to parse file (error #%d: %s)\n"), code, scew_error_string (code)); if (code == scew_error_expat) { enum XML_Error expat_code = scew_error_expat_code (parser); scew_printf (_XT("Expat error #%d (line %d, column %d): %s\n"), expat_code, scew_error_expat_line (parser), scew_error_expat_column (parser), scew_error_expat_string (expat_code)); } /* Frees the SCEW parser and reader. */ scew_reader_free (reader); scew_parser_free (parser); return EXIT_FAILURE; } /* Prints full tree. */ scew_printf (_XT("\n*** Manual print:\n\n")); print_element (scew_tree_root (tree), 0); /* Prints full tree using SCEW writer. */ scew_printf (_XT("\n\n*** SCEW writer (stdout) print:\n\n")); writer = scew_writer_fp_create (stdout); printer = scew_printer_create (writer); scew_printer_print_tree (printer, tree); scew_printf (_XT("\n")); /* Remember to free tree (scew_parser_free does not free it). */ scew_tree_free (tree); /* Also free the printer and writer. */ scew_writer_free (writer); scew_printer_free (printer); /* Frees the SCEW parser and reader. */ scew_reader_free (reader); scew_parser_free (parser); return 0; }
/******************************************************************************* * Function: Free_Parser * * This function frees the memory used by a scew parser. * * Parameters: * _parser: void * which represents a pointer to a scew_parser. This * pointer must have been created previously using the function * Create_Load_Scew_Parser() . * * Returns: Nothing * ******************************************************************************/ DLLIMPORT void Free_Parser(void *_parser) { scew_parser_free( (scew_parser *)_parser ); }