/src/mozilla-central/extensions/spellcheck/hunspell/glue/mozHunspellFileMgr.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozHunspellFileMgr.h" |
8 | | |
9 | | #include "mozilla/DebugOnly.h" |
10 | | #include "nsContentUtils.h" |
11 | | #include "nsILoadInfo.h" |
12 | | #include "nsNetUtil.h" |
13 | | |
14 | | using namespace mozilla; |
15 | | |
16 | | FileMgr::FileMgr(const char* aFilename, const char* aKey) |
17 | 0 | { |
18 | 0 | DebugOnly<Result<Ok, nsresult>> result = Open(nsDependentCString(aFilename)); |
19 | 0 | NS_WARNING_ASSERTION(result.value.isOk(), "Failed to open Hunspell file"); |
20 | 0 | } |
21 | | |
22 | | Result<Ok, nsresult> |
23 | | FileMgr::Open(const nsACString& aPath) |
24 | 0 | { |
25 | 0 | nsCOMPtr<nsIURI> uri; |
26 | 0 | MOZ_TRY(NS_NewURI(getter_AddRefs(uri), aPath)); |
27 | 0 |
|
28 | 0 | nsCOMPtr<nsIChannel> channel; |
29 | 0 | MOZ_TRY(NS_NewChannel( |
30 | 0 | getter_AddRefs(channel), |
31 | 0 | uri, |
32 | 0 | nsContentUtils::GetSystemPrincipal(), |
33 | 0 | nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS, |
34 | 0 | nsIContentPolicy::TYPE_OTHER)); |
35 | 0 |
|
36 | 0 | MOZ_TRY(channel->Open2(getter_AddRefs(mStream))); |
37 | 0 | return Ok(); |
38 | 0 | } |
39 | | |
40 | | Result<Ok, nsresult> |
41 | | FileMgr::ReadLine(nsACString& aLine) |
42 | 0 | { |
43 | 0 | if (!mStream) { |
44 | 0 | return Err(NS_ERROR_NOT_INITIALIZED); |
45 | 0 | } |
46 | 0 | |
47 | 0 | bool ok; |
48 | 0 | MOZ_TRY(NS_ReadLine(mStream.get(), &mLineBuffer, aLine, &ok)); |
49 | 0 | if (!ok) { |
50 | 0 | mStream = nullptr; |
51 | 0 | } |
52 | 0 |
|
53 | 0 | mLineNum++; |
54 | 0 | return Ok(); |
55 | 0 | } |
56 | | |
57 | | bool |
58 | | FileMgr::getline(std::string& aResult) |
59 | 0 | { |
60 | 0 | nsAutoCString line; |
61 | 0 | auto res = ReadLine(line); |
62 | 0 | if (res.isErr()) { |
63 | 0 | return false; |
64 | 0 | } |
65 | 0 | |
66 | 0 | aResult.assign(line.BeginReading(), line.Length()); |
67 | 0 | return true; |
68 | 0 | } |