xpcom/tests/CvtURL.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #include <stdio.h>
michael@0 6 #include "nscore.h"
michael@0 7 #include "nsIConverterInputStream.h"
michael@0 8 #include "nsIURL.h"
michael@0 9 #include "nsNetUtil.h"
michael@0 10 #include "nsCRT.h"
michael@0 11 #include "nsString.h"
michael@0 12 #include "prprf.h"
michael@0 13 #include "prtime.h"
michael@0 14
michael@0 15 static nsString* ConvertCharacterSetName(const char* aName)
michael@0 16 {
michael@0 17 return new nsString(NS_ConvertASCIItoUTF16(aName));
michael@0 18 }
michael@0 19
michael@0 20 int main(int argc, char** argv)
michael@0 21 {
michael@0 22 if (3 != argc) {
michael@0 23 printf("usage: CvtURL url utf8\n");
michael@0 24 return -1;
michael@0 25 }
michael@0 26
michael@0 27 char* characterSetName = argv[2];
michael@0 28 nsString* cset = ConvertCharacterSetName(characterSetName);
michael@0 29 if (NS_PTR_TO_INT32(cset) < 0) {
michael@0 30 printf("illegal character set name: '%s'\n", characterSetName);
michael@0 31 return -1;
michael@0 32 }
michael@0 33
michael@0 34 // Create url object
michael@0 35 char* urlName = argv[1];
michael@0 36 nsIURI* url;
michael@0 37 nsresult rv;
michael@0 38 rv = NS_NewURI(&url, urlName);
michael@0 39 if (NS_OK != rv) {
michael@0 40 printf("invalid URL: '%s'\n", urlName);
michael@0 41 return -1;
michael@0 42 }
michael@0 43
michael@0 44 // Get an input stream from the url
michael@0 45 nsresult ec;
michael@0 46 nsIInputStream* in;
michael@0 47 ec = NS_OpenURI(&in, url);
michael@0 48 if (nullptr == in) {
michael@0 49 printf("open of url('%s') failed: error=%x\n", urlName, ec);
michael@0 50 return -1;
michael@0 51 }
michael@0 52
michael@0 53 // Translate the input using the argument character set id into
michael@0 54 // unicode
michael@0 55 nsCOMPtr<nsIConverterInputStream> uin =
michael@0 56 do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv);
michael@0 57 if (NS_SUCCEEDED(rv))
michael@0 58 rv = uin->Init(in, cset->get(), 4096);
michael@0 59 if (NS_FAILED(rv)) {
michael@0 60 printf("can't create converter input stream: %d\n", rv);
michael@0 61 return -1;
michael@0 62 }
michael@0 63
michael@0 64 // Read the input and write some output
michael@0 65 PRTime start = PR_Now();
michael@0 66 int32_t count = 0;
michael@0 67 for (;;) {
michael@0 68 char16_t buf[1000];
michael@0 69 uint32_t nb;
michael@0 70 ec = uin->Read(buf, 0, 1000, &nb);
michael@0 71 if (NS_FAILED(ec)) {
michael@0 72 printf("i/o error: %d\n", ec);
michael@0 73 break;
michael@0 74 }
michael@0 75 if (nb == 0) break; // EOF
michael@0 76 count += nb;
michael@0 77 }
michael@0 78 PRTime end = PR_Now();
michael@0 79 PRTime conversion = (end - start) / 1000;
michael@0 80 char buf[500];
michael@0 81 PR_snprintf(buf, sizeof(buf),
michael@0 82 "converting and discarding %d bytes took %lldms",
michael@0 83 count, conversion);
michael@0 84 puts(buf);
michael@0 85
michael@0 86 // Release the objects
michael@0 87 in->Release();
michael@0 88 url->Release();
michael@0 89
michael@0 90 return 0;
michael@0 91 }

mercurial