LCOV - code coverage report
Current view: top level - intl/hyphenation/glue - hnjstdio.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 50 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : // This file provides substitutes for the basic stdio routines used by hyphen.c
       7             : // to read its dictionary files. We #define the stdio names to these versions
       8             : // in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and
       9             : // access the dictionary resources.
      10             : 
      11             : #include "hnjalloc.h"
      12             : #undef FILE // Undo the damage done in hnjalloc.h
      13             : #include "nsNetUtil.h"
      14             : #include "nsIInputStream.h"
      15             : #include "nsIURI.h"
      16             : #include "nsContentUtils.h"
      17             : 
      18             : #define BUFSIZE 1024
      19             : 
      20           0 : struct hnjFile_ {
      21             :     nsCOMPtr<nsIInputStream> mStream;
      22             :     char                     mBuffer[BUFSIZE];
      23             :     uint32_t                 mCurPos;
      24             :     uint32_t                 mLimit;
      25             : };
      26             : 
      27             : // replacement for fopen()
      28             : // (not a full substitute: only supports read access)
      29             : hnjFile*
      30           0 : hnjFopen(const char* aURISpec, const char* aMode)
      31             : {
      32             :     // this override only needs to support "r"
      33           0 :     NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen");
      34             : 
      35           0 :     nsCOMPtr<nsIURI> uri;
      36           0 :     nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
      37           0 :     if (NS_FAILED(rv)) {
      38           0 :         return nullptr;
      39             :     }
      40             : 
      41           0 :     nsCOMPtr<nsIChannel> channel;
      42           0 :     rv = NS_NewChannel(getter_AddRefs(channel),
      43             :                        uri,
      44             :                        nsContentUtils::GetSystemPrincipal(),
      45             :                        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
      46           0 :                        nsIContentPolicy::TYPE_OTHER);
      47           0 :     if (NS_FAILED(rv)) {
      48           0 :         return nullptr;
      49             :     }
      50             : 
      51           0 :     nsCOMPtr<nsIInputStream> instream;
      52           0 :     rv = channel->Open2(getter_AddRefs(instream));
      53           0 :     if (NS_FAILED(rv)) {
      54           0 :         return nullptr;
      55             :     }
      56             : 
      57           0 :     hnjFile *f = new hnjFile;
      58           0 :     f->mStream = instream;
      59           0 :     f->mCurPos = 0;
      60           0 :     f->mLimit = 0;
      61             : 
      62           0 :     return f;
      63             : }
      64             : 
      65             : // replacement for fclose()
      66             : int
      67           0 : hnjFclose(hnjFile* f)
      68             : {
      69           0 :     NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose");
      70             : 
      71           0 :     int result = 0;
      72           0 :     nsresult rv = f->mStream->Close();
      73           0 :     if (NS_FAILED(rv)) {
      74           0 :         result = EOF;
      75             :     }
      76           0 :     f->mStream = nullptr;
      77             : 
      78           0 :     delete f;
      79           0 :     return result;
      80             : }
      81             : 
      82             : // replacement for fgets()
      83             : // (not a full reimplementation, but sufficient for libhyphen's needs)
      84             : char*
      85           0 : hnjFgets(char* s, int n, hnjFile* f)
      86             : {
      87           0 :     NS_ASSERTION(s && f, "bad argument to hnjFgets");
      88             : 
      89           0 :     int i = 0;
      90           0 :     while (i < n - 1) {
      91           0 :         if (f->mCurPos < f->mLimit) {
      92           0 :             char c = f->mBuffer[f->mCurPos++];
      93           0 :             s[i++] = c;
      94           0 :             if (c == '\n' || c == '\r') {
      95             :                 break;
      96             :             }
      97           0 :             continue;
      98             :         }
      99             : 
     100           0 :         f->mCurPos = 0;
     101             : 
     102           0 :         nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
     103           0 :         if (NS_FAILED(rv)) {
     104           0 :             f->mLimit = 0;
     105           0 :             return nullptr;
     106             :         }
     107             : 
     108           0 :         if (f->mLimit == 0) {
     109           0 :             break;
     110             :         }
     111             :     }
     112             : 
     113           0 :     if (i == 0) {
     114           0 :         return nullptr; // end of file
     115             :     }
     116             : 
     117           0 :     s[i] = '\0'; // null-terminate the returned string
     118           0 :     return s;
     119             : }

Generated by: LCOV version 1.13