michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: #include "nscore.h" michael@0: #include "nsIConverterInputStream.h" michael@0: #include "nsIURL.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsCRT.h" michael@0: #include "nsString.h" michael@0: #include "prprf.h" michael@0: #include "prtime.h" michael@0: michael@0: static nsString* ConvertCharacterSetName(const char* aName) michael@0: { michael@0: return new nsString(NS_ConvertASCIItoUTF16(aName)); michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: if (3 != argc) { michael@0: printf("usage: CvtURL url utf8\n"); michael@0: return -1; michael@0: } michael@0: michael@0: char* characterSetName = argv[2]; michael@0: nsString* cset = ConvertCharacterSetName(characterSetName); michael@0: if (NS_PTR_TO_INT32(cset) < 0) { michael@0: printf("illegal character set name: '%s'\n", characterSetName); michael@0: return -1; michael@0: } michael@0: michael@0: // Create url object michael@0: char* urlName = argv[1]; michael@0: nsIURI* url; michael@0: nsresult rv; michael@0: rv = NS_NewURI(&url, urlName); michael@0: if (NS_OK != rv) { michael@0: printf("invalid URL: '%s'\n", urlName); michael@0: return -1; michael@0: } michael@0: michael@0: // Get an input stream from the url michael@0: nsresult ec; michael@0: nsIInputStream* in; michael@0: ec = NS_OpenURI(&in, url); michael@0: if (nullptr == in) { michael@0: printf("open of url('%s') failed: error=%x\n", urlName, ec); michael@0: return -1; michael@0: } michael@0: michael@0: // Translate the input using the argument character set id into michael@0: // unicode michael@0: nsCOMPtr uin = michael@0: do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = uin->Init(in, cset->get(), 4096); michael@0: if (NS_FAILED(rv)) { michael@0: printf("can't create converter input stream: %d\n", rv); michael@0: return -1; michael@0: } michael@0: michael@0: // Read the input and write some output michael@0: PRTime start = PR_Now(); michael@0: int32_t count = 0; michael@0: for (;;) { michael@0: char16_t buf[1000]; michael@0: uint32_t nb; michael@0: ec = uin->Read(buf, 0, 1000, &nb); michael@0: if (NS_FAILED(ec)) { michael@0: printf("i/o error: %d\n", ec); michael@0: break; michael@0: } michael@0: if (nb == 0) break; // EOF michael@0: count += nb; michael@0: } michael@0: PRTime end = PR_Now(); michael@0: PRTime conversion = (end - start) / 1000; michael@0: char buf[500]; michael@0: PR_snprintf(buf, sizeof(buf), michael@0: "converting and discarding %d bytes took %lldms", michael@0: count, conversion); michael@0: puts(buf); michael@0: michael@0: // Release the objects michael@0: in->Release(); michael@0: url->Release(); michael@0: michael@0: return 0; michael@0: }