MagickCore  7.0.7
Convert, Edit, Or Compose Bitmap Images
token.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % TTTTT OOO K K EEEEE N N %
7 % T O O K K E NN N %
8 % T O O KKK EEE N N N %
9 % T O O K K E N NN %
10 % T OOO K K EEEEE N N %
11 % %
12 % %
13 % MagickCore Token Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % January 1993 %
18 % %
19 % %
20 % Copyright 1999-2018 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://www.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 "MagickCore/studio.h"
44 #include "MagickCore/exception.h"
46 #include "MagickCore/image.h"
47 #include "MagickCore/memory_.h"
49 #include "MagickCore/string_.h"
51 #include "MagickCore/token.h"
53 #include "MagickCore/utility.h"
55 
56 /*
57  Typedef declaractions.
58 */
59 struct _TokenInfo
60 {
61  int
63 
66 
67  ssize_t
69 
70  char
72 
73  size_t
75 };
76 
77 /*
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % %
80 % %
81 % %
82 % A c q u i r e T o k e n I n f o %
83 % %
84 % %
85 % %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %
88 % AcquireTokenInfo() allocates the TokenInfo structure.
89 %
90 % The format of the AcquireTokenInfo method is:
91 %
92 % TokenInfo *AcquireTokenInfo()
93 %
94 */
96 {
97  TokenInfo
98  *token_info;
99 
100  token_info=(TokenInfo *) AcquireCriticalMemory(sizeof(*token_info));
101  token_info->signature=MagickCoreSignature;
102  return(token_info);
103 }
104 
105 /*
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % %
108 % %
109 % %
110 % D e s t r o y T o k e n I n f o %
111 % %
112 % %
113 % %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
117 % structure.
118 %
119 % The format of the DestroyTokenInfo method is:
120 %
121 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
122 %
123 % A description of each parameter follows:
124 %
125 % o token_info: Specifies a pointer to an TokenInfo structure.
126 %
127 */
129 {
130  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
131  assert(token_info != (TokenInfo *) NULL);
132  assert(token_info->signature == MagickCoreSignature);
133  token_info->signature=(~MagickCoreSignature);
134  token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
135  return(token_info);
136 }
137 
138 /*
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 % %
141 % %
142 % %
143 + G e t N e x t T o k e n %
144 % %
145 % %
146 % %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %
149 % GetNextToken() gets a token from the token stream. A token is defined as
150 % a sequence of characters delimited by whitespace (e.g. clip-path), a
151 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
152 % parenthesis (e.g. rgb(0,0,0)). GetNextToken() also recognizes these
153 % separator characters: ':', '=', ',', and ';'.
154 %
155 % The format of the GetNextToken method is:
156 %
157 % void GetNextToken(const char *start,const char **end,
158 % const size_t extent,char *token)
159 %
160 % A description of each parameter follows:
161 %
162 % o start: the start of the token sequence.
163 %
164 % o end: point to the end of the token sequence.
165 %
166 % o extent: maximum extent of the token.
167 %
168 % o token: copy the token to this buffer.
169 %
170 */
171 MagickExport void GetNextToken(const char *start,const char **end,
172  const size_t extent,char *token)
173 {
174  double
175  value;
176 
177  register const char
178  *p;
179 
180  register ssize_t
181  i;
182 
183  size_t
184  length;
185 
186  assert(start != (const char *) NULL);
187  assert(token != (char *) NULL);
188  i=0;
189  length=strlen(start);
190  p=start;
191  while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
192  p++;
193  switch (*p)
194  {
195  case '\0':
196  break;
197  case '"':
198  case '\'':
199  case '`':
200  case '{':
201  {
202  register char
203  escape;
204 
205  switch (*p)
206  {
207  case '"': escape='"'; break;
208  case '\'': escape='\''; break;
209  case '`': escape='\''; break;
210  case '{': escape='}'; break;
211  default: escape=(*p); break;
212  }
213  for (p++; *p != '\0'; p++)
214  {
215  if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
216  p++;
217  else
218  if (*p == escape)
219  {
220  p++;
221  break;
222  }
223  if (i < (ssize_t) (extent-1))
224  token[i++]=(*p);
225  if ((size_t) (p-start) >= length)
226  break;
227  }
228  break;
229  }
230  case '/':
231  {
232  if (i < (ssize_t) (extent-1))
233  token[i++]=(*p);
234  p++;
235  if ((*p == '>') || (*p == '/'))
236  {
237  if (i < (ssize_t) (extent-1))
238  token[i++]=(*p);
239  p++;
240  }
241  break;
242  }
243  default:
244  {
245  char
246  *q;
247 
248  value=StringToDouble(p,&q);
249  (void) value;
250  if ((p != q) && (*p != ','))
251  {
252  for ( ; (p < q) && (*p != ','); p++)
253  {
254  if (i < (ssize_t) (extent-1))
255  token[i++]=(*p);
256  if ((size_t) (p-start) >= length)
257  break;
258  }
259  if (*p == '%')
260  {
261  if (i < (ssize_t) (extent-1))
262  token[i++]=(*p);
263  p++;
264  }
265  break;
266  }
267  if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
268  (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
269  {
270  if (i < (ssize_t) (extent-1))
271  token[i++]=(*p);
272  p++;
273  break;
274  }
275  for ( ; *p != '\0'; p++)
276  {
277  if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
278  (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
279  break;
280  if ((i > 0) && (*p == '<'))
281  break;
282  if (i < (ssize_t) (extent-1))
283  token[i++]=(*p);
284  if (*p == '>')
285  break;
286  if (*p == '(')
287  for (p++; *p != '\0'; p++)
288  {
289  if (i < (ssize_t) (extent-1))
290  token[i++]=(*p);
291  if ((*p == ')') && (*(p-1) != '\\'))
292  break;
293  if ((size_t) (p-start) >= length)
294  break;
295  }
296  if ((size_t) (p-start) >= length)
297  break;
298  }
299  break;
300  }
301  }
302  token[i]='\0';
303  if ((LocaleNCompare(token,"url(",4) == 0) && (strlen(token) > 4))
304  {
305  ssize_t
306  offset;
307 
308  offset=4;
309  if (token[offset] == '#')
310  offset++;
311  i=(ssize_t) strlen(token);
312  (void) CopyMagickString(token,token+offset,MagickPathExtent);
313  token[i-offset-1]='\0';
314  }
315  while (isspace((int) ((unsigned char) *p)) != 0)
316  p++;
317  if (end != (const char **) NULL)
318  *end=(const char *) p;
319 }
320 
321 /*
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % %
324 % %
325 % %
326 % G l o b E x p r e s s i o n %
327 % %
328 % %
329 % %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 %
332 % GlobExpression() returns MagickTrue if the expression matches the pattern.
333 %
334 % The format of the GlobExpression function is:
335 %
336 % MagickBooleanType GlobExpression(const char *expression,
337 % const char *pattern,const MagickBooleanType case_insensitive)
338 %
339 % A description of each parameter follows:
340 %
341 % o expression: Specifies a pointer to a text string containing a file name.
342 %
343 % o pattern: Specifies a pointer to a text string containing a pattern.
344 %
345 % o case_insensitive: set to MagickTrue to ignore the case when matching
346 % an expression.
347 %
348 */
350  const char *pattern,const MagickBooleanType case_insensitive)
351 {
353  done,
354  match;
355 
356  register const char
357  *p;
358 
359  /*
360  Return on empty pattern or '*'.
361  */
362  if (pattern == (char *) NULL)
363  return(MagickTrue);
364  if (GetUTFCode(pattern) == 0)
365  return(MagickTrue);
366  if (LocaleCompare(pattern,"*") == 0)
367  return(MagickTrue);
368  p=pattern+strlen(pattern)-1;
369  if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
370  {
372  *exception;
373 
374  ImageInfo
375  *image_info;
376 
377  /*
378  Determine if pattern is a scene, i.e. img0001.pcd[2].
379  */
380  image_info=AcquireImageInfo();
381  (void) CopyMagickString(image_info->filename,pattern,MagickPathExtent);
382  exception=AcquireExceptionInfo();
383  (void) SetImageInfo(image_info,0,exception);
384  exception=DestroyExceptionInfo(exception);
385  if (LocaleCompare(image_info->filename,pattern) != 0)
386  {
387  image_info=DestroyImageInfo(image_info);
388  return(MagickFalse);
389  }
390  image_info=DestroyImageInfo(image_info);
391  }
392  /*
393  Evaluate glob expression.
394  */
395  done=MagickFalse;
396  while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
397  {
398  if (GetUTFCode(expression) == 0)
399  if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
400  break;
401  switch (GetUTFCode(pattern))
402  {
403  case '*':
404  {
406  status;
407 
408  status=MagickFalse;
409  pattern+=GetUTFOctets(pattern);
410  while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
411  {
412  status=GlobExpression(expression,pattern,case_insensitive);
413  expression+=GetUTFOctets(expression);
414  }
415  if (status != MagickFalse)
416  {
417  while (GetUTFCode(expression) != 0)
418  expression+=GetUTFOctets(expression);
419  while (GetUTFCode(pattern) != 0)
420  pattern+=GetUTFOctets(pattern);
421  }
422  break;
423  }
424  case '[':
425  {
426  int
427  c;
428 
429  pattern+=GetUTFOctets(pattern);
430  for ( ; ; )
431  {
432  if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
433  {
434  done=MagickTrue;
435  break;
436  }
437  if (GetUTFCode(pattern) == '\\')
438  {
439  pattern+=GetUTFOctets(pattern);
440  if (GetUTFCode(pattern) == 0)
441  {
442  done=MagickTrue;
443  break;
444  }
445  }
446  if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
447  {
448  c=GetUTFCode(pattern);
449  pattern+=GetUTFOctets(pattern);
450  pattern+=GetUTFOctets(pattern);
451  if (GetUTFCode(pattern) == ']')
452  {
453  done=MagickTrue;
454  break;
455  }
456  if (GetUTFCode(pattern) == '\\')
457  {
458  pattern+=GetUTFOctets(pattern);
459  if (GetUTFCode(pattern) == 0)
460  {
461  done=MagickTrue;
462  break;
463  }
464  }
465  if ((GetUTFCode(expression) < c) ||
466  (GetUTFCode(expression) > GetUTFCode(pattern)))
467  {
468  pattern+=GetUTFOctets(pattern);
469  continue;
470  }
471  }
472  else
473  if (GetUTFCode(pattern) != GetUTFCode(expression))
474  {
475  pattern+=GetUTFOctets(pattern);
476  continue;
477  }
478  pattern+=GetUTFOctets(pattern);
479  while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
480  {
481  if ((GetUTFCode(pattern) == '\\') &&
482  (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
483  pattern+=GetUTFOctets(pattern);
484  pattern+=GetUTFOctets(pattern);
485  }
486  if (GetUTFCode(pattern) != 0)
487  {
488  pattern+=GetUTFOctets(pattern);
489  expression+=GetUTFOctets(expression);
490  }
491  break;
492  }
493  break;
494  }
495  case '?':
496  {
497  pattern+=GetUTFOctets(pattern);
498  expression+=GetUTFOctets(expression);
499  break;
500  }
501  case '{':
502  {
503  pattern+=GetUTFOctets(pattern);
504  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
505  {
506  p=expression;
507  match=MagickTrue;
508  while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
509  (GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
510  (match != MagickFalse))
511  {
512  if (GetUTFCode(pattern) == '\\')
513  pattern+=GetUTFOctets(pattern);
514  match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
515  MagickFalse;
516  p+=GetUTFOctets(p);
517  pattern+=GetUTFOctets(pattern);
518  }
519  if (GetUTFCode(pattern) == 0)
520  {
521  match=MagickFalse;
522  done=MagickTrue;
523  break;
524  }
525  if (match != MagickFalse)
526  {
527  expression=p;
528  while ((GetUTFCode(pattern) != '}') &&
529  (GetUTFCode(pattern) != 0))
530  {
531  pattern+=GetUTFOctets(pattern);
532  if (GetUTFCode(pattern) == '\\')
533  {
534  pattern+=GetUTFOctets(pattern);
535  if (GetUTFCode(pattern) == '}')
536  pattern+=GetUTFOctets(pattern);
537  }
538  }
539  }
540  else
541  {
542  while ((GetUTFCode(pattern) != '}') &&
543  (GetUTFCode(pattern) != ',') &&
544  (GetUTFCode(pattern) != 0))
545  {
546  pattern+=GetUTFOctets(pattern);
547  if (GetUTFCode(pattern) == '\\')
548  {
549  pattern+=GetUTFOctets(pattern);
550  if ((GetUTFCode(pattern) == '}') ||
551  (GetUTFCode(pattern) == ','))
552  pattern+=GetUTFOctets(pattern);
553  }
554  }
555  }
556  if (GetUTFCode(pattern) != 0)
557  pattern+=GetUTFOctets(pattern);
558  }
559  break;
560  }
561  case '\\':
562  {
563  pattern+=GetUTFOctets(pattern);
564  if (GetUTFCode(pattern) == 0)
565  break;
566  }
567  default:
568  {
569  if (case_insensitive != MagickFalse)
570  {
571  if (tolower((int) GetUTFCode(expression)) !=
572  tolower((int) GetUTFCode(pattern)))
573  {
574  done=MagickTrue;
575  break;
576  }
577  }
578  else
579  if (GetUTFCode(expression) != GetUTFCode(pattern))
580  {
581  done=MagickTrue;
582  break;
583  }
584  expression+=GetUTFOctets(expression);
585  pattern+=GetUTFOctets(pattern);
586  }
587  }
588  }
589  while (GetUTFCode(pattern) == '*')
590  pattern+=GetUTFOctets(pattern);
591  match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
593  return(match);
594 }
595 
596 /*
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 % %
599 % %
600 % %
601 + I s G l o b %
602 % %
603 % %
604 % %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %
607 % IsGlob() returns MagickTrue if the path specification contains a globbing
608 % pattern.
609 %
610 % The format of the IsGlob method is:
611 %
612 % MagickBooleanType IsGlob(const char *geometry)
613 %
614 % A description of each parameter follows:
615 %
616 % o path: the path.
617 %
618 */
620 {
622  status = MagickFalse;
623 
624  register const char
625  *p;
626 
627  if (IsPathAccessible(path) != MagickFalse)
628  return(MagickFalse);
629  for (p=path; *p != '\0'; p++)
630  {
631  switch (*p)
632  {
633  case '*':
634  case '?':
635  case '{':
636  case '}':
637  case '[':
638  case ']':
639  {
640  status=MagickTrue;
641  break;
642  }
643  default:
644  break;
645  }
646  }
647  return(status);
648 }
649 
650 /*
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % %
653 % %
654 % %
655 % T o k e n i z e r %
656 % %
657 % %
658 % %
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 %
661 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
662 % one at a time from a string of characters. The characters used for white
663 % space, for break characters, and for quotes can be specified. Also,
664 % characters in the string can be preceded by a specifiable escape character
665 % which removes any special meaning the character may have.
666 %
667 % Here is some terminology:
668 %
669 % o token: A single unit of information in the form of a group of
670 % characters.
671 %
672 % o white space: Apace that gets ignored (except within quotes or when
673 % escaped), like blanks and tabs. in addition, white space terminates a
674 % non-quoted token.
675 %
676 % o break set: One or more characters that separates non-quoted tokens.
677 % Commas are a common break character. The usage of break characters to
678 % signal the end of a token is the same as that of white space, except
679 % multiple break characters with nothing or only white space between
680 % generate a null token for each two break characters together.
681 %
682 % For example, if blank is set to be the white space and comma is set to
683 % be the break character, the line
684 %
685 % A, B, C , , DEF
686 %
687 % ... consists of 5 tokens:
688 %
689 % 1) "A"
690 % 2) "B"
691 % 3) "C"
692 % 4) "" (the null string)
693 % 5) "DEF"
694 %
695 % o Quote character: A character that, when surrounding a group of other
696 % characters, causes the group of characters to be treated as a single
697 % token, no matter how many white spaces or break characters exist in
698 % the group. Also, a token always terminates after the closing quote.
699 % For example, if ' is the quote character, blank is white space, and
700 % comma is the break character, the following string
701 %
702 % A, ' B, CD'EF GHI
703 %
704 % ... consists of 4 tokens:
705 %
706 % 1) "A"
707 % 2) " B, CD" (note the blanks & comma)
708 % 3) "EF"
709 % 4) "GHI"
710 %
711 % The quote characters themselves do not appear in the resultant
712 % tokens. The double quotes are delimiters i use here for
713 % documentation purposes only.
714 %
715 % o Escape character: A character which itself is ignored but which
716 % causes the next character to be used as is. ^ and \ are often used
717 % as escape characters. An escape in the last position of the string
718 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
719 % and non-escape) character. For example, assume white space, break
720 % character, and quote are the same as in the above examples, and
721 % further, assume that ^ is the escape character. Then, in the string
722 %
723 % ABC, ' DEF ^' GH' I ^ J K^ L ^
724 %
725 % ... there are 7 tokens:
726 %
727 % 1) "ABC"
728 % 2) " DEF ' GH"
729 % 3) "I"
730 % 4) " " (a lone blank)
731 % 5) "J"
732 % 6) "K L"
733 % 7) "^" (passed as is at end of line)
734 %
735 % The format of the Tokenizer method is:
736 %
737 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
738 % const size_t max_token_length,const char *line,const char *white,
739 % const char *break_set,const char *quote,const char escape,
740 % char *breaker,int *next,char *quoted)
741 %
742 % A description of each parameter follows:
743 %
744 % o flag: right now, only the low order 3 bits are used.
745 %
746 % 1 => convert non-quoted tokens to upper case
747 % 2 => convert non-quoted tokens to lower case
748 % 0 => do not convert non-quoted tokens
749 %
750 % o token: a character string containing the returned next token
751 %
752 % o max_token_length: the maximum size of "token". Characters beyond
753 % "max_token_length" are truncated.
754 %
755 % o string: the string to be parsed.
756 %
757 % o white: a string of the valid white spaces. example:
758 %
759 % char whitesp[]={" \t"};
760 %
761 % blank and tab will be valid white space.
762 %
763 % o break: a string of the valid break characters. example:
764 %
765 % char breakch[]={";,"};
766 %
767 % semicolon and comma will be valid break characters.
768 %
769 % o quote: a string of the valid quote characters. An example would be
770 %
771 % char whitesp[]={"'\"");
772 %
773 % (this causes single and double quotes to be valid) Note that a
774 % token starting with one of these characters needs the same quote
775 % character to terminate it.
776 %
777 % for example:
778 %
779 % "ABC '
780 %
781 % is unterminated, but
782 %
783 % "DEF" and 'GHI'
784 %
785 % are properly terminated. Note that different quote characters
786 % can appear on the same line; only for a given token do the quote
787 % characters have to be the same.
788 %
789 % o escape: the escape character (NOT a string ... only one
790 % allowed). Use zero if none is desired.
791 %
792 % o breaker: the break character used to terminate the current
793 % token. If the token was quoted, this will be the quote used. If
794 % the token is the last one on the line, this will be zero.
795 %
796 % o next: this variable points to the first character of the
797 % next token. it gets reset by "tokenizer" as it steps through the
798 % string. Set it to 0 upon initialization, and leave it alone
799 % after that. You can change it if you want to jump around in the
800 % string or re-parse from the beginning, but be careful.
801 %
802 % o quoted: set to True if the token was quoted and MagickFalse
803 % if not. You may need this information (for example: in C, a
804 % string with quotes around it is a character string, while one
805 % without is an identifier).
806 %
807 % o result: 0 if we haven't reached EOS (end of string), and 1
808 % if we have.
809 %
810 */
811 
812 #define IN_WHITE 0
813 #define IN_TOKEN 1
814 #define IN_QUOTE 2
815 #define IN_OZONE 3
816 
817 static ssize_t sindex(int c,const char *string)
818 {
819  register const char
820  *p;
821 
822  for (p=string; *p != '\0'; p++)
823  if (c == (int) (*p))
824  return((ssize_t) (p-string));
825  return(-1);
826 }
827 
828 static void StoreToken(TokenInfo *token_info,char *string,
829  size_t max_token_length,int c)
830 {
831  register ssize_t
832  i;
833 
834  if ((token_info->offset < 0) ||
835  ((size_t) token_info->offset >= (max_token_length-1)))
836  return;
837  i=token_info->offset++;
838  string[i]=(char) c;
839  if (token_info->state == IN_QUOTE)
840  return;
841  switch (token_info->flag & 0x03)
842  {
843  case 1:
844  {
845  string[i]=(char) toupper(c);
846  break;
847  }
848  case 2:
849  {
850  string[i]=(char) tolower(c);
851  break;
852  }
853  default:
854  break;
855  }
856 }
857 
858 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
859  char *token,const size_t max_token_length,const char *line,const char *white,
860  const char *break_set,const char *quote,const char escape,char *breaker,
861  int *next,char *quoted)
862 {
863  int
864  c;
865 
866  register ssize_t
867  i;
868 
869  *breaker='\0';
870  *quoted='\0';
871  if (line[*next] == '\0')
872  return(1);
873  token_info->state=IN_WHITE;
874  token_info->quote=(char) MagickFalse;
875  token_info->flag=flag;
876  for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
877  {
878  c=(int) line[*next];
879  i=sindex(c,break_set);
880  if (i >= 0)
881  {
882  switch (token_info->state)
883  {
884  case IN_WHITE:
885  case IN_TOKEN:
886  case IN_OZONE:
887  {
888  (*next)++;
889  *breaker=break_set[i];
890  token[token_info->offset]='\0';
891  return(0);
892  }
893  case IN_QUOTE:
894  {
895  StoreToken(token_info,token,max_token_length,c);
896  break;
897  }
898  }
899  continue;
900  }
901  i=sindex(c,quote);
902  if (i >= 0)
903  {
904  switch (token_info->state)
905  {
906  case IN_WHITE:
907  {
908  token_info->state=IN_QUOTE;
909  token_info->quote=quote[i];
910  *quoted=(char) MagickTrue;
911  break;
912  }
913  case IN_QUOTE:
914  {
915  if (quote[i] != token_info->quote)
916  StoreToken(token_info,token,max_token_length,c);
917  else
918  {
919  token_info->state=IN_OZONE;
920  token_info->quote='\0';
921  }
922  break;
923  }
924  case IN_TOKEN:
925  case IN_OZONE:
926  {
927  *breaker=(char) c;
928  token[token_info->offset]='\0';
929  return(0);
930  }
931  }
932  continue;
933  }
934  i=sindex(c,white);
935  if (i >= 0)
936  {
937  switch (token_info->state)
938  {
939  case IN_WHITE:
940  case IN_OZONE:
941  break;
942  case IN_TOKEN:
943  {
944  token_info->state=IN_OZONE;
945  break;
946  }
947  case IN_QUOTE:
948  {
949  StoreToken(token_info,token,max_token_length,c);
950  break;
951  }
952  }
953  continue;
954  }
955  if (c == (int) escape)
956  {
957  if (line[(*next)+1] == '\0')
958  {
959  *breaker='\0';
960  StoreToken(token_info,token,max_token_length,c);
961  (*next)++;
962  token[token_info->offset]='\0';
963  return(0);
964  }
965  switch (token_info->state)
966  {
967  case IN_WHITE:
968  {
969  (*next)--;
970  token_info->state=IN_TOKEN;
971  break;
972  }
973  case IN_TOKEN:
974  case IN_QUOTE:
975  {
976  (*next)++;
977  c=(int) line[*next];
978  StoreToken(token_info,token,max_token_length,c);
979  break;
980  }
981  case IN_OZONE:
982  {
983  token[token_info->offset]='\0';
984  return(0);
985  }
986  }
987  continue;
988  }
989  switch (token_info->state)
990  {
991  case IN_WHITE:
992  {
993  token_info->state=IN_TOKEN;
994  StoreToken(token_info,token,max_token_length,c);
995  break;
996  }
997  case IN_TOKEN:
998  case IN_QUOTE:
999  {
1000  StoreToken(token_info,token,max_token_length,c);
1001  break;
1002  }
1003  case IN_OZONE:
1004  {
1005  token[token_info->offset]='\0';
1006  return(0);
1007  }
1008  }
1009  }
1010  token[token_info->offset]='\0';
1011  return(0);
1012 }
static ssize_t sindex(int c, const char *string)
Definition: token.c:817
MagickExport TokenInfo * DestroyTokenInfo(TokenInfo *token_info)
Definition: token.c:128
MagickExport ImageInfo * AcquireImageInfo(void)
Definition: image.c:341
MagickPrivate MagickBooleanType IsGlob(const char *path)
Definition: token.c:619
#define IN_OZONE
Definition: token.c:815
static double StringToDouble(const char *magick_restrict string, char **magick_restrict sentinal)
MagickExport ExceptionInfo * AcquireExceptionInfo(void)
Definition: exception.c:108
int state
Definition: token.c:62
ssize_t offset
Definition: token.c:68
MagickStatusType flag
Definition: token.c:65
Definition: log.h:52
static void StoreToken(TokenInfo *token_info, char *string, size_t max_token_length, int c)
Definition: token.c:828
MagickExport TokenInfo * AcquireTokenInfo(void)
Definition: token.c:95
#define MagickCoreSignature
MagickBooleanType
Definition: magick-type.h:156
#define DirectorySeparator
Definition: studio.h:254
unsigned int MagickStatusType
Definition: magick-type.h:119
static int GetUTFCode(const char *text)
char filename[MagickPathExtent]
Definition: image.h:471
MagickExport int LocaleNCompare(const char *p, const char *q, const size_t length)
Definition: locale.c:1509
MagickExport MagickBooleanType GlobExpression(const char *expression, const char *pattern, const MagickBooleanType case_insensitive)
Definition: token.c:349
#define MagickPathExtent
MagickExport MagickBooleanType static void * AcquireCriticalMemory(const size_t size)
#define IN_WHITE
Definition: token.c:812
static unsigned int GetUTFOctets(const char *text)
MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info, const unsigned int frames, ExceptionInfo *exception)
Definition: image.c:2596
size_t signature
Definition: token.c:74
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1397
MagickExport MagickBooleanType IsPathAccessible(const char *path)
Definition: utility.c:1466
MagickExport size_t CopyMagickString(char *destination, const char *source, const size_t length)
Definition: string.c:742
char quote
Definition: token.c:71
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1409
#define GetMagickModule()
Definition: log.h:28
MagickExport void GetNextToken(const char *start, const char **end, const size_t extent, char *token)
Definition: token.c:171
MagickExport ImageInfo * DestroyImageInfo(ImageInfo *image_info)
Definition: image.c:1253
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1038
#define MagickPrivate
#define MagickExport
#define IN_QUOTE
Definition: token.c:814
MagickExport int Tokenizer(TokenInfo *token_info, const unsigned flag, char *token, const size_t max_token_length, const char *line, const char *white, const char *break_set, const char *quote, const char escape, char *breaker, int *next, char *quoted)
Definition: token.c:858
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:417
#define IN_TOKEN
Definition: token.c:813