// ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: testprog.cpp // Compiler Used: MSVC40, egcs-2.90.29, HP CPP 10.24, DJGPP 2.7.2.1 // Produced By: Doug Gaer // File Creation Date: 01/25/2000 // Date Last Modified: 06/21/2000 // Copyright (c) 2000 Dougas M. Gaer // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer. All those who put this code or its derivatives in a commercial product MUST mention this copyright in their documentation for users of the products in which this code or its derivative classes are used. Otherwise, you have the freedom to redistribute verbatim copies of this source code, adapt it to your specific needs, or improve the code and release your improvements to the public provided that the modified files carry prominent notices stating that you changed the files and the date of any change. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE, YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. This program is used to test vbsHTML classes. */ // ----------------------------------------------------------- // #include <iostream.h> #include <stdlib.h> #include "vbshtml.h" #include "vbs_ver.h" // Version number and program name const double ProgramVersionNumber = vbSocketVersion; const char *ProgramName = "vbsHTML test program"; // Program globals int parsing_tags = 0; int listing_doc_tags = 0; int listing_supported_tags = 0; int CopyToMemory(const char *fname, MemoryBuffer &membuf) { membuf.Clear(); const unsigned buf_size = 32767; unsigned i, len; char sbuf[buf_size]; DiskFileB f; int rv = f.df_Open(fname , DiskFileB::df_READONLY, DiskFileB::df_NO_CREATE); if(rv) return rv; while(!f.df_EOF()) { for(i = 0; i < buf_size; i++) sbuf[i] = '\0'; rv = f.df_Read((char *)sbuf, buf_size); if(rv) return rv; len = strlen(sbuf); membuf.Cat((char *)sbuf, len); } return 0; } void ParseTags(const char *fname) { vbsHTML html; cout << endl; cout << "Parsing " << fname << endl; if(html.LoadHTMLFile(fname) != 0) { html.DiskFileExceptionMessage(); return; } cout << "Found " << html.NumTags() << " tags" << endl; cout << endl; cout << "Parsing in-memory copy of " << fname << endl; MemoryBuffer membuf; if(CopyToMemory(fname, membuf) != 0) { html.DiskFileExceptionMessage(); return; } if(html.LoadMemoryBuffer(membuf) != 0) { cout << "A memory error occurred" << endl; return; } cout << "Found " << html.NumTags() << " tags" << endl; cout << endl; } void PrintTagInfo(const vbsHTMLTagInfo &t) { cout << "----- Tag's file information-----" << endl; cout << "Tag file position (start): " << (df_StreamPos)t.start_tag << endl; cout << "Tag file position (end): " << (df_StreamPos)t.end_tag << endl; cout << "Tag's total length <---->: " << t.tag_length << endl; cout << "----- Tag Infomation ------" << endl; cout << "Tag ID----: " << t.tag_id << endl; cout << "Tag ------: " << t.tag.c_str() << endl; cout << "Attributes: " << t.attr.c_str() << endl; cout << "Original tag: " << t.tag_info.c_str() << endl; cout << "----- Tag instructions -----" << endl; if(t.start_instruction) cout << "Start of tag instruction" << endl; if(t.end_instruction) cout << "End of tag instruction" << endl; if(t.has_attributes) cout << "This tag has attibutes associated with it" << endl; cout << endl; } void ListSupportedTags() { vbsHTML html; for(unsigned i = 0; i < vbsMAX_SUPPORTED_TAGS; i++) { cout << "<" << html.GetTag(i) << ">" << endl; } } void ListDocTags(const char *fname) { vbsHTML html; html.LoadHTMLFile(fname); if(!html) { cout << "Could not open the " << fname << " file!" << endl; return; } vbDLList<vbsHTMLTagInfo> *tag_list = html.GetTagList(); vbDNode<vbsHTMLTagInfo> *list_ptr = tag_list->GetFront(); while(!tag_list->IsHeader(list_ptr)) { PrintTagInfo(list_ptr->Data); list_ptr = list_ptr->GetNext(); } } void HelpMessage(const char *program_name, const double version_number) { cout << endl; cout.setf(ios::showpoint | ios::fixed); cout.precision(3); cout << endl; cout << program_name << " version " << version_number << endl; cout << "Usage: " << program_name << " [switches] infile.html" << endl; cout << "Example: " << program_name << " -r index.html" << endl; cout << "Switches: " << endl; cout << " -l = List all the tags found in an HTML document." << endl; cout << " -L = List all the tags supported by this program." << endl; cout << " -p = Parse all the supported tags in an HTML document." << endl; cout << endl; return; } int ProcessArgs(char *arg) { switch(arg[1]) { case 'l' : parsing_tags = 0; listing_doc_tags = 1; break; case 'L' : ListSupportedTags(); return 0; case 'p' : parsing_tags = 1; listing_doc_tags = 0; break; default: cerr << endl; cerr << "Unknown switch " << arg << endl; cerr << "Exiting..." << endl; cerr << endl; return 0; } arg[0] = '\0'; return 1; // All command line arguments were valid } // Program's main thread of execution. // ----------------------------------------------------------- int main(int argc, char **argv) // NOTE: None of the MSVC compilers will expand wildcard characters // used in command-line arguments unless linked with the setargv.obj // library. All the UNIX compliers will expand wildcard characters // by default. { if(argc < 2) { HelpMessage(ProgramName, ProgramVersionNumber); return 0; } // Process command ling arguments and files int narg; char *arg = argv[narg = 1]; while (narg < argc) { if(arg[0] != '\0') { if(arg[0] == '-') { // Look for command line arguments if(!ProcessArgs(arg)) return 0; // Exit if argument is not valid } else { if(parsing_tags) { ParseTags((const char *)arg); } else if(listing_doc_tags) { ListDocTags((const char *)arg); } else { cout << "No operation specified!" << endl; cout << "Exiting..." << endl; } } arg = argv[++narg]; } } return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //