LibreOffice Module lingucomponent (master)  1
guess.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <cassert>
23 #include <iostream>
24 #include <string.h>
25 
26 #ifdef SYSTEM_LIBEXTTEXTCAT
27 #include <libexttextcat/textcat.h>
28 #else
29 #include <textcat.h>
30 #endif
31 
32 #include "guess.hxx"
33 
34 /* Old textcat.h versions defined bad spelled constants. */
35 #ifndef TEXTCAT_RESULT_UNKNOWN_STR
36 #define TEXTCAT_RESULT_UNKNOWN_STR _TEXTCAT_RESULT_UNKOWN
37 #endif
38 
39 #ifndef TEXTCAT_RESULT_SHORT_STR
40 #define TEXTCAT_RESULT_SHORT_STR _TEXTCAT_RESULT_SHORT
41 #endif
42 
43 using namespace std;
44 
46  : language_str(DEFAULT_LANGUAGE)
47  , country_str(DEFAULT_COUNTRY)
48 {
49 }
50 
51 /*
52 * this use a char * string to build the guess object
53 * a string like those is made as : [language-country-encoding]...
54 *
55 */
56 Guess::Guess(const char * guess_str)
57  : language_str(DEFAULT_LANGUAGE)
58  , country_str(DEFAULT_COUNTRY)
59 {
60  //if the guess is not like "UNKNOWN" or "SHORT", go into the brackets
61  if(strcmp(guess_str + 1, TEXTCAT_RESULT_UNKNOWN_STR) != 0
62  &&
63  strcmp(guess_str + 1, TEXTCAT_RESULT_SHORT_STR) != 0)
64  {
65  // From how this ctor is called from SimpleGuesser::GuessLanguage and
66  // SimpleGuesser::GetManagedLanguages in
67  // lingucomponent/source/languageguessing/simpleguesser.cxx, guess_str must start with "[":
68  assert(guess_str[0] == GUESS_SEPARATOR_OPEN);
69  auto const start = guess_str + 1;
70  // Only look at the prefix of guess_str, delimited by the next "]" or "[" or end-of-string;
71  // split it into at most three segments separated by "-" (where excess occurrences of "-"
72  // would become part of the third segment), like "en-US-utf8"; the first segment denotes the
73  // language; if there are three segments, the second denotes the country and the third the
74  // encoding; otherwise, the second segment, if any (e.g., in "haw-utf8"), denotes the
75  // encoding:
76  char const * dash1 = nullptr;
77  char const * dash2 = nullptr;
78  auto p = start;
79  for (;; ++p) {
80  auto const c = *p;
81  if (c == '\0' || c == GUESS_SEPARATOR_OPEN || c == GUESS_SEPARATOR_CLOSE) {
82  break;
83  }
84  if (c == GUESS_SEPARATOR_SEP) {
85  if (dash1 == nullptr) {
86  dash1 = p;
87  } else {
88  dash2 = p;
89  // The encoding is ignored, so we can stop as soon as we found the second "-":
90  break;
91  }
92  }
93  }
94  auto const langLen = (dash1 == nullptr ? p : dash1) - start;
95  if (langLen != 0) { // if not we use the default value
96  language_str.assign(start, langLen);
97  }
98  if (dash2 != nullptr) {
99  country_str.assign(dash1 + 1, dash2 - (dash1 + 1));
100  }
101  }
102 }
103 
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define GUESS_SEPARATOR_OPEN
Definition: guess.hxx:22
#define TEXTCAT_RESULT_UNKNOWN_STR
Definition: guess.cxx:36
string language_str
Definition: guess.hxx:50
#define DEFAULT_COUNTRY
Definition: guess.hxx:26
string country_str
Definition: guess.hxx:51
#define TEXTCAT_RESULT_SHORT_STR
Definition: guess.cxx:40
#define DEFAULT_LANGUAGE
Definition: guess.hxx:25
#define GUESS_SEPARATOR_SEP
Definition: guess.hxx:24
Guess()
Default init.
Definition: guess.cxx:45
#define GUESS_SEPARATOR_CLOSE
Definition: guess.hxx:23