LCOV - code coverage report
Current view: directory - intl/hyphenation/src - hnjstdio.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 46 0 0.0 %
Date: 2012-06-02 Functions: 5 0 0.0 %

       1                 : /* ***** BEGIN LICENSE BLOCK *****
       2                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       3                 :  *
       4                 :  * The contents of this file are subject to the Mozilla Public License Version
       5                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       6                 :  * the License. You may obtain a copy of the License at
       7                 :  * http://www.mozilla.org/MPL/
       8                 :  *
       9                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      10                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      11                 :  * for the specific language governing rights and limitations under the
      12                 :  * License.
      13                 :  *
      14                 :  * The Original Code is Mozilla Hyphenation Service.
      15                 :  *
      16                 :  * The Initial Developer of the Original Code is
      17                 :  * Mozilla Foundation.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Jonathan Kew <jfkthame@gmail.com>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : // This file provides substitutes for the basic stdio routines used by hyphen.c
      39                 : // to read its dictionary files. We #define the stdio names to these versions
      40                 : // in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and
      41                 : // access the dictionary resources.
      42                 : 
      43                 : #include "hnjalloc.h"
      44                 : #include "nsNetUtil.h"
      45                 : 
      46                 : #define BUFSIZE 1024
      47                 : 
      48               0 : struct hnjFile_ {
      49                 :     nsCOMPtr<nsIInputStream> mStream;
      50                 :     char                     mBuffer[BUFSIZE];
      51                 :     PRUint32                 mCurPos;
      52                 :     PRUint32                 mLimit;
      53                 : };
      54                 : 
      55                 : // replacement for fopen()
      56                 : // (not a full substitute: only supports read access)
      57                 : hnjFile*
      58               0 : hnjFopen(const char* aURISpec, const char* aMode)
      59                 : {
      60                 :     // this override only needs to support "r"
      61               0 :     NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen");
      62                 : 
      63               0 :     nsCOMPtr<nsIURI> uri;
      64               0 :     nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
      65               0 :     if (NS_FAILED(rv)) {
      66               0 :         return nsnull;
      67                 :     }
      68                 : 
      69               0 :     nsCOMPtr<nsIInputStream> instream;
      70               0 :     rv = NS_OpenURI(getter_AddRefs(instream), uri);
      71               0 :     if (NS_FAILED(rv)) {
      72               0 :         return nsnull;
      73                 :     }
      74                 : 
      75               0 :     hnjFile *f = new hnjFile;
      76               0 :     f->mStream = instream;
      77               0 :     f->mCurPos = 0;
      78               0 :     f->mLimit = 0;
      79                 : 
      80               0 :     return f;
      81                 : }
      82                 : 
      83                 : // replacement for fclose()
      84                 : int
      85               0 : hnjFclose(hnjFile* f)
      86                 : {
      87               0 :     NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose");
      88                 : 
      89               0 :     int result = 0;
      90               0 :     nsresult rv = f->mStream->Close();
      91               0 :     if (NS_FAILED(rv)) {
      92               0 :         result = EOF;
      93                 :     }
      94               0 :     f->mStream = nsnull;
      95                 : 
      96               0 :     delete f;
      97               0 :     return result;
      98                 : }
      99                 : 
     100                 : // replacement for fgets()
     101                 : // (not a full reimplementation, but sufficient for libhyphen's needs)
     102                 : char*
     103               0 : hnjFgets(char* s, int n, hnjFile* f)
     104                 : {
     105               0 :     NS_ASSERTION(s && f, "bad argument to hnjFgets");
     106                 : 
     107               0 :     int i = 0;
     108               0 :     while (i < n - 1) {
     109               0 :         if (f->mCurPos < f->mLimit) {
     110               0 :             char c = f->mBuffer[f->mCurPos++];
     111               0 :             s[i++] = c;
     112               0 :             if (c == '\n' || c == '\r') {
     113               0 :                 break;
     114                 :             }
     115               0 :             continue;
     116                 :         }
     117                 : 
     118               0 :         f->mCurPos = 0;
     119                 : 
     120               0 :         nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
     121               0 :         if (NS_FAILED(rv)) {
     122               0 :             f->mLimit = 0;
     123               0 :             return nsnull;
     124                 :         }
     125                 : 
     126               0 :         if (f->mLimit == 0) {
     127               0 :             break;
     128                 :         }
     129                 :     }
     130                 : 
     131               0 :     if (i == 0) {
     132               0 :         return nsnull; // end of file
     133                 :     }
     134                 : 
     135               0 :     s[i] = '\0'; // null-terminate the returned string
     136               0 :     return s;
     137                 : }

Generated by: LCOV version 1.7