/* MIT Licence Copyright (c) 2002 Seairth Jacobs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //--------------------------------------------------------------------------- #include #include #include "onxep.h" class ONXLiteEventHandler : public ONXEventHandler { public: FILE * fOutput; bool EnterContainerNode(char * pszName) { // fwrite("<", 1, 1, fOutput); // fwrite(pszName, strlen(pszName), 1, fOutput); // fwrite("=", 1, 1, fOutput); fprintf(fOutput, ":%s{", pszName); return true; } bool ExitContainerNode(char * pszName) { if(strcmp(pszName, "ONX") == 0) { // all other ContainerNodes except the RootNode may be // in the short form. fwrite("}onx", 4, 1, fOutput); } else { fwrite("}", 1, 1, fOutput); } return true; } bool ValueNode(char * pszName, ONXValueNodeValue * pValues, unsigned int nValues) { char * pszValue; unsigned int nLength; fprintf(fOutput, ":%s[", pszName); if(nValues > 0) { for(unsigned int nValue = 0; nValue < nValues; nValue++) { pszValue = pValues[nValue].value; nLength = pValues[nValue].length; fwrite("\"", 1, 1, fOutput); if(memchr(pszValue, '\\', nLength) || memchr(pszValue, '\"', nLength) || memchr(pszValue, '\0', nLength)) { // If there are any characters that will require escaping, just escape the // entire value. For long strings, this will speed up future parsing of the // lite version of the infoblock. On the other hand, short strings will // not necessarily take advantage of this method. // fwrite("\\[", 2, 1, fOutput); // fprintf(fOutput, "%X", nLength); // fwrite("]", 1, 1, fOutput); fprintf(fOutput, "\\[%X]", nLength); } // may not be done as a fprintf() since this value may contain // embedded NULL characters (and other non-ascii characters) fwrite(pszValue, nLength, 1, fOutput); fwrite("\"", 1, 1, fOutput); } // for } // if fprintf(fOutput, "]"); return true; } bool Error(unsigned int nError, unsigned int nOffset) { printf("Error #%d occured at offset %d", nError, nOffset); return false; } }; //--------------------------------------------------------------------------- int main(int argc, char* argv[]) { if(argc != 3) { printf("Usage: ONXLite file_in file_out"); } else { FILE * fSource = fopen(argv[1], "r"); if(! fSource) { printf("Failed to open input file: %s", argv[1]); } else { // get the length of the file fseek(fSource, 0, SEEK_END); int nSourceLength = ftell(fSource); fseek(fSource, 0, SEEK_SET); // create a buffer and read in the file char * pszSource = new char[nSourceLength + 1]; fread(pszSource, nSourceLength, 1, fSource); // null-terminate the input buffer *(pszSource + nSourceLength) = NULL; // the file is no longer needed. fclose(fSource); // create the event handler and parser. ONXLiteEventHandler * pHandler = new ONXLiteEventHandler; ONXEventParser * pONX = new ONXEventParser(pHandler); // open the output file pHandler->fOutput = fopen(argv[2], "w"); if(! pHandler->fOutput) { printf("Failed to open output file: %s", argv[2]); } else { pONX->Parse(pszSource); fclose(pHandler->fOutput); } delete pONX; delete[] pszSource; } } return 0; } //---------------------------------------------------------------------------