Back to index

lightning-sunbird  0.9+nobinonly
logparse.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsXPCOM.h"
00039 #include "nsIComponentManager.h"
00040 #include "nsParserCIID.h"
00041 #include "nsIAtom.h"
00042 #include "nsIParser.h"
00043 #include "nsILoggingSink.h"
00044 #include "nsIIOService.h"
00045 #include "nsNetCID.h"
00046 #include "nsIURI.h"
00047 #include "CNavDTD.h"
00048 #include <fstream.h>
00049 
00050 // Class IID's
00051 static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
00052 static NS_DEFINE_IID(kLoggingSinkCID, NS_LOGGING_SINK_CID);
00053 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00054 
00055 // Interface IID's
00056 
00057 //----------------------------------------------------------------------
00058 
00059 static const char* kWorkingDir = "./";
00060 
00061 nsresult GenerateBaselineFile(const char* aSourceFilename,const char* aBaselineFilename)
00062 {
00063   if (!aSourceFilename || !aBaselineFilename)
00064      return NS_ERROR_INVALID_ARG;
00065 
00066   nsresult rv;
00067 
00068   // Create a parser
00069   nsCOMPtr<nsIParser> parser(do_CreateInstance(kParserCID, &rv));
00070   if (NS_FAILED(rv)) {
00071     cout << "Unable to create a parser (" << rv << ")" <<endl;
00072     return rv;
00073   }
00074 
00075   // Create a sink
00076   nsCOMPtr<nsILoggingSink> sink(do_CreateInstance(kLoggingSinkCID, &rv));
00077   if (NS_FAILED(rv)) {
00078     cout << "Unable to create a sink (" << rv << ")" <<endl;
00079     return rv;
00080   }
00081 
00082   nsCOMPtr<nsILocalFile> localfile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00083   if (NS_FAILED(rv))
00084     return rv;
00085 
00086   localfile->InitWithNativePath(nsDependentCString(aSourceFilename));
00087   nsCOMPtr<nsIURI> inputURI;
00088   {
00089     nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
00090     if (NS_FAILED(rv))
00091       return rv;
00092     rv = ioService->NewFileURI(localfile, getter_AddRefs(inputURI));
00093     if (NS_FAILED(rv))
00094       return rv;
00095   }
00096   localfile->InitWithNativePath(nsDependentCString(aBaselineFilename));
00097   PRFileDesc *outputfile;
00098   localfile->OpenNSPRFileDesc(0660, PR_WRONLY | PR_CREATE_FILE, &outputfile);
00099   sink->SetOutputStream(outputfile);
00100 
00101   // Parse the document, having the sink write the data to fp
00102   nsIDTD* dtd = nsnull;
00103   NS_NewNavHTMLDTD(&dtd);
00104   parser->RegisterDTD(dtd);
00105   parser->SetContentSink(sink);
00106 
00107   rv = parser->Parse(inputURI, 0, PR_FALSE, 0, eDTDMode_unknown);
00108 
00109   return rv;
00110 }
00111 
00112 //----------------------------------------------------------------------
00113 
00114 PRBool CompareFiles(const char* aFilename1, const char* aFilename2) {
00115   PRBool result=PR_TRUE;
00116 
00117   fstream theFirstStream(aFilename1,ios::in | ios::nocreate);
00118   fstream theSecondStream(aFilename2,ios::in | ios::nocreate);
00119 
00120   PRBool done=PR_FALSE;
00121   char   ch1,ch2;
00122 
00123   while(!done) {
00124     theFirstStream >> ch1;
00125     theSecondStream >> ch2;
00126     if(ch1!=ch2) {
00127       result=PR_FALSE;
00128       break;
00129     }
00130     done=PRBool((theFirstStream.ipfx(1)==0) || (theSecondStream.ipfx(1)==0));
00131   }
00132   return result;
00133 }
00134 
00135 //----------------------------------------------------------------------
00136 
00137 void ComputeTempFilename(const char* anIndexFilename, char* aTempFilename) {
00138   if(anIndexFilename) {
00139     strcpy(aTempFilename,anIndexFilename);
00140     char* pos=strrchr(aTempFilename,'\\');
00141     if(!pos)
00142       pos=strrchr(aTempFilename,'/');
00143     if(pos) {
00144       (*pos)=0;
00145       strcat(aTempFilename,"/temp.blx");
00146       return;
00147     }
00148   }
00149   //fall back to our last resort...
00150   strcpy(aTempFilename,"c:/windows/temp/temp.blx");
00151 }
00152 
00153 //----------------------------------------------------------------------
00154 
00155 static const char* kAppName = "logparse ";
00156 static const char* kOption1 = "Compare baseline file-set";
00157 static const char* kOption2 = "Generate baseline ";
00158 static const char* kResultMsg[2] = {" failed!"," ok."};
00159 
00160 void ValidateBaselineFiles(const char* anIndexFilename) {
00161 
00162   fstream theIndexFile(anIndexFilename,ios::in | ios::nocreate);
00163   char    theFilename[500];
00164   char    theBaselineFilename[500];
00165   char    theTempFilename[500];
00166   PRBool  done=PR_FALSE;
00167 
00168   ComputeTempFilename(anIndexFilename,theTempFilename);
00169 
00170   while(!done) {
00171     theIndexFile >> theFilename;
00172     theIndexFile >> theBaselineFilename;
00173     if(theFilename[0] && theBaselineFilename[0]) {
00174       if(NS_SUCCEEDED(GenerateBaselineFile(theFilename,theTempFilename))) {
00175         PRBool matches=CompareFiles(theTempFilename,theBaselineFilename);
00176         cout << theFilename << kResultMsg[matches] << endl;
00177       }
00178     }
00179     theFilename[0]=0;
00180     theBaselineFilename[0]=0;
00181     done=PRBool(theIndexFile.ipfx(1)==0);
00182   }
00183 
00184 
00185   // Now it's time to compare our output to the baseline...
00186 //  if(!CompareFiles(aBaselineFilename,aBaselineFilename)){
00187 //    cout << "File: \"" << aSourceFilename << "\" does not match baseline." << endl;
00188 //  }
00189 
00190 }
00191 
00192 
00193 //----------------------------------------------------------------------
00194 
00195 int main(int argc, char** argv)
00196 {
00197   if (argc < 2) {
00198     cout << "Usage: " << kAppName << " [options] [filename]" << endl;
00199     cout << "     -c [filelist]   " << kOption1 << endl;
00200     cout << "     -g [in] [out]   " << kOption2 << endl;
00201     return -1;
00202   }
00203 
00204   int result=0;
00205 
00206   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00207   if (NS_FAILED(rv)) {
00208     printf("NS_InitXPCOM2 failed\n");
00209     return 1;
00210   }
00211 
00212   if(0==strcmp("-c",argv[1])) {
00213 
00214     if(argc>2) {
00215       cout << kOption1 << "..." << endl;
00216 
00217       //Open the master filelist, and read the filenames.
00218       //Each line contains a source filename and a baseline filename, separated by a space.
00219       ValidateBaselineFiles(argv[2]);
00220     }
00221     else {
00222       cout << kAppName << ": Filelist missing for -c option -- nothing to do." << endl;
00223     }
00224 
00225   }
00226   else if(0==strcmp("-g",argv[1])) {
00227     if(argc>3) {
00228       cout << kOption2 << argv[3] << " from " << argv[2] << "..." << endl;
00229       GenerateBaselineFile(argv[2],argv[3]);
00230     }
00231     else {
00232       cout << kAppName << ": Filename(s) missing for -g option -- nothing to do." << endl;
00233     }
00234   }
00235   else {
00236     cout << kAppName << ": Unknown options -- nothing to do." << endl;
00237   }
00238   return result;
00239 }