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::Location ResolveLocation( const std::string& url)
35 const char FTP[] = { 'f', 't', 'p', ':', '/', '/' };
36 const char SSH[] = { 's', 's', 'h', ':', '/', '/' };
37 const char HTTP[] = { 'h', 't', 't', 'p', ':', '/', '/' };
38 const char HTTPS[] = { 'h', 't', 't', 'p', 's', ':', '/', '/' };
40 const int MATCH_FTP = 0x01;
41 const int MATCH_SSH = 0x02;
42 const int MATCH_HTTP = 0x04;
43 const int MATCH_HTTPS = 0x08;
45 const char* urlCStr = url.c_str();
48 if( urlCStr[3] == ':' || urlCStr[4] == ':' || urlCStr[5] == ':' )
51 for( unsigned int i=0; i < sizeof(HTTPS); ++i )
53 char c = tolower( urlCStr[i] );
54 if( i < sizeof(FTP) && (flags & MATCH_FTP) && c != FTP[i] )
58 if( i < sizeof(SSH) && (flags & MATCH_SSH) && c != SSH[i] )
62 if( i < sizeof(HTTP) && (flags & MATCH_HTTP) && c != HTTP[i] )
66 if( i < sizeof(HTTPS) && (flags & MATCH_HTTPS) && c != HTTPS[i] )
68 flags &= ~MATCH_HTTPS;
71 if( (flags & (MATCH_FTP | MATCH_SSH | MATCH_HTTP | MATCH_HTTPS )) == 0 )
79 return VisualUrl::REMOTE;
83 return VisualUrl::LOCAL;
87 VisualUrl::Type ResolveType( const std::string& url )
89 // if only one char in string, can only be regular image
90 const std::size_t count = url.size();
93 // parsing from the end for better chance of early outs
94 enum { SUFFIX, HASH, HASH_DOT } state = SUFFIX;
95 char SVG[ 4 ] = { 'g', 'v', 's', '.' };
96 char GIF[ 4 ] = { 'f', 'i', 'g', '.' };
97 unsigned int svgScore = 0;
98 unsigned int gifScore = 0;
100 while( --index >= 0 )
102 const char currentChar = url[ index ];
103 const std::size_t offsetFromEnd = count - index - 1u;
104 if( ( offsetFromEnd < sizeof(SVG) )&&( tolower( currentChar ) == SVG[ offsetFromEnd ] ) )
106 // early out if SVG as can't be used in N patch for now
107 if( ++svgScore == sizeof(SVG) )
109 return VisualUrl::SVG;
112 if( ( offsetFromEnd < sizeof(GIF) )&&( tolower( currentChar ) == GIF[ offsetFromEnd ] ) )
115 if( ++gifScore == sizeof(GIF) )
117 return VisualUrl::GIF;
124 if( '.' == currentChar )
132 if( ( '#' == currentChar ) || ( '9' == currentChar ) )
138 // early out, not a valid N/9-patch URL
139 return VisualUrl::REGULAR_IMAGE;
145 if( '.' == currentChar )
147 return VisualUrl::N_PATCH;
151 // early out, not a valid N/9-patch URL
152 return VisualUrl::REGULAR_IMAGE;
159 // if we got here it is a regular image
160 return VisualUrl::REGULAR_IMAGE;
166 VisualUrl::VisualUrl()
168 mType( VisualUrl::REGULAR_IMAGE ),
169 mLocation( VisualUrl::LOCAL )
173 VisualUrl::VisualUrl( const std::string& url )
175 mType( VisualUrl::REGULAR_IMAGE ),
176 mLocation( VisualUrl::LOCAL )
180 mLocation = ResolveLocation( url );
181 mType = ResolveType( url );
185 VisualUrl::VisualUrl( const VisualUrl& url )
188 mLocation( url.mLocation )
192 VisualUrl& VisualUrl::operator=( const VisualUrl& url )
198 mLocation = url.mLocation;
203 const std::string& VisualUrl::GetUrl() const
208 VisualUrl::Type VisualUrl::GetType() const
213 VisualUrl::Location VisualUrl::GetLocation() const
218 bool VisualUrl::IsValid() const
220 return mUrl.size() > 0u;
223 bool VisualUrl::IsLocal() const
225 return mLocation == VisualUrl::LOCAL;