MagickCore  7.1.2-29
Convert, Edit, Or Compose Bitmap Images
string-private.h
1 /*
2  Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License. You may
6  obtain a copy of the License at
7 
8  https://imagemagick.org/license/
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  MagickCore private string methods.
17 */
18 #ifndef MAGICKCORE_STRING_PRIVATE_H
19 #define MAGICKCORE_STRING_PRIVATE_H
20 
21 #include <string.h>
22 #include "MagickCore/locale_.h"
23 
24 #if defined(__cplusplus) || defined(c_plusplus)
25 extern "C" {
26 #endif
27 
28 /* Custom implementation so we can use sscanf without defining _CRT_SECURE_NO_WARNINGS */
29 static inline int MagickSscanf(const char* buffer,const char* format, ...)
30 {
31  int
32  ret;
33 
34  va_list
35  args;
36  va_start(args,format);
37 #if _MSC_VER
38  #pragma warning(push)
39  #pragma warning(disable:4996)
40 #endif
41  ret=vsscanf(buffer,format,args);
42 #if _MSC_VER
43  #pragma warning(pop)
44 #endif
45  va_end(args);
46  return(ret);
47 }
48 
49 static inline double SiPrefixToDoubleInterval(const char *string,
50  const double interval)
51 {
52  char
53  *q;
54 
55  double
56  value;
57 
58  value=InterpretSiPrefixValue(string,&q);
59  if (*q == '%')
60  value*=interval/100.0;
61  return(value);
62 }
63 
64 static inline double StringToDouble(const char *magick_restrict string,
65  char *magick_restrict *sentinel)
66 {
67  return(InterpretLocaleValue(string,sentinel));
68 }
69 
70 static inline float StringToFloat(const char *magick_restrict string,
71  char *magick_restrict *sentinel)
72 {
73  return((float) InterpretLocaleValue(string,sentinel));
74 }
75 
76 static inline const char *StringLocateSubstring(const char *haystack,
77  const char *needle)
78 {
79 #if defined(MAGICKCORE_HAVE_STRCASESTR)
80  return(strcasestr(haystack,needle));
81 #else
82  {
83  size_t
84  length_needle,
85  length_haystack;
86 
87  size_t
88  i;
89 
90  if (!haystack || !needle)
91  return(NULL);
92  length_needle=strlen(needle);
93  length_haystack=strlen(haystack)-length_needle+1;
94  for (i=0; i < length_haystack; i++)
95  {
96  size_t
97  j;
98 
99  for (j=0; j < length_needle; j++)
100  {
101  unsigned char c1 = (unsigned char) haystack[i+j];
102  unsigned char c2 = (unsigned char) needle[j];
103  if (toupper((int) c1) != toupper((int) c2))
104  goto next;
105  }
106  return((char *) haystack+i);
107  next:
108  ;
109  }
110  return((char *) NULL);
111  }
112 #endif
113 }
114 
115 static inline double StringToDoubleInterval(const char *string,
116  const double interval)
117 {
118  char
119  *q;
120 
121  double
122  value;
123 
124  value=InterpretLocaleValue(string,&q);
125  if (*q == '%')
126  value*=interval/100.0;
127  return(value);
128 }
129 
130 static inline int StringToInteger(const char *magick_restrict value)
131 {
132  if (value == (const char *) NULL)
133  return(0);
134  return((int) strtol(value,(char **) NULL,10));
135 }
136 
137 static inline long StringToLong(const char *magick_restrict value)
138 {
139  if (value == (const char *) NULL)
140  return(0);
141  return(strtol(value,(char **) NULL,10));
142 }
143 
144 static inline MagickOffsetType StringToMagickOffsetType(const char *string,
145  const double interval)
146 {
147  double
148  value;
149 
150  value=SiPrefixToDoubleInterval(string,interval);
151  if (value >= (double) MagickULLConstant(~0))
152  return((MagickOffsetType) MagickULLConstant(~0));
153  return((MagickOffsetType) value);
154 }
155 
156 static inline MagickSizeType StringToMagickSizeType(const char *string,
157  const double interval)
158 {
159  double
160  value;
161 
162  value=SiPrefixToDoubleInterval(string,interval);
163  if (value >= (double) MagickULLConstant(~0))
164  return(MagickULLConstant(~0));
165  return((MagickSizeType) value);
166 }
167 
168 static inline size_t StringToSizeType(const char *string,const double interval)
169 {
170  double
171  value;
172 
173  value=SiPrefixToDoubleInterval(string,interval);
174  if (value >= (double) MagickULLConstant(~0))
175  return(~0UL);
176  return((size_t) value);
177 }
178 
179 static inline unsigned long StringToUnsignedLong(
180  const char *magick_restrict value)
181 {
182  if (value == (const char *) NULL)
183  return(0);
184  return(strtoul(value,(char **) NULL,10));
185 }
186 
187 #if defined(__cplusplus) || defined(c_plusplus)
188 }
189 #endif
190 
191 #endif