2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/visuals/visual-url.h>
21 #include <cstring> // for toupper()
33 VisualUrl::ProtocolType ResolveLocation( const std::string& url )
35 const char* urlCStr = url.c_str();
36 const uint32_t length = url.size();
37 if( ( length > 7 ) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/' )
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] ) ) )
46 return VisualUrl::REMOTE;
49 else if( ( length > 6 ) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/' )
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] );
61 return VisualUrl::REMOTE;
68 return VisualUrl::TEXTURE;
71 else if( ( length > 5 ) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/' )
74 const char fOrS = tolower( urlCStr[0] );
75 if( ( 'f' == fOrS )||( 's' == fOrS ) )
77 const char tOrs = tolower( urlCStr[1] );
78 if( ( 't' == tOrs )||( 's' == tOrs ) )
80 const char pOrh = tolower( urlCStr[2] );
81 if( ( 'p' == pOrh )||( 'h' == pOrh ) )
83 return VisualUrl::REMOTE;
88 return VisualUrl::LOCAL;
92 VisualUrl::Type ResolveType( const std::string& url )
94 // if only one char in string, can only be regular image
95 const std::size_t count = url.size();
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 unsigned int svgScore = 0;
103 unsigned int gifScore = 0;
105 while( --index >= 0 )
107 const char currentChar = tolower( url[ index ] );
108 const std::size_t offsetFromEnd = count - index - 1u;
109 if( ( offsetFromEnd < sizeof(SVG) )&&( currentChar == SVG[ offsetFromEnd ] ) )
111 // early out if SVG as can't be used in N patch for now
112 if( ++svgScore == sizeof(SVG) )
114 return VisualUrl::SVG;
117 if( ( offsetFromEnd < sizeof(GIF) )&&( currentChar == GIF[ offsetFromEnd ] ) )
119 // early out if GIF as can't be used in N patch for now
120 if( ++gifScore == sizeof(GIF) )
122 return VisualUrl::GIF;
129 if( '.' == currentChar )
137 if( ( '#' == currentChar ) || ( '9' == currentChar ) )
143 // early out, not a valid N/9-patch URL
144 return VisualUrl::REGULAR_IMAGE;
150 if( '.' == currentChar )
152 return VisualUrl::N_PATCH;
156 // early out, not a valid N/9-patch URL
157 return VisualUrl::REGULAR_IMAGE;
164 // if we got here it is a regular image
165 return VisualUrl::REGULAR_IMAGE;
171 VisualUrl::VisualUrl()
173 mType( VisualUrl::REGULAR_IMAGE ),
174 mLocation( VisualUrl::LOCAL )
178 VisualUrl::VisualUrl( const std::string& url )
180 mType( VisualUrl::REGULAR_IMAGE ),
181 mLocation( VisualUrl::LOCAL )
185 mLocation = ResolveLocation( url );
186 if( VisualUrl::TEXTURE != mLocation )
188 // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
189 mType = ResolveType( url );
194 VisualUrl::VisualUrl( const VisualUrl& url )
197 mLocation( url.mLocation )
201 VisualUrl& VisualUrl::operator=( const VisualUrl& url )
207 mLocation = url.mLocation;
212 const std::string& VisualUrl::GetUrl() const
217 VisualUrl::Type VisualUrl::GetType() const
222 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
227 bool VisualUrl::IsValid() const
229 return mUrl.size() > 0u;
232 bool VisualUrl::IsLocalResource() const
234 return mLocation == VisualUrl::LOCAL;
237 std::string VisualUrl::GetLocation()
239 const auto location = mUrl.find( "://" );
240 if( std::string::npos != location )
242 return mUrl.substr( location + 3u ); // 3 characters forwards from the start of ://
247 std::string VisualUrl::CreateTextureUrl( const std::string& location )
249 return "dali://" + location;