1 /***************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_lingucomponent.hxx"
30 
31 #include <iostream>
32 #include <string.h>
33 
34 #include <libtextcat/textcat.h>
35 #include <altstrfunc.hxx>
36 #include <guess.hxx>
37 
38 using namespace std;
39 
40 Guess::Guess()
41 {
42     language_str = DEFAULT_LANGUAGE;
43     country_str = DEFAULT_COUNTRY;
44     encoding_str = DEFAULT_ENCODING;
45 }
46 
47 /*
48 * this use a char * string to build the guess object
49 * a string like those is made as : [language-country-encoding]...
50 *
51 */
52 
53 Guess::Guess(char * guess_str)
54 {
55     Guess();
56 
57     string lang;
58     string country;
59     string enc;
60 
61     //if the guess is not like "UNKNOWN" or "SHORT", go into the brackets
62 //     if(strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN, strlen(_TEXTCAT_RESULT_UNKOWN)) != 0
63 //        &&
64 //        strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT, strlen(_TEXTCAT_RESULT_SHORT)) != 0)
65 //     {
66         if(strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN) != 0
67            &&
68            strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT) != 0)
69         {
70 
71         int current_pointer = 0;
72 
73         //this is to go to the first char of the guess string ( the '[' of "[en-US-utf8]" )
74         while(!isSeparator(guess_str[current_pointer])){
75             current_pointer++;
76         }
77         current_pointer++;
78 
79         //this is to pick up the language ( the "en" from "[en-US-utf8]" )
80         while(!isSeparator(guess_str[current_pointer])){
81             lang+=guess_str[current_pointer];
82             current_pointer++;
83         }
84         current_pointer++;
85 
86         //this is to pick up the country ( the "US" from "[en-US-utf8]" )
87         while(!isSeparator(guess_str[current_pointer])){
88             country+=guess_str[current_pointer];
89             current_pointer++;
90         }
91         current_pointer++;
92 
93         //this is to pick up the encoding ( the "utf8" from "[en-US-utf8]" )
94         while(!isSeparator(guess_str[current_pointer])){
95             enc+=guess_str[current_pointer];
96             current_pointer++;
97         }
98 
99         if(lang!=""){//if not we use the default value
100             language_str=lang;
101         }
102         country_str=country;
103 
104         if(enc!=""){//if not we use the default value
105             encoding_str=enc;
106         }
107     }
108 }
109 
110 Guess::~Guess(){}
111 
112 string Guess::GetLanguage()
113 {
114     return language_str;
115 }
116 
117 string Guess::GetCountry()
118 {
119     return country_str;
120 }
121 
122 string Guess::GetEncoding()
123 {
124     return encoding_str;
125 }
126 
127 bool Guess::operator==(string lang)
128 {
129     string toString;
130     toString += GetLanguage();
131     toString += "-";
132     toString += GetCountry();
133     toString += "-";
134     toString += GetEncoding();
135     return start(toString, lang);
136 }
137