/src/mozilla-central/intl/hyphenation/glue/hnjstdio.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
13 | | #undef FILE // Undo #defines from hnjalloc.h before #including other headers |
14 | | #undef fopen |
15 | | #undef fclose |
16 | | #undef fgets |
17 | | #undef feof |
18 | | #undef fgetc |
19 | | |
20 | | #include "nsNetUtil.h" |
21 | | #include "nsIInputStream.h" |
22 | | #include "nsIURI.h" |
23 | | #include "nsContentUtils.h" |
24 | | |
25 | 0 | #define BUFSIZE 1024 |
26 | | |
27 | | struct hnjFile_ { |
28 | | nsCOMPtr<nsIInputStream> mStream; |
29 | | char mBuffer[BUFSIZE]; |
30 | | uint32_t mCurPos; |
31 | | uint32_t mLimit; |
32 | | bool mEOF; |
33 | | }; |
34 | | |
35 | | // replacement for fopen() |
36 | | // (not a full substitute: only supports read access) |
37 | | hnjFile* |
38 | | hnjFopen(const char* aURISpec, const char* aMode) |
39 | 0 | { |
40 | 0 | // this override only needs to support "r" |
41 | 0 | NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen"); |
42 | 0 |
|
43 | 0 | nsCOMPtr<nsIURI> uri; |
44 | 0 | nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec); |
45 | 0 | if (NS_FAILED(rv)) { |
46 | 0 | return nullptr; |
47 | 0 | } |
48 | 0 | |
49 | 0 | nsCOMPtr<nsIChannel> channel; |
50 | 0 | rv = NS_NewChannel(getter_AddRefs(channel), |
51 | 0 | uri, |
52 | 0 | nsContentUtils::GetSystemPrincipal(), |
53 | 0 | nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL, |
54 | 0 | nsIContentPolicy::TYPE_OTHER); |
55 | 0 | if (NS_FAILED(rv)) { |
56 | 0 | return nullptr; |
57 | 0 | } |
58 | 0 | |
59 | 0 | nsCOMPtr<nsIInputStream> instream; |
60 | 0 | rv = channel->Open2(getter_AddRefs(instream)); |
61 | 0 | if (NS_FAILED(rv)) { |
62 | 0 | return nullptr; |
63 | 0 | } |
64 | 0 | |
65 | 0 | hnjFile *f = new hnjFile; |
66 | 0 | f->mStream = instream; |
67 | 0 | f->mCurPos = 0; |
68 | 0 | f->mLimit = 0; |
69 | 0 | f->mEOF = false; |
70 | 0 |
|
71 | 0 | return f; |
72 | 0 | } |
73 | | |
74 | | // replacement for fclose() |
75 | | int |
76 | | hnjFclose(hnjFile* f) |
77 | 0 | { |
78 | 0 | NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose"); |
79 | 0 |
|
80 | 0 | int result = 0; |
81 | 0 | nsresult rv = f->mStream->Close(); |
82 | 0 | if (NS_FAILED(rv)) { |
83 | 0 | result = EOF; |
84 | 0 | } |
85 | 0 | f->mStream = nullptr; |
86 | 0 |
|
87 | 0 | delete f; |
88 | 0 | return result; |
89 | 0 | } |
90 | | |
91 | | // replacement for fgetc() |
92 | | int |
93 | | hnjFgetc(hnjFile* f) |
94 | 0 | { |
95 | 0 | if (f->mCurPos >= f->mLimit) { |
96 | 0 | f->mCurPos = 0; |
97 | 0 |
|
98 | 0 | nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit); |
99 | 0 | if (NS_FAILED(rv)) { |
100 | 0 | f->mLimit = 0; |
101 | 0 | } |
102 | 0 |
|
103 | 0 | if (f->mLimit == 0) { |
104 | 0 | f->mEOF = true; |
105 | 0 | return EOF; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | |
109 | 0 | return f->mBuffer[f->mCurPos++]; |
110 | 0 | } |
111 | | |
112 | | // replacement for fgets() |
113 | | // (not a full reimplementation, but sufficient for libhyphen's needs) |
114 | | char* |
115 | | hnjFgets(char* s, int n, hnjFile* f) |
116 | 0 | { |
117 | 0 | NS_ASSERTION(s && f, "bad argument to hnjFgets"); |
118 | 0 |
|
119 | 0 | int i = 0; |
120 | 0 | while (i < n - 1) { |
121 | 0 | int c = hnjFgetc(f); |
122 | 0 |
|
123 | 0 | if (c == EOF) { |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | |
127 | 0 | s[i++] = c; |
128 | 0 |
|
129 | 0 | if (c == '\n' || c == '\r') { |
130 | 0 | break; |
131 | 0 | } |
132 | 0 | } |
133 | 0 |
|
134 | 0 | if (i == 0) { |
135 | 0 | return nullptr; // end of file |
136 | 0 | } |
137 | 0 | |
138 | 0 | s[i] = '\0'; // null-terminate the returned string |
139 | 0 | return s; |
140 | 0 | } |
141 | | |
142 | | int |
143 | | hnjFeof(hnjFile* f) |
144 | 0 | { |
145 | 0 | return f->mEOF ? EOF : 0; |
146 | 0 | } |