1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_guesslang.hxx"
26 
27 #include <iostream>
28 #include <string.h>
29 
30 #include <libtextcat/textcat.h>
31 #include <altstrfunc.hxx>
32 #include <guess.hxx>
33 
34 using namespace std;
35 
Guess()36 Guess::Guess()
37 {
38     language_str = DEFAULT_LANGUAGE;
39     country_str = DEFAULT_COUNTRY;
40     encoding_str = DEFAULT_ENCODING;
41 }
42 
43 /*
44 * this use a char * string to build the guess object
45 * a string like those is made as : [language-country-encoding]...
46 *
47 */
48 
Guess(char * guess_str)49 Guess::Guess(char * guess_str)
50 {
51     Guess();
52 
53     string lang;
54     string country;
55     string enc;
56 
57     //if the guess is not like "UNKNOWN" or "SHORT", go into the brackets
58 //     if(strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN, strlen(_TEXTCAT_RESULT_UNKOWN)) != 0
59 //        &&
60 //        strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT, strlen(_TEXTCAT_RESULT_SHORT)) != 0)
61 //     {
62         if(strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN) != 0
63            &&
64            strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT) != 0)
65         {
66 
67         int current_pointer = 0;
68 
69         //this is to go to the first char of the guess string ( the '[' of "[en-US-utf8]" )
70         while(!isSeparator(guess_str[current_pointer])){
71             current_pointer++;
72         }
73         current_pointer++;
74 
75         //this is to pick up the language ( the "en" from "[en-US-utf8]" )
76         while(!isSeparator(guess_str[current_pointer])){
77             lang+=guess_str[current_pointer];
78             current_pointer++;
79         }
80         current_pointer++;
81 
82         //this is to pick up the country ( the "US" from "[en-US-utf8]" )
83         while(!isSeparator(guess_str[current_pointer])){
84             country+=guess_str[current_pointer];
85             current_pointer++;
86         }
87         current_pointer++;
88 
89         //this is to pick up the encoding ( the "utf8" from "[en-US-utf8]" )
90         while(!isSeparator(guess_str[current_pointer])){
91             enc+=guess_str[current_pointer];
92             current_pointer++;
93         }
94 
95         if(lang!=""){//if not we use the default value
96             language_str=lang;
97         }
98         country_str=country;
99 
100         if(enc!=""){//if not we use the default value
101             encoding_str=enc;
102         }
103     }
104 }
105 
~Guess()106 Guess::~Guess(){}
107 
GetLanguage()108 string Guess::GetLanguage()
109 {
110     return language_str;
111 }
112 
GetCountry()113 string Guess::GetCountry()
114 {
115     return country_str;
116 }
117 
GetEncoding()118 string Guess::GetEncoding()
119 {
120     return encoding_str;
121 }
122 
operator ==(string lang)123 bool Guess::operator==(string lang)
124 {
125     string toString;
126     toString += GetLanguage();
127     toString += "-";
128     toString += GetCountry();
129     toString += "-";
130     toString += GetEncoding();
131     return start(toString, lang);
132 }
133