[dali_2.0.44] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-url.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/visual-url.h>
19
20 // EXTERNAL HEADERS
21 #include <cstring> // for toupper()
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27 namespace Internal
28 {
29 namespace
30 {
31 VisualUrl::ProtocolType ResolveLocation(const std::string& url)
32 {
33   const char*    urlCStr = url.c_str();
34   const uint32_t length  = url.size();
35   if((length > 7) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/')
36   {
37     // https:// or enbuf://
38     const char hOre = tolower(urlCStr[0]);
39     const char tOrn = tolower(urlCStr[1]);
40     const char tOrb = tolower(urlCStr[2]);
41     const char pOru = tolower(urlCStr[3]);
42     const char sOrf = tolower(urlCStr[4]);
43     if(('h' == hOre) &&
44        ('t' == tOrn) &&
45        ('t' == tOrb) &&
46        ('p' == pOru) &&
47        ('s' == sOrf))
48     {
49       return VisualUrl::REMOTE;
50     }
51     if(('e' == hOre) &&
52        ('n' == tOrn) &&
53        ('b' == tOrb) &&
54        ('u' == pOru) &&
55        ('f' == sOrf))
56     {
57       return VisualUrl::BUFFER;
58     }
59   }
60   else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
61   {
62     // http:// or dali://
63     const char hOrd = tolower(urlCStr[0]);
64     const char tOra = tolower(urlCStr[1]);
65     const char tOrl = tolower(urlCStr[2]);
66     const char pOri = tolower(urlCStr[3]);
67     if(('h' == hOrd) &&
68        ('t' == tOra) &&
69        ('t' == tOrl) &&
70        ('p' == pOri))
71     {
72       return VisualUrl::REMOTE;
73     }
74     if(('d' == hOrd) &&
75        ('a' == tOra) &&
76        ('l' == tOrl) &&
77        ('i' == pOri))
78     {
79       return VisualUrl::TEXTURE;
80     }
81   }
82   else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
83   {
84     // ftp:// or ssh://
85     const char fOrs = tolower(urlCStr[0]);
86     const char tOrs = tolower(urlCStr[1]);
87     const char pOrh = tolower(urlCStr[2]);
88     if(('f' == fOrs) &&
89        ('t' == tOrs) &&
90        ('p' == pOrh))
91     {
92       return VisualUrl::REMOTE;
93     }
94     if(('s' == fOrs) &&
95        ('s' == tOrs) &&
96        ('h' == pOrh))
97     {
98       return VisualUrl::REMOTE;
99     }
100   }
101   return VisualUrl::LOCAL;
102 }
103
104 VisualUrl::Type ResolveType(const std::string& url)
105 {
106   // if only one char in string, can only be regular image
107   const std::size_t count = url.size();
108   if(count > 0)
109   {
110     // parsing from the end for better chance of early outs
111     enum
112     {
113       SUFFIX,
114       HASH,
115       HASH_DOT
116     } state                = SUFFIX;
117     char         SVG[4]    = {'g', 'v', 's', '.'};
118     char         GIF[4]    = {'f', 'i', 'g', '.'};
119     char         WEBP[5]   = {'p', 'b', 'e', 'w', '.'};
120     char         JSON[5]   = {'n', 'o', 's', 'j', '.'};
121     unsigned int svgScore  = 0;
122     unsigned int gifScore  = 0;
123     unsigned int webpScore = 0;
124     unsigned int jsonScore = 0;
125     int          index     = count;
126     while(--index >= 0)
127     {
128       const char        currentChar   = tolower(url[index]);
129       const std::size_t offsetFromEnd = count - index - 1u;
130       if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
131       {
132         // early out if SVG as can't be used in N patch for now
133         if(++svgScore == sizeof(SVG))
134         {
135           return VisualUrl::SVG;
136         }
137       }
138       if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
139       {
140         // early out if GIF as can't be used in N patch for now
141         if(++gifScore == sizeof(GIF))
142         {
143           return VisualUrl::GIF;
144         }
145       }
146       if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
147       {
148         // early out if WEBP as can't be used in N patch for now
149         if(++webpScore == sizeof(WEBP))
150         {
151           return VisualUrl::WEBP;
152         }
153       }
154       if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
155       {
156         // early out if JSON as can't be used in N patch for now
157         if(++jsonScore == sizeof(JSON))
158         {
159           return VisualUrl::JSON;
160         }
161       }
162       switch(state)
163       {
164         case SUFFIX:
165         {
166           if('.' == currentChar)
167           {
168             state = HASH;
169           }
170           break;
171         }
172         case HASH:
173         {
174           if(('#' == currentChar) || ('9' == currentChar))
175           {
176             state = HASH_DOT;
177           }
178           else
179           {
180             // early out, not a valid N/9-patch URL
181             return VisualUrl::REGULAR_IMAGE;
182           }
183           break;
184         }
185         case HASH_DOT:
186         {
187           if('.' == currentChar)
188           {
189             return VisualUrl::N_PATCH;
190           }
191           else
192           {
193             // early out, not a valid N/9-patch URL
194             return VisualUrl::REGULAR_IMAGE;
195           }
196           break;
197         }
198       }
199     }
200   }
201   // if we got here it is a regular image
202   return VisualUrl::REGULAR_IMAGE;
203 }
204
205 } // namespace
206
207 VisualUrl::VisualUrl()
208 : mUrl(),
209   mType(VisualUrl::REGULAR_IMAGE),
210   mLocation(VisualUrl::LOCAL)
211 {
212 }
213
214 VisualUrl::VisualUrl(const std::string& url)
215 : mUrl(url),
216   mType(VisualUrl::REGULAR_IMAGE),
217   mLocation(VisualUrl::LOCAL)
218 {
219   if(!url.empty())
220   {
221     mLocation = ResolveLocation(url);
222     if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
223     {
224       // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
225       mType = ResolveType(url);
226     }
227   }
228 }
229
230 VisualUrl::VisualUrl(const VisualUrl& url)
231 : mUrl(url.mUrl),
232   mType(url.mType),
233   mLocation(url.mLocation)
234 {
235 }
236
237 VisualUrl::~VisualUrl()
238 {
239 }
240
241 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
242 {
243   if(&url != this)
244   {
245     mUrl      = url.mUrl;
246     mType     = url.mType;
247     mLocation = url.mLocation;
248   }
249   return *this;
250 }
251
252 const std::string& VisualUrl::GetUrl() const
253 {
254   return mUrl;
255 }
256
257 VisualUrl::Type VisualUrl::GetType() const
258 {
259   return mType;
260 }
261
262 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
263 {
264   return mLocation;
265 }
266
267 bool VisualUrl::IsValid() const
268 {
269   return mUrl.size() > 0u;
270 }
271
272 bool VisualUrl::IsLocalResource() const
273 {
274   return mLocation == VisualUrl::LOCAL;
275 }
276
277 bool VisualUrl::IsBufferResource() const
278 {
279   return mLocation == VisualUrl::BUFFER;
280 }
281
282 std::string VisualUrl::GetLocation() const
283 {
284   return GetLocation(mUrl);
285 }
286
287 std::string VisualUrl::CreateTextureUrl(const std::string& location)
288 {
289   return "dali://" + location;
290 }
291
292 std::string VisualUrl::CreateBufferUrl(const std::string& location)
293 {
294   return "enbuf://" + location;
295 }
296
297 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
298 {
299   return ResolveLocation(url);
300 }
301
302 std::string VisualUrl::GetLocation(const std::string& url)
303 {
304   const auto location = url.find("://");
305   if(std::string::npos != location)
306   {
307     return url.substr(location + 3u); // 3 characters forwards from the start of ://
308   }
309   return url;
310 }
311
312 } // namespace Internal
313
314 } // namespace Toolkit
315
316 } // namespace Dali