Back to index

lightning-sunbird  0.9+nobinonly
CvtURL.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.org 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 #include <stdio.h>
00038 #include "nscore.h"
00039 #include "nsIConverterInputStream.h"
00040 #include "nsIURL.h"
00041 #include "nsNetUtil.h"
00042 #include "nsCRT.h"
00043 #include "nsString.h"
00044 #include "prprf.h"
00045 #include "prtime.h"
00046 
00047 static nsString* ConvertCharacterSetName(const char* aName)
00048 {
00049   return new nsString(NS_ConvertASCIItoUCS2(aName));
00050 }
00051 
00052 int main(int argc, char** argv)
00053 {
00054   if (3 != argc) {
00055     printf("usage: CvtURL url utf8\n");
00056     return -1;
00057   }
00058 
00059   char* characterSetName = argv[2];
00060   nsString* cset = ConvertCharacterSetName(characterSetName);
00061   if (NS_PTR_TO_INT32(cset) < 0) {
00062     printf("illegal character set name: '%s'\n", characterSetName);
00063     return -1;
00064   }
00065 
00066   // Create url object
00067   char* urlName = argv[1];
00068   nsIURI* url;
00069   nsresult rv;
00070   rv = NS_NewURI(&url, urlName);
00071   if (NS_OK != rv) {
00072     printf("invalid URL: '%s'\n", urlName);
00073     return -1;
00074   }
00075 
00076   // Get an input stream from the url
00077   nsresult ec;
00078   nsIInputStream* in;
00079   ec = NS_OpenURI(&in, url);
00080   if (nsnull == in) {
00081     printf("open of url('%s') failed: error=%x\n", urlName, ec);
00082     return -1;
00083   }
00084 
00085   // Translate the input using the argument character set id into
00086   // unicode
00087   nsCOMPtr<nsIConverterInputStream> uin =
00088     do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv);
00089   if (NS_SUCCEEDED(rv))
00090     rv = uin->Init(in, cset->get(), 4096);
00091   if (NS_FAILED(rv)) {
00092     printf("can't create converter input stream: %d\n", rv);
00093     return -1;
00094   }
00095 
00096   // Read the input and write some output
00097   PRTime start = PR_Now();
00098   PRInt32 count = 0;
00099   for (;;) {
00100     PRUnichar buf[1000];
00101     PRUint32 nb;
00102     ec = uin->Read(buf, 0, 1000, &nb);
00103     if (NS_FAILED(ec)) {
00104       printf("i/o error: %d\n", ec);
00105       break;
00106     }
00107     if (nb == 0) break; // EOF
00108     count += nb;
00109   }
00110   PRTime end = PR_Now();
00111   PRTime conversion, ustoms;
00112   LL_I2L(ustoms, 1000);
00113   LL_SUB(conversion, end, start);
00114   LL_DIV(conversion, conversion, ustoms);
00115   char buf[500];
00116   PR_snprintf(buf, sizeof(buf),
00117               "converting and discarding %d bytes took %lldms",
00118               count, conversion);
00119   puts(buf);
00120 
00121   // Release the objects
00122   in->Release();
00123   url->Release();
00124 
00125   return 0;
00126 }