MagickCore 7.0.10
string.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% SSSSS TTTTT RRRR IIIII N N GGGG %
7% SS T R R I NN N G %
8% SSS T RRRR I N N N G GGG %
9% SS T R R I N NN G G %
10% SSSSS T R R IIIII N N GGGG %
11% %
12% %
13% Wizard's Toolkit String Methods %
14% %
15% Software Design %
16% Cristy %
17% March 2003 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/blob.h"
45#include "wizard/cipher.h"
46#include "wizard/crc64.h"
47#include "wizard/exception.h"
49#include "wizard/memory_.h"
51#include "wizard/string_.h"
53
54/*
55 Structure declarations.
56*/
58{
59 char
61
62 unsigned char
64
65 size_t
67
68 time_t
70
71 size_t
73};
74
75/*
76 Static declarations.
77*/
78#if !defined(WIZARDSTOOLKIT_HAVE_STRCASECMP) || !defined(WIZARDSTOOLKIT_HAVE_STRNCASECMP)
79static const unsigned char
81 {
82 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
83 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
84 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
85 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
86 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
87 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
88 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
89 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
90 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
91 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
92 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
93 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
94 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
95 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
96 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
97 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
98 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
99 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
100 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
101 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
102 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
103 0xfc, 0xfd, 0xfe, 0xff,
104 };
105#endif
106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% A c q u i r e S t r i n g %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% AcquireString() returns an new extented string, containing a clone of the
119% given string.
120%
121% An extended string is the string length, plus an extra WizardPathExtent space
122% to allow for the string to be activally worked on.
123%
124% The format of the AcquireString method is:
125%
126% char *AcquireString(const char *source)
127%
128% A description of each parameter follows:
129%
130% o source: A character string.
131%
132*/
133WizardExport char *AcquireString(const char *source)
134{
135 char
136 *destination;
137
138 size_t
139 length;
140
141 length=0;
142 if (source != (char *) NULL)
143 length+=strlen(source);
144 if (~length < WizardPathExtent)
145 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
146 destination=(char *) AcquireQuantumMemory(length+WizardPathExtent,
147 sizeof(*destination));
148 if (destination == (char *) NULL)
149 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
150 *destination='\0';
151 if (source != (char *) NULL)
152 (void) memcpy(destination,source,length*sizeof(*destination));
153 destination[length]='\0';
154 return(destination);
155}
156
157/*
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159% %
160% %
161% %
162% A c q u i r e S t r i n g I n f o %
163% %
164% %
165% %
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167%
168% AcquireStringInfo() allocates the StringInfo structure.
169%
170% The format of the AcquireStringInfo method is:
171%
172% StringInfo *AcquireStringInfo(const size_t length)
173%
174% A description of each parameter follows:
175%
176% o length: The string length.
177%
178*/
180{
182 *string_info;
183
184 string_info=(StringInfo *) AcquireWizardMemory(sizeof(*string_info));
185 if (string_info == (StringInfo *) NULL)
186 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
187 (void) memset(string_info,0,sizeof(*string_info));
188 string_info->timestamp=time((time_t *) NULL);
189 string_info->signature=WizardSignature;
190 string_info->length=length;
191 string_info->datum=(unsigned char *) NULL;
192 if (~string_info->length >= (MaxCipherBlocksize-1))
193 string_info->datum=(unsigned char *) AcquireQuantumMemory(
194 string_info->length+MaxCipherBlocksize,sizeof(*string_info->datum));
195 if (string_info->datum == (unsigned char *) NULL)
196 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
197 (void) memset(string_info->datum,0,(length+MaxCipherBlocksize)*
198 sizeof(*string_info->datum));
199 return(string_info);
200}
201
202/*
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204% %
205% %
206% %
207% B l o b T o S t r i n g I n f o %
208% %
209% %
210% %
211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212%
213% BlobToStringInfo() returns the contents of a blob as a string.
214%
215% The format of the BlobToStringInfo method is:
216%
217% StringInfo *BlobToStringInfo(const void *blob,const size_t length)
218%
219% A description of each parameter follows:
220%
221% o blob: the blob.
222%
223% o length: the length of the blob.
224%
225*/
226WizardExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
227{
229 *string_info;
230
231 string_info=AcquireStringInfo(0);
232 if (~length < WizardPathExtent)
233 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
234 string_info->length=length;
235 if (string_info->datum == (unsigned char *) NULL)
236 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
237 WizardPathExtent,sizeof(*string_info->datum));
238 else
239 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
240 length+WizardPathExtent,sizeof(*string_info->datum));
241 if (string_info->datum == (unsigned char *) NULL)
242 {
243 string_info=DestroyStringInfo(string_info);
244 return((StringInfo *) NULL);
245 }
246 if (blob != (const void *) NULL)
247 (void) memcpy(string_info->datum,blob,length);
248 else
249 (void) memset(string_info->datum,0,length*sizeof(*string_info->datum));
250 (void) memset(string_info->datum+length,0,WizardPathExtent*
251 sizeof(*string_info->datum));
252 return(string_info);
253}
254
255/*
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257% %
258% %
259% %
260% C l o n e S t r i n g %
261% %
262% %
263% %
264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265%
266% CloneString() allocates memory for the destination string and copies
267% the source string to that memory location.
268%
269% The format of the CloneString method is:
270%
271% char *CloneString(char **destination,const char *source)
272%
273% A description of each parameter follows:
274%
275% o destination: A pointer to a character string.
276%
277% o source: A character string.
278%
279*/
280WizardExport char *CloneString(char **destination,const char *source)
281{
282 size_t
283 length;
284
285 WizardAssert(StringDomain,destination != (char **) NULL);
286 if (source == (const char *) NULL)
287 {
288 if (*destination != (char *) NULL)
289 *destination=DestroyString(*destination);
290 return(*destination);
291 }
292 if (*destination == (char *) NULL)
293 {
294 *destination=AcquireString(source);
295 return(*destination);
296 }
297 length=strlen(source);
298 if (~length < WizardPathExtent)
299 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
300 *destination=(char *) ResizeQuantumMemory(*destination,length+WizardPathExtent,
301 sizeof(**destination));
302 if (*destination == (char *) NULL)
303 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
304 if (length != 0)
305 (void) memcpy(*destination,source,length*sizeof(**destination));
306 (*destination)[length]='\0';
307 return(*destination);
308}
309
310/*
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312% %
313% %
314% %
315% C l o n e S t r i n g I n f o %
316% %
317% %
318% %
319%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320%
321% CloneStringInfo() clones a copy of the StringInfo structure.
322%
323% The format of the CloneStringInfo method is:
324%
325% StringInfo *CloneStringInfo(const StringInfo *string_info)
326%
327% A description of each parameter follows:
328%
329% o string_info: The string info.
330%
331*/
333{
335 *clone_info;
336
337 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
339 clone_info=AcquireStringInfo(string_info->length);
340 if (string_info->length != 0)
341 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+
343 return(clone_info);
344}
345
346/*
347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348% %
349% %
350% %
351% C o m p a r e S t r i n g I n f o %
352% %
353% %
354% %
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356%
357% CompareStringInfo() compares the two datums target and source. It returns
358% an integer less than, equal to, or greater than zero if target is found,
359% respectively, to be less than, to match, or be greater than source.
360%
361% The format of the CompareStringInfo method is:
362%
363% int CompareStringInfo(const StringInfo *target,const StringInfo *source)
364%
365% A description of each parameter follows:
366%
367% o target: The target string.
368%
369% o source: The source string.
370%
371*/
373 const StringInfo *source)
374{
375 int
376 status;
377
378 WizardAssert(StringDomain,target != (StringInfo *) NULL);
380 WizardAssert(StringDomain,source != (StringInfo *) NULL);
382 status=memcmp(target->datum,source->datum,WizardMin(target->length,
383 source->length));
384 if (status != 0)
385 return(status);
386 if (target->length == source->length)
387 return(0);
388 return(target->length < source->length ? -1 : 1);
389}
390
391/*
392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393% %
394% %
395% %
396% C o n c a t e n a t e S t r i n g %
397% %
398% %
399% %
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401%
402% ConcatenateString() appends a copy of string source, including the
403% terminating null character, to the end of string destination.
404%
405% The format of the ConcatenateString method is:
406%
407% WizardBooleanType ConcatenateString(char **destination,
408% const char *source)
409%
410% A description of each parameter follows:
411%
412% o destination: A pointer to a character string.
413%
414% o source: A character string.
415%
416*/
418 const char *source)
419{
420 size_t
421 destination_length,
422 length,
423 source_length;
424
425 assert(destination != (char **) NULL);
426 if (*destination == (char *) NULL)
427 {
428 *destination=AcquireString(source);
429 return(WizardTrue);
430 }
431 destination_length=strlen(*destination);
432 source_length=strlen(source);
433 length=destination_length;
434 if (~length < source_length)
435 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
436 length+=source_length;
437 if (~length < WizardPathExtent)
438 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
439 *destination=(char *) ResizeQuantumMemory(*destination,
440 OverAllocateMemory(length+WizardPathExtent),sizeof(**destination));
441 if (*destination == (char *) NULL)
442 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
443 if (source_length != 0)
444 (void) memcpy((*destination)+destination_length,source,source_length);
445 (*destination)[length]='\0';
446 return(WizardTrue);
447}
448
449/*
450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451% %
452% %
453% %
454% C o n c a t e n a t e W i z a r d S t r i n g %
455% %
456% %
457% %
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459%
460% ConcatenateWizardString() concatenates the source string to the destination
461% string. The destination buffer is always null-terminated even if the
462% string must be truncated.
463%
464% The format of the ConcatenateWizardString method is:
465%
466% size_t ConcatenateWizardString(char *destination,const char *source,
467% const size_t length)
468%
469% A description of each parameter follows:
470%
471% o destination: The destination string.
472%
473% o source: The source string.
474%
475% o length: The length of the destination string.
476%
477*/
478WizardExport size_t ConcatenateWizardString(char *destination,
479 const char *source,const size_t length)
480{
481#if !defined(WIZARDSTOOLKIT_HAVE_STRLCAT)
482 char
483 *q;
484
485 const char
486 *p;
487
488 size_t
489 i;
490
491 size_t
492 count;
493
494 assert(destination != (char *) NULL);
495 assert(source != (const char *) NULL);
496 assert(length >= 1);
497 p=source;
498 q=destination;
499 i=length;
500 while ((i-- != 0) && (*q != '\0'))
501 q++;
502 count=(size_t) (q-destination);
503 i=length-count;
504 if (i == 0)
505 return(count+strlen(p));
506 while (*p != '\0')
507 {
508 if (i != 1)
509 {
510 *q++=(*p);
511 i--;
512 }
513 p++;
514 }
515 *q='\0';
516 return(count+(p-source));
517#else
518 return(strlcat(destination,source,length));
519#endif
520}
521
522/*
523%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524% %
525% %
526% %
527% C o n f i g u r e F i l e T o S t r i n g I n f o %
528% %
529% %
530% %
531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532%
533% ConfigureFileToStringInfo() returns the contents of a configure file as a
534% string.
535%
536% The format of the ConfigureFileToStringInfo method is:
537%
538% StringInfo *ConfigureFileToStringInfo(const char *filename)
539% ExceptionInfo *exception)
540%
541% A description of each parameter follows:
542%
543% o filename: The filename.
544%
545*/
547{
548 char
549 *string;
550
551 int
552 file;
553
555 offset;
556
557 size_t
558 length;
559
561 *string_info;
562
563 void
564 *map;
565
566 WizardAssert(StringDomain,filename != (const char *) NULL);
567 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
568 if (file == -1)
569 return((StringInfo *) NULL);
570 offset=(WizardOffsetType) lseek(file,0,SEEK_END);
571 if ((offset < 0) || (offset != (WizardOffsetType) ((ssize_t) offset)))
572 {
573 file=close(file)-1;
574 return((StringInfo *) NULL);
575 }
576 length=(size_t) offset;
577 string=(char *) NULL;
578 if (~length >= (MaxCipherBlocksize-1))
579 string=(char *) AcquireQuantumMemory(length+MaxCipherBlocksize,
580 sizeof(*string));
581 if (string == (char *) NULL)
582 {
583 file=close(file)-1;
584 return((StringInfo *) NULL);
585 }
586 map=MapBlob(file,ReadMode,0,length);
587 if (map != (void *) NULL)
588 {
589 (void) memcpy(string,map,length);
590 (void) UnmapBlob(map,length);
591 }
592 else
593 {
594 size_t
595 i;
596
597 ssize_t
598 count;
599
600 (void) lseek(file,0,SEEK_SET);
601 for (i=0; i < length; i+=count)
602 {
603 count=read(file,string+i,(size_t) WizardMin(length-i,(size_t)
604 SSIZE_MAX));
605 if (count <= 0)
606 {
607 count=0;
608 if (errno != EINTR)
609 break;
610 }
611 }
612 if (i < length)
613 {
614 file=close(file)-1;
615 string=(char *) RelinquishWizardMemory(string);
616 return((StringInfo *) NULL);
617 }
618 }
619 string[length]='\0';
620 file=close(file)-1;
621 string_info=AcquireStringInfo(0);
622 (void) CopyWizardString(string_info->path,filename,WizardPathExtent);
623 string_info->length=length;
624 if (string_info->datum != (unsigned char *) NULL)
625 string_info->datum=(unsigned char *) RelinquishWizardMemory(
626 string_info->datum);
627 string_info->datum=(unsigned char *) string;
628 return(string_info);
629}
630
631/*
632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633% %
634% %
635% %
636% C o n s t a n t S t r i n g %
637% %
638% %
639% %
640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641%
642% ConstantString() allocates memory for a string and copies the source string
643% to that memory location (and returns it). Use it for strings that you do
644% do not expect to change over its lifetime.
645%
646% The format of the ConstantString method is:
647%
648% char *ConstantString(const char *source)
649%
650% A description of each parameter follows:
651%
652% o source: A character string.
653%
654*/
655WizardExport char *ConstantString(const char *source)
656{
657 char
658 *destination;
659
660 size_t
661 length;
662
663 length=0;
664 if (source != (char *) NULL)
665 length+=strlen(source);
666 destination=(char *) NULL;
667 if (~length >= 1UL)
668 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
669 if (destination == (char *) NULL)
670 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
671 *destination='\0';
672 if (source != (char *) NULL)
673 (void) memcpy(destination,source,length*sizeof(*destination));
674 destination[length]='\0';
675 return(destination);
676}
677
678/*
679%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680% %
681% %
682% %
683% C o n c a t e n a t e S t r i n g I n f o %
684% %
685% %
686% %
687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688%
689% ConcatenateStringInfo() concatenates the source string to the destination
690% string.
691%
692% The format of the ConcatentateStringInfo method is:
693%
694% void ConcatentateStringInfo(StringInfo *string_info,
695% const StringInfo *source)
696%
697% A description of each parameter follows:
698%
699% o string_info: The string info.
700%
701% o source: The source string.
702%
703*/
705 const StringInfo *source)
706{
707 size_t
708 length;
709
710 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
712 WizardAssert(StringDomain,source != (const StringInfo *) NULL);
713 length=string_info->length;
714 if (~length < source->length)
715 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
716 length+=source->length;
717 if (~length < WizardPathExtent)
718 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
719 string_info->length=length;
720 if (string_info->datum == (unsigned char *) NULL)
721 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
722 WizardPathExtent,sizeof(*string_info->datum));
723 else
724 string_info->datum=(unsigned char *) ResizeQuantumMemory(
725 string_info->datum,OverAllocateMemory(length+WizardPathExtent),
726 sizeof(*string_info->datum));
727 if (string_info->datum == (unsigned char *) NULL)
728 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
729 (void) memcpy(string_info->datum+length,source->datum,source->length);
730}
731
732/*
733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734% %
735% %
736% %
737% C o p y W i z a r d S t r i n g %
738% %
739% %
740% %
741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742%
743% CopyWizardString() copies the source string to the destination string. The
744% destination buffer is always null-terminated even if the string must be
745% truncated. The return value is the minimum of the ource string length
746% or the length parameter.
747%
748% The format of the CopyWizardString method is:
749%
750% size_t CopyWizardString(const char *destination,char *source,
751% const size_t length)
752%
753% A description of each parameter follows:
754%
755% o destination: The destination string.
756%
757% o source: The source string.
758%
759% o length: The length of the destination string.
760%
761*/
762WizardExport size_t CopyWizardString(char *destination,const char *source,
763 const size_t length)
764{
765 char
766 *q;
767
768 const char
769 *p;
770
771 size_t
772 n;
773
774 p=source;
775 q=destination;
776 for (n=length; n > 4; n-=4)
777 {
778 *q=(*p++);
779 if (*q == '\0')
780 return((size_t) (p-source-1));
781 q++;
782 *q=(*p++);
783 if (*q == '\0')
784 return((size_t) (p-source-1));
785 q++;
786 *q=(*p++);
787 if (*q == '\0')
788 return((size_t) (p-source-1));
789 q++;
790 *q=(*p++);
791 if (*q == '\0')
792 return((size_t) (p-source-1));
793 q++;
794 }
795 if (n != 0)
796 for (n--; n != 0; n--)
797 {
798 *q=(*p++);
799 if (*q == '\0')
800 return((size_t) (p-source-1));
801 q++;
802 }
803 if (length != 0)
804 *q='\0';
805 return((size_t) (p-source-1));
806}
807
808/*
809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810% %
811% %
812% %
813% D e s t r o y S t r i n g %
814% %
815% %
816% %
817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818%
819% DestroyString() destorys memory associated with a string.
820%
821% The format of the DestroyString method is:
822%
823% char *DestroyString(char *string)
824%
825% A description of each parameter follows:
826%
827% o string: The string.
828%
829*/
830WizardExport char *DestroyString(char *string)
831{
832 return((char *) RelinquishWizardMemory(string));
833}
834
835/*
836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837% %
838% %
839% %
840% D e s t r o y S t r i n g I n f o %
841% %
842% %
843% %
844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845%
846% DestroyStringInfo() zeros memory associated with the StringInfo structure.
847%
848% The format of the DestroyStringInfo method is:
849%
850% StringInfo *DestroyStringInfo(StringInfo *string_info)
851%
852% A description of each parameter follows:
853%
854% o string_info: The string info.
855%
856*/
858{
859 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
861 if (string_info->datum != (unsigned char *) NULL)
862 string_info->datum=(unsigned char *) RelinquishWizardMemory(
863 string_info->datum);
864 string_info->signature=(~WizardSignature);
865 string_info=(StringInfo *) RelinquishWizardMemory(string_info);
866 return(string_info);
867}
868
869/*
870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871% %
872% %
873% %
874% D e s t r o y S t r i n g L i s t %
875% %
876% %
877% %
878%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879%
880% DestroyStringList() zeros memory associated with a string list.
881%
882% The format of the DestroyStringList method is:
883%
884% char **DestroyStringList(char **list)
885%
886% A description of each parameter follows:
887%
888% o list: The string list.
889%
890*/
892{
893 ssize_t
894 i;
895
896 assert(list != (char **) NULL);
897 for (i=0; list[i] != (char *) NULL; i++)
898 list[i]=DestroyString(list[i]);
899 list=(char **) RelinquishWizardMemory(list);
900 return(list);
901}
902
903/*
904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905% %
906% %
907% %
908% F i l e T o S t r i n g %
909% %
910% %
911% %
912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913%
914% FileToString() returns the contents of a file as a string.
915%
916% The format of the FileToString method is:
917%
918% char *FileToString(const char *filename,const size_t extent,
919% ExceptionInfo *exception)
920%
921% A description of each parameter follows:
922%
923% o filename: The filename.
924%
925% o extent: Maximum length of the string.
926%
927% o exception: Return any errors or warnings in this structure.
928%
929*/
930WizardExport char *FileToString(const char *filename,const size_t extent,
931 ExceptionInfo *exception)
932{
933 size_t
934 length;
935
936 WizardAssert(StringDomain,filename != (const char *) NULL);
937 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",filename);
938 WizardAssert(StringDomain,exception != (ExceptionInfo *) NULL);
939 return((char *) FileToBlob(filename,extent,&length,exception));
940}
941
942/*
943%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944% %
945% %
946% %
947% F i l e T o S t r i n g I n f o %
948% %
949% %
950% %
951%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
952%
953% FileToStringInfo() returns the contents of a file as a string.
954%
955% The format of the FileToStringInfo method is:
956%
957% StringInfo *FileToStringInfo(const char *filename,const size_t extent,
958% ExceptionInfo *exception)
959%
960% A description of each parameter follows:
961%
962% o filename: The filename.
963%
964% o extent: Maximum length of the string.
965%
966% o exception: Return any errors or warnings in this structure.
967%
968*/
970 const size_t extent,ExceptionInfo *exception)
971{
973 *string_info;
974
975 WizardAssert(StringDomain,filename != (const char *) NULL);
976 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",filename);
977 WizardAssert(StringDomain,exception != (ExceptionInfo *) NULL);
978 string_info=AcquireStringInfo(0);
979 (void) CopyWizardString(string_info->path,filename,WizardPathExtent);
980 if (string_info->datum != (unsigned char *) NULL)
981 string_info->datum=(unsigned char *) RelinquishWizardMemory(
982 string_info->datum);
983 string_info->datum=(unsigned char *) FileToBlob(filename,extent,
984 &string_info->length,exception);
985 if (string_info->datum == (unsigned char *) NULL)
986 {
987 string_info=DestroyStringInfo(string_info);
988 return((StringInfo *) NULL);
989 }
990 return(string_info);
991}
992
993/*
994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995% %
996% %
997% %
998% F o r m a t W i z a r d S i z e %
999% %
1000% %
1001% %
1002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003%
1004% FormatWizardSize() converts a size to a human readable format, for example,
1005% 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1006% 1000.
1007%
1008% The format of the FormatWizardSize method is:
1009%
1010% ssize_t FormatWizardSize(const WizardSizeType size,
1011% const WizardBooleanType bi,const size_t length,char *format)
1012%
1013% A description of each parameter follows:
1014%
1015% o size: convert this size to a human readable format.
1016%
1017% o bi: use power of two rather than power of ten.
1018%
1019% o length: The maximum length of the string.
1020%
1021% o format: human readable format.
1022%
1023*/
1025 const WizardBooleanType bi,const size_t length,char *format)
1026{
1027 const char
1028 **units;
1029
1030 double
1031 bytes,
1032 extent;
1033
1034 ssize_t
1035 count;
1036
1037 ssize_t
1038 i,
1039 j;
1040
1041 static const char
1042 *bi_units[] =
1043 {
1044 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1045 },
1046 *traditional_units[] =
1047 {
1048 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1049 };
1050
1051 bytes=1000.0;
1052 units=traditional_units;
1053 if (bi != WizardFalse)
1054 {
1055 bytes=1024.0;
1056 units=bi_units;
1057 }
1058#if defined(_MSC_VER) && (_MSC_VER == 1200)
1059 extent=(double) ((WizardOffsetType) size);
1060#else
1061 extent=(double) size;
1062#endif
1063 for (i=0; (extent >= bytes) && (units[i+1] != (const char *) NULL); i++)
1064 extent/=bytes;
1065 count=0;
1066 for (j=2; j < 12; j++)
1067 {
1068 count=FormatLocaleString(format,length,"%.*g%sB",(int) (i+j),extent,
1069 units[i]);
1070 if (strchr(format,'+') == (char *) NULL)
1071 break;
1072 }
1073 return(count);
1074}
1075
1076/*
1077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078% %
1079% %
1080% %
1081% F o r m a t W i z a r d T i m e %
1082% %
1083% %
1084% %
1085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086%
1087% FormatWizardTime() returns the specified time in the Internet date/time
1088% format and the length of the timestamp.
1089%
1090% The format of the FormatWizardTime method is:
1091%
1092% ssize_t FormatWizardTime(const time_t time,const size_t length,
1093% char *timestamp)
1094%
1095% A description of each parameter follows.
1096%
1097% o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1098% measured in seconds.
1099%
1100% o length: The maximum length of the string.
1101%
1102% o timestamp: Return the Internet date/time here.
1103%
1104*/
1105WizardExport ssize_t FormatWizardTime(const time_t time,const size_t length,
1106 char *timestamp)
1107{
1108 ssize_t
1109 count;
1110
1111 struct tm
1112 gm_time,
1113 local_time;
1114
1115 time_t
1116 timezone;
1117
1118 assert(timestamp != (char *) NULL);
1119#if defined(WIZARDSTOOLKIT_HAVE_LOCALTIME_R)
1120 (void) localtime_r(&time,&local_time);
1121#else
1122 {
1123 struct tm
1124 *my_time;
1125
1126 my_time=localtime(&time);
1127 if (my_time != (struct tm *) NULL)
1128 (void) memcpy(&local_time,my_time,sizeof(local_time));
1129 }
1130#endif
1131#if defined(WIZARDSTOOLKIT_HAVE_GMTIME_R)
1132 (void) gmtime_r(&time,&gm_time);
1133#else
1134 {
1135 struct tm
1136 *my_time;
1137
1138 my_time=gmtime(&time);
1139 if (my_time != (struct tm *) NULL)
1140 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1141 }
1142#endif
1143 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1144 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1145 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1146 (local_time.tm_yday-gm_time.tm_yday)));
1147 count=FormatLocaleString(timestamp,length,
1148 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1149 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1150 local_time.tm_min,local_time.tm_sec,(long) timezone);
1151 return(count);
1152}
1153
1154/*
1155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156% %
1157% %
1158% %
1159% G e t E n v i r o n m e n t V a l u e %
1160% %
1161% %
1162% %
1163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164%
1165% GetEnvironmentValue() returns the environment string that matches the
1166% specified name.
1167%
1168% The format of the GetEnvironmentValue method is:
1169%
1170% char *GetEnvironmentValue(const char *name)
1171%
1172% A description of each parameter follows:
1173%
1174% o name: the environment name.
1175%
1176*/
1177WizardExport char *GetEnvironmentValue(const char *name)
1178{
1179 const char
1180 *environment;
1181
1182 environment=getenv(name);
1183 if (environment == (const char *) NULL)
1184 return((char *) NULL);
1185 return(ConstantString(environment));
1186}
1187
1188/*
1189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1190% %
1191% %
1192% %
1193% G e t S t r i n g I n f o C R C %
1194% %
1195% %
1196% %
1197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1198%
1199% GetStringInfoCRC() returns the CRC-64 of a string.
1200%
1201% The format of the GetStringInfo method is:
1202%
1203% WizardSizeType GetStringInfo(const StringInfo *string_info)
1204%
1205% A description of each parameter follows:
1206%
1207% o string_info: The string info.
1208%
1209*/
1211{
1212 CRC64Info
1213 *crc_info;
1214
1216 crc;
1217
1218 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1220 crc_info=AcquireCRC64Info();
1221 (void) InitializeCRC64(crc_info);
1222 (void) UpdateCRC64(crc_info,string_info);
1223 (void) FinalizeCRC64(crc_info);
1224 crc=GetCRC64CyclicRedundancyCheck(crc_info);
1225 crc_info=DestroyCRC64Info(crc_info);
1226 return(crc);
1227}
1228
1229/*
1230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231% %
1232% %
1233% %
1234% G e t S t r i n g I n f o D a t u m %
1235% %
1236% %
1237% %
1238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239%
1240% GetStringInfoDatum() returns the datum associated with the string.
1241%
1242% The format of the GetStringInfoDatum method is:
1243%
1244% unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1245%
1246% A description of each parameter follows:
1247%
1248% o string_info: the string info.
1249%
1250*/
1251WizardExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1252{
1253 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1255 return(string_info->datum);
1256}
1257
1258/*
1259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1260% %
1261% %
1262% %
1263% G e t S t r i n g I n f o L e n g t h %
1264% %
1265% %
1266% %
1267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268%
1269% GetStringInfoLength() returns the string length.
1270%
1271% The format of the GetStringInfoLength method is:
1272%
1273% size_t GetStringInfoLength(const StringInfo *string_info)
1274%
1275% A description of each parameter follows:
1276%
1277% o string_info: the string info.
1278%
1279*/
1281{
1282 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1284 return(string_info->length);
1285}
1286
1287/*
1288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1289% %
1290% %
1291% %
1292% G e t S t r i n g I n f o P a t h %
1293% %
1294% %
1295% %
1296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1297%
1298% GetStringInfoPath() returns the path associated with the string.
1299%
1300% The format of the GetStringInfoPath method is:
1301%
1302% const char *GetStringInfoPath(const StringInfo *string_info)
1303%
1304% A description of each parameter follows:
1305%
1306% o string_info: the string info.
1307%
1308*/
1309WizardExport const char *GetStringInfoPath(const StringInfo *string_info)
1310{
1311 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1313 return(string_info->path);
1314}
1315
1316/*
1317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1318% %
1319% %
1320% %
1321% H e x S t r i n g T o S t r i n g I n f o %
1322% %
1323% %
1324% %
1325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1326%
1327% HexStringToStringInfo() converts a string to a StringInfo type.
1328%
1329% The format of the HexStringToStringInfo method is:
1330%
1331% StringInfo *HexStringToStringInfo(const char *string)
1332%
1333% A description of each parameter follows:
1334%
1335% o string: The string.
1336%
1337*/
1339{
1340 const unsigned char
1341 *p;
1342
1343 ssize_t
1344 i;
1345
1346 unsigned char
1347 *q;
1348
1349 unsigned char
1350 hex_digits[256];
1351
1353 *string_info;
1354
1355 WizardAssert(StringDomain,string != (const char *) NULL);
1356 string_info=AcquireStringInfo(strlen(string)/2);
1357 (void) memset(hex_digits,0,sizeof(hex_digits));
1358 hex_digits[(int) '0']=0;
1359 hex_digits[(int) '1']=1;
1360 hex_digits[(int) '2']=2;
1361 hex_digits[(int) '3']=3;
1362 hex_digits[(int) '4']=4;
1363 hex_digits[(int) '5']=5;
1364 hex_digits[(int) '6']=6;
1365 hex_digits[(int) '7']=7;
1366 hex_digits[(int) '8']=8;
1367 hex_digits[(int) '9']=9;
1368 hex_digits[(int) 'a']=10;
1369 hex_digits[(int) 'b']=11;
1370 hex_digits[(int) 'c']=12;
1371 hex_digits[(int) 'd']=13;
1372 hex_digits[(int) 'e']=14;
1373 hex_digits[(int) 'f']=15;
1374 hex_digits[(int) 'A']=10;
1375 hex_digits[(int) 'B']=11;
1376 hex_digits[(int) 'C']=12;
1377 hex_digits[(int) 'D']=13;
1378 hex_digits[(int) 'E']=14;
1379 hex_digits[(int) 'F']=15;
1380 p=(unsigned char *) string;
1381 q=string_info->datum;
1382 for (i=0; i < (ssize_t) string_info->length; i++)
1383 {
1384 *q=hex_digits[*p++] << 4;
1385 *q|=hex_digits[*p++];
1386 q++;
1387 }
1388 return(string_info);
1389}
1390
1391/*
1392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393% %
1394% %
1395% %
1396+ I n t e r p r e t S i P r e f i x V a l u e %
1397% %
1398% %
1399% %
1400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401%
1402% InterpretSiPrefixValue() converts the initial portion of the string to a
1403% double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1404% etc.).
1405%
1406% The format of the InterpretSiPrefixValue method is:
1407%
1408% double InterpretSiPrefixValue(const char *value,char **sentinal)
1409%
1410% A description of each parameter follows:
1411%
1412% o value: the string value.
1413%
1414% o sentinal: if sentinal is not NULL, return a pointer to the character
1415% after the last character used in the conversion.
1416%
1417*/
1418WizardExport double InterpretSiPrefixValue(const char *string,char **sentinal)
1419{
1420 char
1421 *q;
1422
1423 double
1424 value;
1425
1426 value=InterpretLocaleValue(string,&q);
1427 if (q != string)
1428 {
1429 if ((*q >= 'E') && (*q <= 'z'))
1430 {
1431 double
1432 e;
1433
1434 switch ((int) ((unsigned char) *q))
1435 {
1436 case 'y': e=(-24.0); break;
1437 case 'z': e=(-21.0); break;
1438 case 'a': e=(-18.0); break;
1439 case 'f': e=(-15.0); break;
1440 case 'p': e=(-12.0); break;
1441 case 'n': e=(-9.0); break;
1442 case 'u': e=(-6.0); break;
1443 case 'm': e=(-3.0); break;
1444 case 'c': e=(-2.0); break;
1445 case 'd': e=(-1.0); break;
1446 case 'h': e=2.0; break;
1447 case 'k': e=3.0; break;
1448 case 'K': e=3.0; break;
1449 case 'M': e=6.0; break;
1450 case 'G': e=9.0; break;
1451 case 'T': e=12.0; break;
1452 case 'P': e=15.0; break;
1453 case 'E': e=18.0; break;
1454 case 'Z': e=21.0; break;
1455 case 'Y': e=24.0; break;
1456 default: e=0.0; break;
1457 }
1458 if (e >= WizardEpsilon)
1459 {
1460 if (q[1] == 'i')
1461 {
1462 value*=pow(2.0,e/0.3);
1463 q+=2;
1464 }
1465 else
1466 {
1467 value*=pow(10.0,e);
1468 q++;
1469 }
1470 }
1471 }
1472 if ((*q == 'B') || (*q == 'P'))
1473 q++;
1474 }
1475 if (sentinal != (char **) NULL)
1476 *sentinal=q;
1477 return(value);
1478}
1479
1480/*
1481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1482% %
1483% %
1484% %
1485% L o c a l e C o m p a r e %
1486% %
1487% %
1488% %
1489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490%
1491% LocaleCompare() performs a case-insensitive comparison of two strings
1492% byte-by-byte, according to the ordering of the current locale encoding.
1493% LocaleCompare returns an integer greater than, equal to, or less than 0,
1494% if the string pointed to by p is greater than, equal to, or less than the
1495% string pointed to by q respectively. The sign of a non-zero return value
1496% is determined by the sign of the difference between the values of the first
1497% pair of bytes that differ in the strings being compared.
1498%
1499% The format of the LocaleCompare method is:
1500%
1501% int LocaleCompare(const char *p,const char *q)
1502%
1503% A description of each parameter follows:
1504%
1505% o p: A pointer to a character string.
1506%
1507% o q: A pointer to a character string to compare to p.
1508%
1509*/
1510WizardExport int LocaleCompare(const char *p,const char *q)
1511{
1512 if ((p == (char *) NULL) && (q == (char *) NULL))
1513 return(0);
1514 if (p == (char *) NULL)
1515 return(-1);
1516 if (q == (char *) NULL)
1517 return(1);
1518#if defined(WIZARDSTOOLKIT_HAVE_STRCASECMP)
1519 return(strcasecmp(p,q));
1520#else
1521 {
1522 int
1523 c,
1524 d;
1525
1526 for ( ; ; )
1527 {
1528 c=(int) *((unsigned char *) p);
1529 d=(int) *((unsigned char *) q);
1530 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1531 break;
1532 p++;
1533 q++;
1534 }
1535 return(AsciiMap[c]-(int) AsciiMap[d]);
1536 }
1537#endif
1538}
1539
1540/*
1541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1542% %
1543% %
1544% %
1545% L o c a l e L o w e r %
1546% %
1547% %
1548% %
1549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550%
1551% LocaleLower() transforms all of the characters in the supplied
1552% null-terminated string, changing all uppercase letters to lowercase.
1553%
1554% The format of the LocaleLower method is:
1555%
1556% void LocaleLower(char *string)
1557%
1558% A description of each parameter follows:
1559%
1560% o string: A pointer to the string to convert to lower-case Locale.
1561%
1562*/
1563WizardExport void LocaleLower(char *string)
1564{
1565 char
1566 *q;
1567
1568 assert(string != (char *) NULL);
1569 for (q=string; *q != '\0'; q++)
1570 *q=(char) tolower((int) *q);
1571}
1572
1573/*
1574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575% %
1576% %
1577% %
1578% L o c a l e N C o m p a r e %
1579% %
1580% %
1581% %
1582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1583%
1584% LocaleNCompare() performs a case-insensitive comparison of two
1585% strings byte-by-byte, according to the ordering of the current locale
1586% encoding. LocaleNCompare returns an integer greater than, equal to, or
1587% less than 0, if the string pointed to by p is greater than, equal to, or
1588% less than the string pointed to by q respectively. The sign of a non-zero
1589% return value is determined by the sign of the difference between the
1590% values of the first pair of bytes that differ in the strings being
1591% compared. The LocaleNCompare method makes the same comparison as
1592% LocaleCompare but looks at a maximum of n bytes. Bytes following a
1593% null byte are not compared.
1594%
1595% The format of the LocaleNCompare method is:
1596%
1597% int LocaleNCompare(const char *p,const char *q,const size_t n)
1598%
1599% A description of each parameter follows:
1600%
1601% o p: A pointer to a character string.
1602%
1603% o q: A pointer to a character string to compare to p.
1604%
1605% o length: The number of characters to compare in strings p and q.
1606%
1607*/
1608WizardExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1609{
1610 if ((p == (char *) NULL) && (q == (char *) NULL))
1611 return(0);
1612 if (p == (char *) NULL)
1613 return(-1);
1614 if (q == (char *) NULL)
1615 return(1);
1616#if defined(WIZARDSTOOLKIT_HAVE_STRNCASECMP)
1617 return(strncasecmp(p,q,length));
1618#else
1619 {
1620 int
1621 c,
1622 d;
1623
1624 size_t
1625 i;
1626
1627 for (i=length; i != 0; i--)
1628 {
1629 c=(int) *((unsigned char *) p);
1630 d=(int) *((unsigned char *) q);
1631 if (AsciiMap[c] != AsciiMap[d])
1632 return(AsciiMap[c]-(int) AsciiMap[d]);
1633 if (c == 0)
1634 return(0);
1635 p++;
1636 q++;
1637 }
1638 return(0);
1639 }
1640#endif
1641}
1642
1643/*
1644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645% %
1646% %
1647% %
1648% P r i n t W i z a r d S t r i n g %
1649% %
1650% %
1651% %
1652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1653%
1654% PrintWizardString() prints the string to the specified file.
1655%
1656% The format of the PrintWizardString method is:
1657%
1658% ssize_t PrintWizardString(FILE *,const char *format,...)
1659%
1660% A description of each parameter follows:
1661%
1662% o file: print to this file.
1663%
1664% o format: A string describing the format to use to write the remaining
1665% arguments.
1666%
1667*/
1668WizardExport ssize_t PrintWizardString(FILE *file,const char *format,...)
1669{
1670 char
1671 string[WizardPathExtent];
1672
1673 ssize_t
1674 length;
1675
1676 va_list
1677 operands;
1678
1679 va_start(operands,format);
1680 length=FormatLocaleStringList(string,WizardPathExtent,format,operands);
1681 va_end(operands);
1682 if (length < 0)
1683 return(-1);
1684 return((ssize_t) fwrite(string,(size_t) length,1,file));
1685}
1686
1687/*
1688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1689% %
1690% %
1691% %
1692% P r i n t S t r i n g I n f o %
1693% %
1694% %
1695% %
1696%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1697%
1698% PrintStringInfo() prints the string.
1699%
1700% The format of the PrintStringInfo method is:
1701%
1702% void PrintStringInfo(FILE *file,const char *id,
1703% const StringInfo *string_info)
1704%
1705% A description of each parameter follows:
1706%
1707% o id: The string id.
1708%
1709% o string_info: The string info.
1710%
1711*/
1712WizardExport void PrintStringInfo(FILE *file,const char *id,
1713 const StringInfo *string_info)
1714{
1715 unsigned char
1716 *p,
1717 *q;
1718
1719 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1721 q=string_info->datum+string_info->length;
1722 for (p=string_info->datum; p < q; p++)
1723 {
1724 if (((int) *p < 32) && (isspace((int) ((unsigned char) *p)) == 0))
1725 break;
1726 if (isascii((int) ((unsigned char) *p)) == 0)
1727 break;
1728 }
1729 (void) PrintWizardString(file,"%s(%.20g):\n",id,(double) string_info->length);
1730 if (p == q)
1731 for (p=string_info->datum; p < q; p++)
1732 (void) PrintWizardString(file,"%c",(int) *p);
1733 else
1734 for (p=string_info->datum; p < q; p++)
1735 (void) PrintWizardString(file,"%02lx",(unsigned long) *p);
1736 (void) PrintWizardString(file,"\n");
1737}
1738
1739/*
1740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741% %
1742% %
1743% %
1744% R e s e t S t r i n g I n f o %
1745% %
1746% %
1747% %
1748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749%
1750% ResetStringInfo() reset the string to all null bytes.
1751%
1752% The format of the ResetStringInfo method is:
1753%
1754% void ResetStringInfo(StringInfo *string_info)
1755%
1756% A description of each parameter follows:
1757%
1758% o string_info: The string info.
1759%
1760*/
1762{
1763 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1765 (void) memset(string_info->datum,0,string_info->length);
1766}
1767
1768/*
1769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1770% %
1771% %
1772% %
1773% S e t S t r i n g I n f o %
1774% %
1775% %
1776% %
1777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1778%
1779% SetStringInfo() copies the source string to the destination string.
1780%
1781% The format of the SetStringInfo method is:
1782%
1783% void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1784%
1785% A description of each parameter follows:
1786%
1787% o string_info: The string info.
1788%
1789% o source: The source string.
1790%
1791*/
1793 const StringInfo *source)
1794{
1795 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1797 WizardAssert(StringDomain,source != (StringInfo *) NULL);
1799 if (string_info->length == 0)
1800 return;
1801 (void) memset(string_info->datum,0,string_info->length);
1802 (void) memcpy(string_info->datum,source->datum,
1803 WizardMin(string_info->length,source->length));
1804}
1805
1806/*
1807%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808% %
1809% %
1810% %
1811% S e t S t r i n g I n f o D a t u m %
1812% %
1813% %
1814% %
1815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816%
1817% SetStringInfoDatum() copies bytes from the source string for the length of
1818% the destination string.
1819%
1820% The format of the SetStringInfoDatum method is:
1821%
1822% void SetStringInfoDatum(StringInfo *string_info,
1823% const unsigned char *source)
1824%
1825% A description of each parameter follows:
1826%
1827% o string_info: The string info.
1828%
1829% o source: The source string.
1830%
1831*/
1833 const unsigned char *source)
1834{
1835 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1837 if (string_info->length != 0)
1838 (void) memcpy(string_info->datum,source,string_info->length);
1839}
1840
1841/*
1842%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1843% %
1844% %
1845% %
1846% S e t S t r i n g I n f o L e n g t h %
1847% %
1848% %
1849% %
1850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1851%
1852% SetStringInfoLength() set the string length to the specified value.
1853%
1854% The format of the SetStringInfoLength method is:
1855%
1856% void SetStringInfoLength(StringInfo *string_info,const size_t length)
1857%
1858% A description of each parameter follows:
1859%
1860% o string_info: The string info.
1861%
1862% o length: The string length.
1863%
1864*/
1866 const size_t length)
1867{
1868 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1870 string_info->length=length;
1871 if (~length < MaxCipherBlocksize)
1872 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
1873 if (string_info->datum == (unsigned char *) NULL)
1874 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1875 MaxCipherBlocksize,sizeof(*string_info->datum));
1876 else
1877 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1878 length+MaxCipherBlocksize,sizeof(*string_info->datum));
1879 if (string_info->datum == (unsigned char *) NULL)
1880 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
1881}
1882
1883/*
1884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1885% %
1886% %
1887% %
1888% S e t S t r i n g I n f o D a t u m %
1889% %
1890% %
1891% %
1892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1893%
1894% SetStringInfoPath() sets the path associated with the string.
1895%
1896% The format of the SetStringInfoPath method is:
1897%
1898% void SetStringInfoPath(StringInfo *string_info,const char *path)
1899%
1900% A description of each parameter follows:
1901%
1902% o string_info: The string info.
1903%
1904% o path: The path.
1905%
1906*/
1907WizardExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1908{
1909 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1911 WizardAssert(StringDomain,path != (const char *) NULL);
1912 (void) CopyWizardString(string_info->path,path,WizardPathExtent);
1913}
1914
1915/*
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917% %
1918% %
1919% %
1920% S p l i t S t r i n g I n f o %
1921% %
1922% %
1923% %
1924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1925%
1926% SplitStringInfo() splits a string into two and returns it.
1927%
1928% The format of the SplitStringInfo method is:
1929%
1930% StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1931%
1932% A description of each parameter follows:
1933%
1934% o string_info: The string info.
1935%
1936*/
1938 const size_t offset)
1939{
1941 *split_info;
1942
1943 WizardAssert(StringDomain,string_info != (StringInfo *) NULL);
1945 if (offset > string_info->length)
1946 return((StringInfo *) NULL);
1947 split_info=AcquireStringInfo(offset);
1948 SetStringInfo(split_info,string_info);
1949 (void) memmove(string_info->datum,string_info->datum+offset,
1950 string_info->length-offset+MaxCipherBlocksize);
1951 SetStringInfoLength(string_info,string_info->length-offset);
1952 return(split_info);
1953}
1954
1955/*
1956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957% %
1958% %
1959% %
1960% S t r i n g I n f o T o H e x S t r i n g %
1961% %
1962% %
1963% %
1964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1965%
1966% StringInfoToHexString() converts a string info string to a C string.
1967%
1968% The format of the StringInfoToHexString method is:
1969%
1970% char *StringInfoToHexString(const StringInfo *string_info)
1971%
1972% A description of each parameter follows:
1973%
1974% o string_info: The string.
1975%
1976*/
1978{
1979 char
1980 *string;
1981
1982 const unsigned char
1983 *p;
1984
1985 ssize_t
1986 i;
1987
1988 unsigned char
1989 *q;
1990
1991 size_t
1992 length;
1993
1994 unsigned char
1995 hex_digits[16];
1996
1997 length=string_info->length;
1998 if (~length < WizardPathExtent)
1999 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
2000 string=(char *) AcquireQuantumMemory(length+WizardPathExtent,2*sizeof(*string));
2001 if (string == (char *) NULL)
2002 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
2003 hex_digits[0]='0';
2004 hex_digits[1]='1';
2005 hex_digits[2]='2';
2006 hex_digits[3]='3';
2007 hex_digits[4]='4';
2008 hex_digits[5]='5';
2009 hex_digits[6]='6';
2010 hex_digits[7]='7';
2011 hex_digits[8]='8';
2012 hex_digits[9]='9';
2013 hex_digits[10]='a';
2014 hex_digits[11]='b';
2015 hex_digits[12]='c';
2016 hex_digits[13]='d';
2017 hex_digits[14]='e';
2018 hex_digits[15]='f';
2019 p=string_info->datum;
2020 q=(unsigned char *) string;
2021 for (i=0; i < (ssize_t) string_info->length; i++)
2022 {
2023 *q++=hex_digits[(*p >> 4) & 0x0f];
2024 *q++=hex_digits[*p & 0x0f];
2025 p++;
2026 }
2027 *q='\0';
2028 return(string);
2029}
2030
2031/*
2032%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2033% %
2034% %
2035% %
2036% S t r i n g I n f o T o S t r i n g %
2037% %
2038% %
2039% %
2040%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2041%
2042% StringInfoToString() converts a string info string to a C string.
2043%
2044% The format of the StringInfoToString method is:
2045%
2046% char *StringInfoToString(const StringInfo *string_info)
2047%
2048% A description of each parameter follows:
2049%
2050% o string_info: The string.
2051%
2052*/
2054{
2055 char
2056 *string;
2057
2058 size_t
2059 length;
2060
2061 string=(char *) NULL;
2062 length=string_info->length;
2063 if (~length >= (WizardPathExtent-1))
2064 string=(char *) AcquireQuantumMemory(length+WizardPathExtent,sizeof(*string));
2065 if (string == (char *) NULL)
2066 return((char *) NULL);
2067 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
2068 string[length]='\0';
2069 return(string);
2070}
2071
2072/*
2073%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2074% %
2075% %
2076% %
2077% S t r i n g T o A r g v %
2078% %
2079% %
2080% %
2081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082%
2083% StringToArgv() converts a text string into command line arguments.
2084%
2085% The format of the StringToArgv method is:
2086%
2087% char **StringToArgv(const char *text,int *argc)
2088%
2089% A description of each parameter follows:
2090%
2091% o argv: Method StringToArgv returns the string list unless an error
2092% occurs, otherwise NULL.
2093%
2094% o text: Specifies the string to segment into a list.
2095%
2096% o argc: This integer pointer returns the number of arguments in the
2097% list.
2098%
2099*/
2100WizardExport char **StringToArgv(const char *text,int *argc)
2101{
2102 char
2103 **argv;
2104
2105 const char
2106 *p,
2107 *q;
2108
2109 ssize_t
2110 i;
2111
2112 *argc=0;
2113 if (text == (char *) NULL)
2114 return((char **) NULL);
2115 /*
2116 Determine the number of arguments.
2117 */
2118 for (p=text; *p != '\0'; )
2119 {
2120 while (isspace((int) ((unsigned char) *p)) != 0)
2121 p++;
2122 if (*p == '\0')
2123 break;
2124 (*argc)++;
2125 if (*p == '"')
2126 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2127 if (*p == '\'')
2128 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2129 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2130 p++;
2131 }
2132 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1),sizeof(*argv));
2133 if (argv == (char **) NULL)
2134 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
2135 /*
2136 Convert string to an ASCII list.
2137 */
2138 p=(char *) text;
2139 for (i=0; i < (ssize_t) *argc; i++)
2140 {
2141 while (isspace((int) ((unsigned char) *p)) != 0)
2142 p++;
2143 q=p;
2144 if (*q == '"')
2145 {
2146 p++;
2147 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2148 }
2149 else
2150 if (*q == '\'')
2151 {
2152 p++;
2153 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2154 }
2155 else
2156 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2157 q++;
2158 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+WizardPathExtent,
2159 sizeof(**argv));
2160 if (argv[i] == (char *) NULL)
2161 {
2162 for (i--; i >= 0; i--)
2163 argv[i]=(char *) RelinquishWizardMemory(argv[i]);
2164 argv=(char **) RelinquishWizardMemory(argv);
2165 ThrowFatalException(StringFatalError,"memory allocation failed `%s'");
2166 }
2167 (void) memcpy(argv[i],p,(size_t) (q-p));
2168 argv[i][q-p]='\0';
2169 p=q;
2170 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2171 p++;
2172 }
2173 argv[i]=(char *) NULL;
2174 return(argv);
2175}
2176
2177/*
2178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2179% %
2180% %
2181% %
2182% S t r i n g T o S t r i n g I n f o %
2183% %
2184% %
2185% %
2186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2187%
2188% StringToStringInfo() converts a string to a StringInfo type.
2189%
2190% The format of the StringToStringInfo method is:
2191%
2192% StringInfo *StringToStringInfo(const char *string)
2193%
2194% A description of each parameter follows:
2195%
2196% o string: The string.
2197%
2198*/
2200{
2202 *string_info;
2203
2204 WizardAssert(StringDomain,string != (const char *) NULL);
2205 string_info=AcquireStringInfo(strlen(string));
2206 SetStringInfoDatum(string_info,(const unsigned char *) string);
2207 return(string_info);
2208}
2209
2210/*
2211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212% %
2213% %
2214% %
2215% S t r i p S t r i n g %
2216% %
2217% %
2218% %
2219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220%
2221% StripString() strips any whitespace or quotes from the beginning and end of
2222% a string of characters.
2223%
2224% The format of the StripString method is:
2225%
2226% void StripString(char *message)
2227%
2228% A description of each parameter follows:
2229%
2230% o message: Specifies an array of characters.
2231%
2232*/
2233WizardExport void StripString(char *message)
2234{
2235 char
2236 *p,
2237 *q;
2238
2239 size_t
2240 length;
2241
2242 assert(message != (char *) NULL);
2243 if (*message == '\0')
2244 return;
2245 length=strlen(message);
2246 p=message;
2247 while (isspace((int) ((unsigned char) *p)) != 0)
2248 p++;
2249 if ((*p == '\'') || (*p == '"'))
2250 p++;
2251 q=message+length-1;
2252 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2253 q--;
2254 if (q > p)
2255 if ((*q == '\'') || (*q == '"'))
2256 q--;
2257 (void) memmove(message,p,(size_t) (q-p+1));
2258 message[q-p+1]='\0';
2259 for (p=message; *p != '\0'; p++)
2260 if (*p == '\n')
2261 *p=' ';
2262}
2263
2264/*
2265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2266% %
2267% %
2268% %
2269% S u b s t i t u t e S t r i n g %
2270% %
2271% %
2272% %
2273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2274%
2275% SubstituteString() performs string substitution on a string, replacing the
2276% string with the substituted version. Buffer must be allocated from the heap.
2277% If the string is matched and status, WizardTrue is returned otherwise
2278% WizardFalse.
2279%
2280% The format of the SubstituteString method is:
2281%
2282% WizardBooleanType SubstituteString(char **string,const char *search,
2283% const char *replace)
2284%
2285% A description of each parameter follows:
2286%
2287% o string: the string to perform replacements on; replaced with new
2288% allocation if a replacement is made.
2289%
2290% o search: search for this string.
2291%
2292% o replace: replace any matches with this string.
2293%
2294*/
2296 const char *search,const char *replace)
2297{
2299 status;
2300
2301 char
2302 *p;
2303
2304 size_t
2305 extent,
2306 replace_extent,
2307 search_extent;
2308
2309 ssize_t
2310 offset;
2311
2312 status=WizardFalse;
2313 search_extent=0,
2314 replace_extent=0;
2315 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2316 {
2317 if (search_extent == 0)
2318 search_extent=strlen(search);
2319 if (strncmp(p,search,search_extent) != 0)
2320 continue;
2321 /*
2322 We found a match.
2323 */
2324 status=WizardTrue;
2325 if (replace_extent == 0)
2326 replace_extent=strlen(replace);
2327 if (replace_extent > search_extent)
2328 {
2329 /*
2330 Make room for the replacement string.
2331 */
2332 offset=(ssize_t) (p-(*string));
2333 extent=strlen(*string)+replace_extent-search_extent+1;
2334 *string=(char *) ResizeQuantumMemory(*string,
2335 OverAllocateMemory(extent+WizardPathExtent),sizeof(*p));
2336 if (*string == (char *) NULL)
2338 "memory allocation failed `%s'");
2339 p=(*string)+offset;
2340 }
2341 /*
2342 Replace string.
2343 */
2344 if (search_extent != replace_extent)
2345 (void) CopyWizardMemory(p+replace_extent,p+search_extent,
2346 strlen(p+search_extent)+1);
2347 (void) CopyWizardMemory(p,replace,replace_extent);
2348 p+=replace_extent-1;
2349 }
2350 return(status);
2351}
WizardExport WizardBooleanType UnmapBlob(void *map, const size_t length)
Definition blob.c:1841
WizardExport void * FileToBlob(const char *filename, const size_t extent, size_t *length, ExceptionInfo *exception)
Definition blob.c:546
WizardExport void * MapBlob(int file, const MapMode mode, const WizardOffsetType offset, const size_t length)
Definition blob.c:920
@ ReadMode
Definition blob.h:33
#define MaxCipherBlocksize
Definition cipher.h:27
WizardExport CRC64Info * DestroyCRC64Info(CRC64Info *crc_info)
Definition crc64.c:137
WizardExport WizardBooleanType InitializeCRC64(CRC64Info *crc_info)
Definition crc64.c:338
WizardExport CRC64Info * AcquireCRC64Info(void)
Definition crc64.c:94
WizardExport WizardSizeType GetCRC64CyclicRedundancyCheck(const CRC64Info *crc64_info)
Definition crc64.c:247
WizardExport WizardBooleanType FinalizeCRC64(CRC64Info *crc_info)
Definition crc64.c:174
WizardExport WizardBooleanType UpdateCRC64(CRC64Info *crc_info, const StringInfo *message)
Definition crc64.c:391
#define WizardAssert(domain, predicate)
#define ThrowFatalException(severity, tag)
@ StringDomain
Definition exception.h:42
@ ResourceFatalError
Definition exception.h:121
@ StringFatalError
Definition exception.h:118
WizardExport ssize_t FormatLocaleStringList(char *string, const size_t length, const char *format, va_list operands)
Definition locale.c:416
WizardExport double InterpretLocaleValue(const char *string, char **sentinal)
Definition locale.c:969
WizardExport ssize_t FormatLocaleString(char *string, const size_t length, const char *format,...)
Definition locale.c:465
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
static size_t OverAllocateMemory(const size_t length)
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * CopyWizardMemory(void *destination, const void *source, const size_t size)
Definition memory.c:700
WizardExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition memory.c:657
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
WizardExport void * ResizeQuantumMemory(void *memory, const size_t count, const size_t quantum)
Definition memory.c:1236
#define WizardExport
#define WizardPathExtent
#define WizardSignature
WizardExport StringInfo * FileToStringInfo(const char *filename, const size_t extent, ExceptionInfo *exception)
Definition string.c:969
WizardExport WizardSizeType GetStringInfoCRC(const StringInfo *string_info)
Definition string.c:1210
WizardExport char * CloneString(char **destination, const char *source)
Definition string.c:280
WizardExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition string.c:1865
WizardExport char * FileToString(const char *filename, const size_t extent, ExceptionInfo *exception)
Definition string.c:930
WizardExport int LocaleNCompare(const char *p, const char *q, const size_t length)
Definition string.c:1608
static const unsigned char AsciiMap[]
Definition string.c:80
WizardExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition string.c:1792
WizardExport double InterpretSiPrefixValue(const char *string, char **sentinal)
Definition string.c:1418
WizardExport char * DestroyString(char *string)
Definition string.c:830
WizardExport WizardBooleanType SubstituteString(char **string, const char *search, const char *replace)
Definition string.c:2295
WizardExport StringInfo * HexStringToStringInfo(const char *string)
Definition string.c:1338
WizardExport void StripString(char *message)
Definition string.c:2233
WizardExport char ** StringToArgv(const char *text, int *argc)
Definition string.c:2100
WizardExport ssize_t FormatWizardSize(const WizardSizeType size, const WizardBooleanType bi, const size_t length, char *format)
Definition string.c:1024
WizardExport int LocaleCompare(const char *p, const char *q)
Definition string.c:1510
WizardExport void LocaleLower(char *string)
Definition string.c:1563
WizardExport char * StringInfoToHexString(const StringInfo *string_info)
Definition string.c:1977
WizardExport void SetStringInfoPath(StringInfo *string_info, const char *path)
Definition string.c:1907
WizardExport char * ConstantString(const char *source)
Definition string.c:655
WizardExport StringInfo * SplitStringInfo(StringInfo *string_info, const size_t offset)
Definition string.c:1937
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition string.c:1280
WizardExport size_t ConcatenateWizardString(char *destination, const char *source, const size_t length)
Definition string.c:478
WizardExport StringInfo * AcquireStringInfo(const size_t length)
Definition string.c:179
WizardExport int CompareStringInfo(const StringInfo *target, const StringInfo *source)
Definition string.c:372
WizardExport StringInfo * ConfigureFileToStringInfo(const char *filename)
Definition string.c:546
WizardExport StringInfo * CloneStringInfo(const StringInfo *string_info)
Definition string.c:332
WizardExport char * AcquireString(const char *source)
Definition string.c:133
WizardExport WizardBooleanType ConcatenateString(char **destination, const char *source)
Definition string.c:417
WizardExport char * StringInfoToString(const StringInfo *string_info)
Definition string.c:2053
WizardExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
Definition string.c:1832
WizardExport ssize_t FormatWizardTime(const time_t time, const size_t length, char *timestamp)
Definition string.c:1105
WizardExport void ConcatenateStringInfo(StringInfo *string_info, const StringInfo *source)
Definition string.c:704
WizardExport StringInfo * BlobToStringInfo(const void *blob, const size_t length)
Definition string.c:226
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition string.c:1251
WizardExport void PrintStringInfo(FILE *file, const char *id, const StringInfo *string_info)
Definition string.c:1712
WizardExport char * GetEnvironmentValue(const char *name)
Definition string.c:1177
WizardExport void ResetStringInfo(StringInfo *string_info)
Definition string.c:1761
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
WizardExport char ** DestroyStringList(char **list)
Definition string.c:891
WizardExport StringInfo * StringToStringInfo(const char *string)
Definition string.c:2199
WizardExport const char * GetStringInfoPath(const StringInfo *string_info)
Definition string.c:1309
WizardExport ssize_t PrintWizardString(FILE *file, const char *format,...)
Definition string.c:1668
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
size_t length
Definition string.c:66
time_t timestamp
Definition string.c:69
size_t signature
Definition string.c:72
char path[WizardPathExtent]
Definition string.c:60
unsigned char * datum
Definition string.c:63
#define O_BINARY
Definition studio.h:326
#define WizardEpsilon
Definition timer.h:25
static int open_utf8(const char *path, int flags, mode_t mode)
#define WizardMin(x, y)
size_t WizardSizeType
Definition wizard-type.h:51
ssize_t WizardOffsetType
Definition wizard-type.h:50
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27