xref: /aosp_15_r20/external/sandboxed-api/contrib/hunspell/example/main.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <unistd.h>
16 
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 
21 #include "absl/flags/parse.h"
22 #include "absl/log/globals.h"
23 #include "absl/log/initialize.h"
24 #include "contrib/hunspell/sandboxed.h"
25 
PrintSuggest(HunspellApi & api,sapi::v::RemotePtr & hunspellrp,sapi::v::ConstCStr & word)26 absl::Status PrintSuggest(HunspellApi& api, sapi::v::RemotePtr& hunspellrp,
27                           sapi::v::ConstCStr& word) {
28   sapi::v::GenericPtr outptr;
29 
30   SAPI_ASSIGN_OR_RETURN(
31       int nlist,
32       api.Hunspell_suggest(&hunspellrp, outptr.PtrAfter(), word.PtrBefore()));
33 
34   if (nlist == 0) {
35     std::cout << "No suggestions.\n";
36     return absl::OkStatus();
37   }
38 
39   sapi::v::Array<char*> ptr_list(nlist);
40   ptr_list.SetRemote(reinterpret_cast<void*>(outptr.GetValue()));
41 
42   SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferFromSandboxee(&ptr_list));
43 
44   std::cout << "Suggestions:\n";
45   for (int i = 0; i < nlist; i++) {
46     sapi::v::RemotePtr sugrp(ptr_list[i]);
47     SAPI_ASSIGN_OR_RETURN(std::string sugestion,
48                           api.GetSandbox()->GetCString(sugrp));
49     std::cout << sugestion[i] << "\n";
50   }
51 
52   api.Hunspell_free_list(&hunspellrp, ptr_list.PtrNone(), nlist).IgnoreError();
53 
54   return absl::OkStatus();
55 }
56 
main(int argc,char * argv[])57 int main(int argc, char* argv[]) {
58   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
59   absl::ParseCommandLine(argc, argv);
60   absl::InitializeLog();
61 
62   if (argc != 4) {
63     std::cerr << "Usage:\n  " << argv[0];
64     std::cerr << " AFFIX_FILE FICTIONARY_FILE WORDS_TO_CHECK_FILE\n";
65     return EXIT_FAILURE;
66   }
67 
68   sapi::v::ConstCStr affix_file_name(argv[1]);
69   sapi::v::ConstCStr dictionary_file_name(argv[2]);
70 
71   HunspellSapiSandbox sandbox(affix_file_name.GetData(),
72                               dictionary_file_name.GetData());
73   if (!sandbox.Init().ok()) {
74     std::cerr << "Unable to start sandbox\n";
75     return EXIT_FAILURE;
76   }
77 
78   HunspellApi api(&sandbox);
79   absl::StatusOr<Hunhandle*> hunspell = api.Hunspell_create(
80       affix_file_name.PtrBefore(), dictionary_file_name.PtrBefore());
81   if (!hunspell.ok()) {
82     std::cerr << "Could not initialize hunsepll\n";
83     return EXIT_FAILURE;
84   }
85   sapi::v::RemotePtr hunspellrp(*hunspell);
86 
87   std::ifstream word_to_check_list(argv[3], std::ios_base::in);
88   if (!word_to_check_list.is_open()) {
89     std::cerr << "Could not open file of words to check\n";
90     return EXIT_FAILURE;
91   }
92 
93   std::string buf;
94   while (std::getline(word_to_check_list, buf)) {
95     sapi::v::ConstCStr cbuf(buf.c_str());
96     absl::StatusOr<int> result =
97         api.Hunspell_spell(&hunspellrp, cbuf.PtrBefore());
98     if (!result.ok()) {
99       std::cerr << "Could not check word\n" << result.status() << std::endl;
100       return EXIT_FAILURE;
101     }
102 
103     if (*result) {
104       std::cout << "Word " << buf << " is ok\n";
105     } else {
106       std::cout << "Word " << buf << " is incorrect\n";
107       absl::Status status = PrintSuggest(api, hunspellrp, cbuf);
108       if (!status.ok()) {
109         std::cerr << "Unable to get all suggestion\n" << status << std::endl;
110       }
111     }
112   }
113 
114   api.Hunspell_destroy(&hunspellrp).IgnoreError();
115 
116   return EXIT_SUCCESS;
117 }
118