731ed2024425e26ee54092baca18b6de59991a3a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-url.cpp
1 /*
2  * Copyright (c) 2017 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
30 namespace
31 {
32
33 VisualUrl::ProtocolType ResolveLocation( const std::string& url )
34 {
35   const char* urlCStr = url.c_str();
36   const uint32_t length = url.size();
37   if( ( length > 7 ) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/' )
38   {
39     // https://
40     if( ( 'h' == tolower( urlCStr[0] ) )&&
41         ( 't' == tolower( urlCStr[1] ) )&&
42         ( 't' == tolower( urlCStr[2] ) )&&
43         ( 'p' == tolower( urlCStr[3] ) )&&
44         ( 's' == tolower( urlCStr[4] ) ) )
45     {
46       return VisualUrl::REMOTE;
47     }
48   }
49   else if( ( length > 6 ) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/' )
50   {
51     // http:// or dali://
52     const char hOrd = tolower( urlCStr[0] );
53     const char tOra = tolower( urlCStr[1] );
54     const char tOrl = tolower( urlCStr[2] );
55     const char pOri = tolower( urlCStr[3] );
56     if( ( 'h' == hOrd )&&
57         ( 't' == tOra )&&
58         ( 't' == tOrl )&&
59         ( 'p' == pOri ) )
60     {
61       return VisualUrl::REMOTE;
62     }
63     if( ( 'd' == hOrd )&&
64         ( 'a' == tOra )&&
65         ( 'l' == tOrl )&&
66         ( 'i' == pOri ) )
67     {
68       return VisualUrl::TEXTURE;
69     }
70   }
71   else if( ( length > 5 ) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/' )
72   {
73     // ftp:// or ssh://
74     const char fOrS = tolower( urlCStr[0] );
75     if( ( 'f' == fOrS )||( 's' == fOrS ) )
76     {
77       const char tOrs = tolower( urlCStr[1] );
78       if( ( 't' == tOrs )||( 's' == tOrs ) )
79       {
80         const char pOrh = tolower( urlCStr[2] );
81         if( ( 'p' == pOrh )||( 'h' == pOrh ) )
82         {
83           return VisualUrl::REMOTE;
84         }
85       }
86     }
87   }
88   return VisualUrl::LOCAL;
89 }
90
91
92 VisualUrl::Type ResolveType( const std::string& url )
93 {
94   // if only one char in string, can only be regular image
95   const std::size_t count = url.size();
96   if( count > 0 )
97   {
98     // parsing from the end for better chance of early outs
99     enum { SUFFIX, HASH, HASH_DOT } state = SUFFIX;
100     char SVG[ 4 ] = { 'g', 'v', 's', '.' };
101     char GIF[ 4 ] = { 'f', 'i', 'g', '.' };
102     char JSON[ 5 ] = { 'n', 'o', 's', 'j', '.' };
103     unsigned int svgScore = 0;
104     unsigned int gifScore = 0;
105     unsigned int jsonScore = 0;
106     int index = count;
107     while( --index >= 0 )
108     {
109       const char currentChar = tolower( url[ index ] );
110       const std::size_t offsetFromEnd = count - index - 1u;
111       if( ( offsetFromEnd < sizeof(SVG) )&&( currentChar == SVG[ offsetFromEnd ] ) )
112       {
113         // early out if SVG as can't be used in N patch for now
114         if( ++svgScore == sizeof(SVG) )
115         {
116           return VisualUrl::SVG;
117         }
118       }
119       if( ( offsetFromEnd < sizeof(GIF) )&&( currentChar == GIF[ offsetFromEnd ] ) )
120       {
121         // early out if GIF as can't be used in N patch for now
122         if( ++gifScore == sizeof(GIF) )
123         {
124           return VisualUrl::GIF;
125         }
126       }
127       if( ( offsetFromEnd < sizeof(JSON) )&&( currentChar == JSON[ offsetFromEnd ] ) )
128       {
129         // early out if JSON as can't be used in N patch for now
130         if( ++jsonScore == sizeof(JSON) )
131         {
132           return VisualUrl::JSON;
133         }
134       }
135       switch( state )
136       {
137         case SUFFIX:
138         {
139           if( '.' == currentChar )
140           {
141             state = HASH;
142           }
143           break;
144         }
145         case HASH:
146         {
147           if( ( '#' == currentChar ) || ( '9' == currentChar ) )
148           {
149             state = HASH_DOT;
150           }
151           else
152           {
153             // early out, not a valid N/9-patch URL
154             return VisualUrl::REGULAR_IMAGE;
155           }
156           break;
157         }
158         case HASH_DOT:
159         {
160           if( '.' == currentChar )
161           {
162             return VisualUrl::N_PATCH;
163           }
164           else
165           {
166             // early out, not a valid N/9-patch URL
167             return VisualUrl::REGULAR_IMAGE;
168           }
169           break;
170         }
171       }
172     }
173   }
174   // if we got here it is a regular image
175   return VisualUrl::REGULAR_IMAGE;
176 }
177
178 }
179
180
181 VisualUrl::VisualUrl()
182 : mUrl(),
183   mType( VisualUrl::REGULAR_IMAGE ),
184   mLocation( VisualUrl::LOCAL )
185 {
186 }
187
188 VisualUrl::VisualUrl( const std::string& url )
189 : mUrl( url ),
190   mType( VisualUrl::REGULAR_IMAGE ),
191   mLocation( VisualUrl::LOCAL )
192 {
193   if( ! url.empty() )
194   {
195     mLocation = ResolveLocation( url );
196     if( VisualUrl::TEXTURE != mLocation )
197     {
198       // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
199       mType = ResolveType( url );
200     }
201   }
202 }
203
204 VisualUrl::VisualUrl( const VisualUrl& url )
205 : mUrl( url.mUrl ),
206   mType( url.mType ),
207   mLocation( url.mLocation )
208 {
209 }
210
211 VisualUrl& VisualUrl::operator=( const VisualUrl& url )
212 {
213   if( &url != this )
214   {
215     mUrl = url.mUrl;
216     mType = url.mType;
217     mLocation = url.mLocation;
218   }
219   return *this;
220 }
221
222 const std::string& VisualUrl::GetUrl() const
223 {
224   return mUrl;
225 }
226
227 VisualUrl::Type VisualUrl::GetType() const
228 {
229   return mType;
230 }
231
232 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
233 {
234   return mLocation;
235 }
236
237 bool VisualUrl::IsValid() const
238 {
239   return mUrl.size() > 0u;
240 }
241
242 bool VisualUrl::IsLocalResource() const
243 {
244   return mLocation == VisualUrl::LOCAL;
245 }
246
247 std::string VisualUrl::GetLocation() const
248 {
249   const auto location = mUrl.find( "://" );
250   if( std::string::npos != location )
251   {
252     return mUrl.substr( location + 3u ); // 3 characters forwards from the start of ://
253   }
254   return mUrl;
255 }
256
257 std::string VisualUrl::CreateTextureUrl( const std::string& location )
258 {
259   return "dali://" + location;
260 }
261
262 } // Internal
263
264 } // Toolkit
265
266 } // Dali