MagickCore 7.0.10
utility.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% U U TTTTT IIIII L IIIII TTTTT Y Y %
7% U U T I L I T Y Y %
8% U U T I L I T Y %
9% U U T I L I T Y %
10% UUU T IIIII LLLLL IIIII T Y %
11% %
12% %
13% Wizard's Toolkit Utility 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 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/exception.h"
45#include "wizard/locale_.h"
46#include "wizard/memory_.h"
47#include "wizard/resource_.h"
49#include "wizard/utility.h"
51#if defined(WIZARDSTOOLKIT_HAVE_MACH_O_DYLD_H)
52#include <mach-o/dyld.h>
53#endif
54
55/*
56 Static declarations.
57*/
58static const char
59 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
60
61/*
62 Forward declaration.
63*/
64static int
65 IsDirectory(const char *);
66
67/*
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69% %
70% %
71% %
72% A p p e n d F i l e E x t e n s i o n %
73% %
74% %
75% %
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77%
78% AppendFileExtension() appends a file extension type to the filename. If an
79% extension to the file already exists, it is first removed.
80%
81% The extension of the AppendFileExtension method is:
82%
83% void AppendFileExtension(const char *extension,char *filename)
84%
85% A description of each parameter follows.
86%
87% o extension: Specifies a pointer to an array of characters. This is the
88% extension of the image.
89%
90% o filename: Specifies a pointer to an array of characters. The unique
91% file name is returned in this array.
92%
93*/
94WizardExport void AppendFileExtension(const char *extension,char *filename)
95{
96 char
97 root[WizardPathExtent];
98
99 assert(extension != (char *) NULL);
100 assert(filename != (char *) NULL);
101 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",filename);
102 if ((*extension == '\0') || (*filename == '\0'))
103 return;
104 GetPathComponent(filename,RootPath,root);
105 (void) CopyWizardString(filename,root,WizardPathExtent);
106 (void) ConcatenateWizardString(filename,".",WizardPathExtent);
107 (void) ConcatenateWizardString(filename,extension,WizardPathExtent);
108}
109
110/*
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112% %
113% %
114% %
115% B a s e 6 4 D e c o d e %
116% %
117% %
118% %
119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120%
121% Base64Decode() decodes Base64-encoded text and returns its binary
122% equivalent. NULL is returned if the text is not valid Base64 data, or a
123% memory allocation failure occurs.
124%
125% The format of the Base64Decode method is:
126%
127% unsigned char *Base64Decode(const char *source,length_t *length)
128%
129% A description of each parameter follows:
130%
131% o source: A pointer to a Base64-encoded string.
132%
133% o length: The number of bytes decoded.
134%
135*/
136WizardExport unsigned char *Base64Decode(const char *source,size_t *length)
137{
138 int
139 state;
140
141 const char
142 *p,
143 *q;
144
145 size_t
146 i;
147
148 unsigned char
149 *decode;
150
152 assert(source != (char *) NULL);
153 assert(length != (size_t *) NULL);
154 *length=0;
155 decode=(unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
156 3*sizeof(*decode));
157 if (decode == (unsigned char *) NULL)
158 return((unsigned char *) NULL);
159 i=0;
160 state=0;
161 for (p=source; *p != '\0'; p++)
162 {
163 if (isspace((int) ((unsigned char) *p)) != 0)
164 continue;
165 if (*p == '=')
166 break;
167 q=strchr(Base64,*p);
168 if (q == (char *) NULL)
169 {
170 decode=(unsigned char *) RelinquishWizardMemory(decode);
171 return((unsigned char *) NULL); /* non-Base64 character */
172 }
173 switch (state)
174 {
175 case 0:
176 {
177 decode[i]=(q-Base64) << 2;
178 state++;
179 break;
180 }
181 case 1:
182 {
183 decode[i++]|=(q-Base64) >> 4;
184 decode[i]=((q-Base64) & 0x0f) << 4;
185 state++;
186 break;
187 }
188 case 2:
189 {
190 decode[i++]|=(q-Base64) >> 2;
191 decode[i]=((q-Base64) & 0x03) << 6;
192 state++;
193 break;
194 }
195 case 3:
196 {
197 decode[i++]|=(q-Base64);
198 state=0;
199 break;
200 }
201 }
202 }
203 /*
204 Verify Base-64 string has proper terminal characters.
205 */
206 if (*p != '=')
207 {
208 if (state != 0)
209 {
210 decode=(unsigned char *) RelinquishWizardMemory(decode);
211 return((unsigned char *) NULL);
212 }
213 }
214 else
215 {
216 p++;
217 switch (state)
218 {
219 case 0:
220 case 1:
221 {
222 /*
223 Unrecognized '=' character.
224 */
225 decode=(unsigned char *) RelinquishWizardMemory(decode);
226 return((unsigned char *) NULL);
227 }
228 case 2:
229 {
230 for ( ; *p != '\0'; p++)
231 if (isspace((int) ((unsigned char) *p)) == 0)
232 break;
233 if (*p != '=')
234 {
235 decode=(unsigned char *) RelinquishWizardMemory(decode);
236 return((unsigned char *) NULL);
237 }
238 p++;
239 }
240 case 3:
241 {
242 for ( ; *p != '\0'; p++)
243 if (isspace((int) ((unsigned char) *p)) == 0)
244 {
245 decode=(unsigned char *) RelinquishWizardMemory(decode);
246 return((unsigned char *) NULL);
247 }
248 if ((int) decode[i] != 0)
249 {
250 decode=(unsigned char *) RelinquishWizardMemory(decode);
251 return((unsigned char *) NULL);
252 }
253 }
254 default:
255 break;
256 }
257 }
258 *length=i;
259 return(decode);
260}
261
262/*
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264% %
265% %
266% %
267% B a s e 6 4 E n c o d e %
268% %
269% %
270% %
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272%
273% Base64Encode() encodes arbitrary binary data to Base64 encoded format as
274% described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
275% returns the result as a null-terminated ASCII string. NULL is returned if
276% a memory allocation failure occurs.
277%
278% The format of the Base64Encode method is:
279%
280% char *Base64Encode(const unsigned char *blob,const size_t blob_length,
281% size_t *encode_length)
282%
283% A description of each parameter follows:
284%
285% o blob: A pointer to binary data to encode.
286%
287% o blob_length: The number of bytes to encode.
288%
289% o encode_length: The number of bytes encoded.
290%
291*/
292WizardExport char *Base64Encode(const unsigned char *blob,
293 const size_t blob_length,size_t *encode_length)
294{
295 char
296 *encode;
297
298 const unsigned char
299 *p;
300
301 size_t
302 i;
303
304 size_t
305 quantum,
306 remainder;
307
309 assert(blob != (const unsigned char *) NULL);
310 assert(blob_length != 0);
311 assert(encode_length != (size_t *) NULL);
312 *encode_length=0;
313 quantum=4*blob_length/3+4;
314 encode=(char *) AcquireQuantumMemory(quantum,sizeof(*encode));
315 if (encode == (char *) NULL)
316 return((char *) NULL);
317 i=0;
318 for (p=blob; p < (blob+blob_length-2); p+=3)
319 {
320 encode[i++]=Base64[(int) (*p >> 2)];
321 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
322 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
323 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
324 }
325 remainder=blob_length % 3;
326 if (remainder != 0)
327 {
328 ssize_t
329 j;
330
331 unsigned char
332 code[3];
333
334 code[0]='\0';
335 code[1]='\0';
336 code[2]='\0';
337 for (j=0; j < (ssize_t) remainder; j++)
338 code[j]=(*p++);
339 encode[i++]=Base64[(int) (code[0] >> 2)];
340 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
341 if (remainder == 1)
342 encode[i++]='=';
343 else
344 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
345 encode[i++]='=';
346 }
347 *encode_length=i;
348 encode[i++]='\0';
349 assert(i <= quantum);
350 return(encode);
351}
352
353/*
354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355% %
356% %
357% %
358% C h o p P a t h C o m p o n e n t s %
359% %
360% %
361% %
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363%
364% ChopPathComponents() removes the number of specified file components from a
365% path.
366%
367% The format of the ChopPathComponents method is:
368%
369% ChopPathComponents(char *path,size_t components)
370%
371% A description of each parameter follows:
372%
373% o path: The path.
374%
375% o components: The number of components to chop.
376%
377*/
378WizardExport void ChopPathComponents(char *path,const size_t components)
379{
380 ssize_t
381 i;
382
383 for (i=0; i < (ssize_t) components; i++)
384 GetPathComponent(path,HeadPath,path);
385}
386
387/*
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389% %
390% %
391% %
392+ G e t P a t h C o m p o n e n t %
393% %
394% %
395% %
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397%
398% GetPathComponent() returns the parent directory name, filename, basename,
399% or extension of a file path.
400%
401% The format of the GetPathComponent function is:
402%
403% GetPathComponent(const char *path,PathType type,char *component)
404%
405% A description of each parameter follows:
406%
407% o path: Specifies a pointer to a character array that contains the
408% file path.
409%
410% o type: Specififies which file path component to return.
411%
412% o component: The selected file path component is returned here.
413%
414*/
415WizardExport void GetPathComponent(const char *path,PathType type,
416 char *component)
417{
418 char
419 filesystem[WizardPathExtent],
420 subnode[WizardPathExtent],
421 *q;
422
423 char
424 *p;
425
426 /*
427 Get basename of client.
428 */
429 assert(path != (const char *) NULL);
430 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",path);
431 assert(component != (const char *) NULL);
432 if (*path == '\0')
433 {
434 *component='\0';
435 return;
436 }
437 (void) CopyWizardString(component,path,WizardPathExtent);
438 *filesystem='\0';
439 for (p=component; *p != '\0'; p++)
440 if ((*p == ':') && (IsDirectory(path) < 0) &&
441 (IsPathAcessible(path) == WizardFalse))
442 {
443 /*
444 Look for filesystem specification (e.g. c:/documents).
445 */
446 (void) CopyWizardString(filesystem,component,(size_t)
447 (p-component+1));
448 if (IsDirectory(filesystem) < 0)
449 *filesystem='\0';
450 else
451 for (q=component; *q != '\0'; q++)
452 *q=(*++p);
453 break;
454 }
455 *subnode='\0';
456 p=component;
457 if (*p != '\0')
458 p=component+strlen(component)-1;
459 if ((*p == ']') && (strchr(component,'[') != (char *) NULL))
460 {
461 /*
462 Look for subnode specification (e.g. book[2]).
463 */
464 for (q=p-1; q > component; q--)
465 if (*q == '[')
466 break;
467 if (*q == '[')
468 {
469 (void) CopyWizardString(subnode,q+1,WizardPathExtent);
470 subnode[p-q-1]='\0';
471 *q='\0';
472 }
473 }
474 p=component;
475 if (*p != '\0')
476 for (p=component+(strlen(component)-1); p > component; p--)
477 if (IsBasenameSeparator(*p))
478 break;
479 switch (type)
480 {
481 case FilesystemPath:
482 {
483 (void) CopyWizardString(component,filesystem,WizardPathExtent);
484 break;
485 }
486 case RootPath:
487 {
488 for (p=component+(strlen(component)-1); p > component; p--)
489 {
491 break;
492 if (*p == '.')
493 break;
494 }
495 if (*p == '.')
496 *p='\0';
497 break;
498 }
499 case HeadPath:
500 {
501 *p='\0';
502 break;
503 }
504 case TailPath:
505 {
507 (void) CopyWizardMemory((unsigned char *) component,
508 (const unsigned char *) (p+1),strlen(p+1)+1);
509 break;
510 }
511 case BasePath:
512 {
514 (void) CopyWizardString(component,p+1,WizardPathExtent);
515 for (p=component+(strlen(component)-1); p > component; p--)
516 if (*p == '.')
517 {
518 *p='\0';
519 break;
520 }
521 break;
522 }
523 case ExtensionPath:
524 {
526 (void) CopyWizardString(component,p+1,WizardPathExtent);
527 p=component;
528 if (*p != '\0')
529 for (p=component+strlen(component)-1; p > component; p--)
530 if (*p == '.')
531 break;
532 *component='\0';
533 if (*p == '.')
534 (void) CopyWizardString(component,p+1,WizardPathExtent);
535 p=strchr(component,'?');
536 if (p != (char *) NULL)
537 *p='\0';
538 break;
539 }
540 case SubnodePath:
541 {
542 (void) CopyWizardString(component,subnode,WizardPathExtent);
543 break;
544 }
545 case CanonicalPath:
546 case UndefinedPath:
547 break;
548 }
549}
550
551/*
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553% %
554% %
555% %
556% G e t P a t h C o m p o n e n t s %
557% %
558% %
559% %
560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561%
562% GetPathComponents() returns a list of path components.
563%
564% The format of the GetPathComponents method is:
565%
566% char **GetPathComponents(const char *path,
567% size_t *number_componenets)
568%
569% A description of each parameter follows:
570%
571% o path: Specifies the string to segment into a list.
572%
573% o number_components: return the number of components in the list
574%
575*/
576WizardExport char **GetPathComponents(const char *path,
577 size_t *number_components)
578{
579 char
580 **components;
581
582 char
583 *q;
584
585 const char
586 *p;
587
588 ssize_t
589 i;
590
591 if (path == (char *) NULL)
592 return((char **) NULL);
593 *number_components=1;
594 for (p=path; *p != '\0'; p++)
595 if (IsBasenameSeparator(*p))
596 (*number_components)++;
597 components=(char **) AcquireQuantumMemory((size_t) (*number_components+1),
598 sizeof(*components));
599 if (components == (char **) NULL)
600 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
601 p=path;
602 for (i=0; i < (ssize_t) *number_components; i++)
603 {
604 for (q=(char *) p; *q != '\0'; q++)
605 if (IsBasenameSeparator(*q))
606 break;
607 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+WizardPathExtent,
608 sizeof(**components));
609 if (components[i] == (char *) NULL)
610 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
611 (void) CopyWizardString(components[i],p,(size_t) (q-p+1));
612 p=q+1;
613 }
614 components[i]=(char *) NULL;
615 return(components);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% G e t E x e c u t i o n P a t h %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% GetExecutionPath() returns the pathname of the executable that started
630% the process. On success WizardTrue is returned, otherwise WizardFalse.
631%
632% The format of the GetExecutionPath method is:
633%
634% WizardBooleanType GetExecutionPath(char *path,const size_t extent)
635%
636% A description of each parameter follows:
637%
638% o path: The pathname of the executable that started the process.
639%
640% o extent: the maximum extent of the path.
641%
642*/
643WizardExport WizardBooleanType GetExecutionPath(char *path,const size_t extent)
644{
645 char
646 *directory;
647
648 *path='\0';
649 directory=getcwd(path,(unsigned long) extent);
650 (void) directory;
651#if defined(WIZARDSTOOLKIT_HAVE_GETPID) && defined(WIZARDSTOOLKIT_HAVE_READLINK) && defined(PATH_MAX)
652 {
653 char
654 link_path[WizardPathExtent],
655 real_path[PATH_MAX+1];
656
657 int
658 length;
659
660 (void) FormatLocaleString(link_path,WizardPathExtent,"/proc/%.20g/exe",
661 (double) getpid());
662 length=readlink(link_path,real_path,PATH_MAX);
663 if (length == -1)
664 {
665 (void) FormatLocaleString(link_path,WizardPathExtent,"/proc/%.20g/file",
666 (double) getpid());
667 length=readlink(link_path,real_path,PATH_MAX);
668 }
669 if ((length > 0) && ((size_t) length <= PATH_MAX))
670 {
671 real_path[length]='\0';
672 (void) CopyWizardString(path,real_path,extent);
673 }
674 }
675#endif
676#if defined(WIZARDSTOOLKIT_HAVE__NSGETEXECUTABLEPATH)
677 {
678
679 char
680 executable_path[PATH_MAX << 1],
681 real_path[PATH_MAX+1];
682
683 size_t
684 length;
685
686 length=sizeof(executable_path);
687 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
688 (realpath(executable_path,real_path) != (char *) NULL))
689 (void) CopyWizardString(path,real_path,extent);
690 }
691#endif
692#if defined(WIZARDSTOOLKIT_HAVE_GETEXECNAME)
693 {
694 const char
695 *execution_path;
696
697 execution_path=(const char *) getexecname();
698 if (execution_path != (const char *) NULL)
699 {
700 if (*execution_path != *DirectorySeparator)
702 (void) ConcatenateWizardString(path,execution_path,extent);
703 }
704 }
705#endif
706#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
707 NTGetExecutionPath(path,extent);
708#endif
709#if defined(__GNU__)
710 {
711 char
712 *program_name,
713 *execution_path;
714
715 ssize_t
716 count;
717
718 count=0;
719 execution_path=(char *) NULL;
720 program_name=program_invocation_name;
721 if (*program_invocation_name != '/')
722 {
723 size_t
724 extent;
725
726 extent=strlen(directory)+strlen(program_name)+2;
727 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
728 if (program_name == (char *) NULL)
729 program_name=program_invocation_name;
730 else
731 count=FormatLocaleString(program_name,extent,"%s/%s",directory,
732 program_invocation_name);
733 }
734 if (count != -1)
735 {
736 execution_path=realpath(program_name,NULL);
737 if (execution_path != (char *) NULL)
738 (void) CopyWizardString(path,execution_path,extent);
739 }
740 if (program_name != program_invocation_name)
741 program_name=(char *) RelinquishWizardMemory(program_name);
742 execution_path=(char *) RelinquishWizardMemory(execution_path);
743 }
744#endif
745#if defined(__OpenBSD__)
746 {
747 extern char
748 *__progname;
749
750 (void) CopyMagickString(path,__progname,extent);
751 }
752#endif
753 return(IsPathAcessible(path));
754}
755
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761% I s P a t h A c c e s s i b l e %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% IsPathAcessible() returns WizardTrue if the file as defined by the path is
768% accessible.
769%
770% The format of the IsPathAcessible method is:
771%
772% WizardBooleanType IsPathAcessible(const char *filename)
773%
774% A description of each parameter follows.
775%
776% o path: Specifies a path to a file.
777%
778*/
780{
781 int
782 status;
783
784 struct stat
785 file_info;
786
787 if ((path == (const char *) NULL) || (*path == '\0'))
788 return(WizardFalse);
789 status=stat_utf8(path,&file_info);
790 if (status != 0)
791 return(WizardFalse);
792 if (S_ISREG(file_info.st_mode) == 0)
793 return(WizardFalse);
794 if (access(path,F_OK) != 0)
795 return(WizardFalse);
796 return(WizardTrue);
797}
798
799/*
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801% %
802% %
803% %
804% G e t W i z a d d P a g e S i z e %
805% %
806% %
807% %
808%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809%
810% GetWizardPageSize() returns the memory page size.
811%
812% The format of the GetWizardPageSize method is:
813%
814% ssize_t GetWizardPageSize()
815%
816*/
818{
819 static ssize_t
820 page_size = -1;
821
822 if (page_size > 0)
823 return(page_size);
824#if defined(WIZARDSTOOLKIT_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
825 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
826#else
827#if defined(WIZARDSTOOLKIT_HAVE_GETPAGESIZE)
828 page_size=(ssize_t) getpagesize();
829#endif
830#endif
831 if (page_size <= 0)
832 page_size=16384;
833 return(page_size);
834}
835
836/*
837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838% %
839% %
840% %
841+ I s D i r e c t o r y %
842% %
843% %
844% %
845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846%
847% IsDirectory() returns -1 if the directory does not exist, 1 is returned
848% if the path represents a directory otherwise 0.
849%
850% The format of the IsDirectory method is:
851%
852% int IsDirectory(const char *path)
853%
854% A description of each parameter follows.
855%
856% o path: The directory path.
857%
858%
859*/
860static int IsDirectory(const char *path)
861{
862#if !defined(X_OK)
863#define X_OK 1
864#endif
865
866 int
867 status;
868
869 struct stat
870 file_info;
871
872 if ((path == (const char *) NULL) || (*path == '\0'))
873 return(WizardFalse);
874 status=stat_utf8(path,&file_info);
875 if (status != 0)
876 return(-1);
877 if (S_ISDIR(file_info.st_mode) == 0)
878 return(0);
879 if (access(path,X_OK) != 0)
880 return(0);
881 return(1);
882}
883
884/*
885%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886% %
887% %
888% %
889% I s W i z a r d T r u e %
890% %
891% %
892% %
893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894%
895% IsWizardTrue() returns WizardTrue if the value is "true", "on", "yes" or
896% "1".
897%
898% The format of the IsWizardTrue method is:
899%
900% WizardBooleanType IsWizardTrue(const char *value)
901%
902% A description of each parameter follows:
903%
904% o option: either WizardTrue or WizardFalse depending on the value
905% parameter.
906%
907% o value: Specifies a pointer to a character array.
908%
909*/
911{
912 if (value == (char *) NULL)
913 return(WizardFalse);
914 if (LocaleCompare(value,"true") == 0)
915 return(WizardTrue);
916 if (LocaleCompare(value,"on") == 0)
917 return(WizardTrue);
918 if (LocaleCompare(value,"yes") == 0)
919 return(WizardTrue);
920 if (LocaleCompare(value,"1") == 0)
921 return(WizardTrue);
922 return(WizardFalse);
923}
924
925/*
926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927% %
928% %
929% %
930% P a r s e W i z a r d T i m e %
931% %
932% %
933% %
934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935%
936% ParseWizardTime() parses the specified time in the Internet date/time
937% format and the returns the results.
938%
939% The format of the ParseWizardTime method is:
940%
941% const char *ParseWizardTime(const char *timestamp,time_t *target)
942%
943% A description of each parameter follows.
944%
945% o timestamp: the Internet date/time here.
946%
947% o target: return the time since the Epoch (00:00:00 UTC, January 1, 1970),
948% measured in seconds.
949%
950*/
951WizardExport const char *ParseWizardTime(const char *timestamp,time_t *target)
952{
953 char
954 *q;
955
956 double
957 value;
958
959 ssize_t
960 timezone;
961
962 const char
963 *p;
964
965 ssize_t
966 i;
967
968 static char
969 separators[] = "--T::-:";
970
971 struct tm
972 gm_time,
973 local_time,
974 target_time;
975
976 *target=time((time_t *) NULL);
977#if defined(WIZARDSTOOLKIT_HAVE_LOCALTIME_R)
978 (void) localtime_r(target,&local_time);
979#else
980 (void) memcpy(&local_time,localtime(target),sizeof(local_time));
981#endif
982#if defined(WIZARDSTOOLKIT_HAVE_GMTIME_R)
983 (void) gmtime_r(target,&gm_time);
984#else
985 (void) memcpy(&gm_time,gmtime(target),sizeof(gm_time));
986#endif
987 timezone=(ssize_t) ((local_time.tm_min-gm_time.tm_min)/60+local_time.tm_hour-
988 gm_time.tm_hour+24*((local_time.tm_year-gm_time.tm_year) != 0 ?
989 (local_time.tm_year-gm_time.tm_year) : (local_time.tm_yday-
990 gm_time.tm_yday)));
991 (void) memset(&target_time,0,sizeof(target_time));
992 p=timestamp;
993 for (i=0; ; i++)
994 {
995 value=StringToDouble(p,&q);
996 if (*q != separators[i])
997 break;
998 switch (i)
999 {
1000 case 0: target_time.tm_year=(int) (value+0.5)-1900; break;
1001 case 1: target_time.tm_mon=(int) (value+0.5)-1; break;
1002 case 2: target_time.tm_mday=(int) (value+0.5); break;
1003 case 3: target_time.tm_hour=(int) (value+0.5); break;
1004 case 4: target_time.tm_min=(int) (value+0.5); break;
1005 case 5: target_time.tm_sec=(int) (value+0.5); break;
1006 case 6: timezone+=(int) (value+0.5); break;
1007 default: break;
1008 }
1009 p=q;
1010 if (*q == '\0')
1011 break;
1012 p++;
1013 }
1014 *target=mktime(&target_time)-3600*timezone;
1015 return(p);
1016}
1017
1018/*
1019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020% %
1021% %
1022% %
1023% W i z a d d D e l a y %
1024% %
1025% %
1026% %
1027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028%
1029% WizardDelay() suspends program execution for the number of milliseconds
1030% specified.
1031%
1032% The format of the Delay method is:
1033%
1034% void WizardDelay(const WizardSizeType milliseconds)
1035%
1036% A description of each parameter follows:
1037%
1038% o milliseconds: Specifies the number of milliseconds to delay before
1039% returning.
1040%
1041*/
1043{
1044 if (milliseconds == 0)
1045 return;
1046#if defined(WIZARDSTOOLKIT_HAVE_NANOSLEEP)
1047 {
1048 struct timespec
1049 timer;
1050
1051 timer.tv_sec=(time_t) (milliseconds/1000);
1052 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1053 (void) nanosleep(&timer,(struct timespec *) NULL);
1054 }
1055#elif defined(WIZARDSTOOLKIT_HAVE_USLEEP)
1056 usleep(1000*milliseconds);
1057#elif defined(WIZARDSTOOLKIT_HAVE_SELECT)
1058 {
1059 struct timeval
1060 timer;
1061
1062 timer.tv_sec=(long) milliseconds/1000;
1063 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1064 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1065 }
1066#elif defined(WIZARDSTOOLKIT_HAVE_POLL)
1067 (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1068#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
1069 Sleep((long) milliseconds);
1070#elif defined(vms)
1071 {
1072 float
1073 timer;
1074
1075 timer=milliseconds/1000.0;
1076 lib$wait(&timer);
1077 }
1078#elif defined(__BEOS__)
1079 snooze(1000*milliseconds);
1080#else
1081# error "Time delay method not defined."
1082#endif
1083}
#define ThrowFatalException(severity, tag)
@ ResourceFatalError
Definition exception.h:121
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
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
#define WizardPrivate
#define WizardExport
#define WizardPathExtent
static double StringToDouble(const char *string, char **sentinal)
WizardExport int LocaleCompare(const char *p, const char *q)
Definition string.c:1510
WizardExport size_t ConcatenateWizardString(char *destination, const char *source, const size_t length)
Definition string.c:478
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
#define S_ISDIR(mode)
Definition studio.h:188
#define IsBasenameSeparator(c)
Definition studio.h:242
#define DirectorySeparator
Definition studio.h:239
#define S_ISREG(mode)
Definition studio.h:191
#define PATH_MAX
Definition studio.h:338
static int stat_utf8(const char *path, struct stat *attributes)
WizardExport WizardBooleanType IsWizardTrue(const char *value)
Definition utility.c:910
WizardExport void AppendFileExtension(const char *extension, char *filename)
Definition utility.c:94
WizardExport void GetPathComponent(const char *path, PathType type, char *component)
Definition utility.c:415
WizardExport WizardBooleanType GetExecutionPath(char *path, const size_t extent)
Definition utility.c:643
WizardExport void ChopPathComponents(char *path, const size_t components)
Definition utility.c:378
WizardPrivate ssize_t GetWizardPageSize(void)
Definition utility.c:817
WizardExport char ** GetPathComponents(const char *path, size_t *number_components)
Definition utility.c:576
#define X_OK
WizardExport WizardBooleanType IsPathAcessible(const char *path)
Definition utility.c:779
WizardExport char * Base64Encode(const unsigned char *blob, const size_t blob_length, size_t *encode_length)
Definition utility.c:292
static const char Base64[]
Definition utility.c:59
static int IsDirectory(const char *)
Definition utility.c:860
WizardExport void WizardDelay(const WizardSizeType milliseconds)
Definition utility.c:1042
WizardExport const char * ParseWizardTime(const char *timestamp, time_t *target)
Definition utility.c:951
WizardExport unsigned char * Base64Decode(const char *source, size_t *length)
Definition utility.c:136
PathType
Definition utility.h:26
@ RootPath
Definition utility.h:29
@ ExtensionPath
Definition utility.h:33
@ SubnodePath
Definition utility.h:34
@ TailPath
Definition utility.h:31
@ FilesystemPath
Definition utility.h:28
@ HeadPath
Definition utility.h:30
@ BasePath
Definition utility.h:32
@ CanonicalPath
Definition utility.h:35
@ UndefinedPath
Definition utility.h:27
size_t WizardSizeType
Definition wizard-type.h:51
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27