1 /*****************************************************************************/
2 // Copyright 2015-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 #include "dng_local_string.h"
10 
11 /*****************************************************************************/
12 
dng_local_string()13 dng_local_string::dng_local_string ()
14 
15     :   fDefaultText ()
16     ,   fDictionary  ()
17 
18     {
19 
20     }
21 
22 /*****************************************************************************/
23 
dng_local_string(const dng_string & s)24 dng_local_string::dng_local_string (const dng_string &s)
25 
26     :   fDefaultText (s)
27     ,   fDictionary  ()
28 
29     {
30 
31     }
32 
33 /*****************************************************************************/
34 
~dng_local_string()35 dng_local_string::~dng_local_string ()
36     {
37 
38     }
39 
40 /*****************************************************************************/
41 
Clear()42 void dng_local_string::Clear ()
43     {
44 
45     fDefaultText.Clear ();
46 
47     fDictionary.clear ();
48 
49     }
50 
51 /*****************************************************************************/
52 
SetDefaultText(const dng_string & s)53 void dng_local_string::SetDefaultText (const dng_string &s)
54     {
55 
56     fDefaultText = s;
57 
58     }
59 
60 /*****************************************************************************/
61 
AddTranslation(const dng_string & language,const dng_string & translation)62 void dng_local_string::AddTranslation (const dng_string &language,
63                                        const dng_string &translation)
64     {
65 
66     dng_string safeLanguage (language);
67 
68     safeLanguage.Truncate (255);
69 
70     fDictionary.push_back (dictionary_entry (safeLanguage,
71                                              translation));
72 
73     }
74 
75 /*****************************************************************************/
76 
Set(const char * s)77 void dng_local_string::Set (const char *s)
78 	{
79 
80 	dng_string defaultText;
81 
82 	defaultText.Set (s);
83 
84 	*this = dng_local_string (defaultText);
85 
86 	}
87 
88 /*****************************************************************************/
89 
LocalText(const dng_string & locale) const90 const dng_string & dng_local_string::LocalText (const dng_string &locale) const
91     {
92 
93     // Pass 1 - try for a match starting with the entire locale string.
94 
95     if (locale.Length () >= 5)
96         {
97 
98         for (uint32 index = 0; index < TranslationCount (); index++)
99             {
100 
101             if (Language (index).StartsWith (locale.Get (), false))
102                 {
103 
104                 return Translation (index);
105 
106                 }
107 
108             }
109 
110         }
111 
112     // Pass 2 - try for a language only match.
113 
114     if (locale.Length () >= 2)
115         {
116 
117         dng_string languageOnly (locale);
118 
119         languageOnly.Truncate (2);
120 
121         for (uint32 index = 0; index < TranslationCount (); index++)
122             {
123 
124             if (Language (index).StartsWith (languageOnly.Get (), false))
125                 {
126 
127                 return Translation (index);
128 
129                 }
130 
131             }
132 
133         }
134 
135     // Otherwise use default text.
136 
137     return DefaultText ();
138 
139     }
140 
141 /*****************************************************************************/
142 
operator ==(const dng_local_string & s) const143 bool dng_local_string::operator== (const dng_local_string &s) const
144     {
145 
146     if (DefaultText () != s.DefaultText ())
147         {
148         return false;
149         }
150 
151     if (TranslationCount () != s.TranslationCount ())
152         {
153         return false;
154         }
155 
156     for (uint32 index = 0; index < TranslationCount (); index++)
157         {
158 
159         if (Language (index) != s.Language (index))
160             {
161             return false;
162             }
163 
164         if (Translation (index) != s.Translation (index))
165             {
166             return false;
167             }
168 
169         }
170 
171     return true;
172 
173     }
174 
175 /*****************************************************************************/
176 
Truncate(uint32 maxBytes)177 void dng_local_string::Truncate (uint32 maxBytes)
178     {
179 
180     fDefaultText.Truncate (maxBytes);
181 
182     for (uint32 index = 0; index < TranslationCount (); index++)
183         {
184 
185         fDictionary [index] . fTranslation . Truncate (maxBytes);
186 
187         }
188 
189     }
190 
191 /*****************************************************************************/
192