Merge "C# Bindings for Transition Policy properties" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-resolve-url.h
1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <ctype.h>
21
22 namespace Dali
23 {
24
25 namespace Toolkit
26 {
27
28 namespace Internal
29 {
30
31 namespace UrlType
32 {
33   /**
34    * The type of the URL based on the string contents
35    */
36   enum Type
37   {
38     REGULAR_IMAGE,
39     N_PATCH,
40     SVG,
41     GIF
42   };
43 }
44
45 /**
46  * Helper to resolve URL type from the string
47  * @param[in] url to check
48  * @return UrlType
49  */
50 inline UrlType::Type ResolveUrlType( const std::string& url )
51 {
52   // if only one char in string, can only be regular image
53   const std::size_t count = url.size();
54   if( count > 0 )
55   {
56     // parsing from the end for better chance of early outs
57     enum { SUFFIX, HASH, HASH_DOT } state = SUFFIX;
58     char SVG[ 4 ] = { 'g', 'v', 's', '.' };
59     char GIF[ 4 ] = { 'f', 'i', 'g', '.' };
60     unsigned int svgScore = 0;
61     unsigned int gifScore = 0;
62     int index = count;
63     while( --index >= 0 )
64     {
65       const char currentChar = url[ index ];
66       const std::size_t offsetFromEnd = count - index - 1u;
67       if( ( offsetFromEnd < sizeof(SVG) )&&( tolower( currentChar ) == SVG[ offsetFromEnd ] ) )
68       {
69         // early out if SVG as can't be used in N patch for now
70         if( ++svgScore == sizeof(SVG) )
71         {
72           return UrlType::SVG;
73         }
74       }
75       if( ( offsetFromEnd < sizeof(GIF) )&&( tolower( currentChar ) == GIF[ offsetFromEnd ] ) )
76       {
77         // early out if GIF
78         if( ++gifScore == sizeof(GIF) )
79         {
80           return UrlType::GIF;
81         }
82       }
83       switch( state )
84       {
85         case SUFFIX:
86         {
87           if( '.' == currentChar )
88           {
89             state = HASH;
90           }
91           break;
92         }
93         case HASH:
94         {
95           if( ( '#' == currentChar ) || ( '9' == currentChar ) )
96           {
97             state = HASH_DOT;
98           }
99           else
100           {
101             // early out, not a valid N/9-patch URL
102             return UrlType::REGULAR_IMAGE;
103           }
104           break;
105         }
106         case HASH_DOT:
107         {
108           if( '.' == currentChar )
109           {
110             return UrlType::N_PATCH;
111           }
112           else
113           {
114             // early out, not a valid N/9-patch URL
115             return UrlType::REGULAR_IMAGE;
116           }
117           break;
118         }
119       }
120     }
121   }
122   // if we got here it is a regular image
123   return UrlType::REGULAR_IMAGE;
124 }
125
126 } // namespace Internal
127
128 } // namespace Toolkit
129
130 } // namespace Dali
131
132 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H */