1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/CvtURL.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#include <stdio.h> 1.9 +#include "nscore.h" 1.10 +#include "nsIConverterInputStream.h" 1.11 +#include "nsIURL.h" 1.12 +#include "nsNetUtil.h" 1.13 +#include "nsCRT.h" 1.14 +#include "nsString.h" 1.15 +#include "prprf.h" 1.16 +#include "prtime.h" 1.17 + 1.18 +static nsString* ConvertCharacterSetName(const char* aName) 1.19 +{ 1.20 + return new nsString(NS_ConvertASCIItoUTF16(aName)); 1.21 +} 1.22 + 1.23 +int main(int argc, char** argv) 1.24 +{ 1.25 + if (3 != argc) { 1.26 + printf("usage: CvtURL url utf8\n"); 1.27 + return -1; 1.28 + } 1.29 + 1.30 + char* characterSetName = argv[2]; 1.31 + nsString* cset = ConvertCharacterSetName(characterSetName); 1.32 + if (NS_PTR_TO_INT32(cset) < 0) { 1.33 + printf("illegal character set name: '%s'\n", characterSetName); 1.34 + return -1; 1.35 + } 1.36 + 1.37 + // Create url object 1.38 + char* urlName = argv[1]; 1.39 + nsIURI* url; 1.40 + nsresult rv; 1.41 + rv = NS_NewURI(&url, urlName); 1.42 + if (NS_OK != rv) { 1.43 + printf("invalid URL: '%s'\n", urlName); 1.44 + return -1; 1.45 + } 1.46 + 1.47 + // Get an input stream from the url 1.48 + nsresult ec; 1.49 + nsIInputStream* in; 1.50 + ec = NS_OpenURI(&in, url); 1.51 + if (nullptr == in) { 1.52 + printf("open of url('%s') failed: error=%x\n", urlName, ec); 1.53 + return -1; 1.54 + } 1.55 + 1.56 + // Translate the input using the argument character set id into 1.57 + // unicode 1.58 + nsCOMPtr<nsIConverterInputStream> uin = 1.59 + do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv); 1.60 + if (NS_SUCCEEDED(rv)) 1.61 + rv = uin->Init(in, cset->get(), 4096); 1.62 + if (NS_FAILED(rv)) { 1.63 + printf("can't create converter input stream: %d\n", rv); 1.64 + return -1; 1.65 + } 1.66 + 1.67 + // Read the input and write some output 1.68 + PRTime start = PR_Now(); 1.69 + int32_t count = 0; 1.70 + for (;;) { 1.71 + char16_t buf[1000]; 1.72 + uint32_t nb; 1.73 + ec = uin->Read(buf, 0, 1000, &nb); 1.74 + if (NS_FAILED(ec)) { 1.75 + printf("i/o error: %d\n", ec); 1.76 + break; 1.77 + } 1.78 + if (nb == 0) break; // EOF 1.79 + count += nb; 1.80 + } 1.81 + PRTime end = PR_Now(); 1.82 + PRTime conversion = (end - start) / 1000; 1.83 + char buf[500]; 1.84 + PR_snprintf(buf, sizeof(buf), 1.85 + "converting and discarding %d bytes took %lldms", 1.86 + count, conversion); 1.87 + puts(buf); 1.88 + 1.89 + // Release the objects 1.90 + in->Release(); 1.91 + url->Release(); 1.92 + 1.93 + return 0; 1.94 +}