Debian Codegraph @

examples/svg_viewer/agg_svg_path_tokenizer.cpp

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // SVG path tokenizer.
17 //
18 //----------------------------------------------------------------------------
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "agg_svg_exception.h"
23 #include "agg_svg_path_tokenizer.h"
24
25
26 namespace agg
27 {
28 namespace svg
29 {
30
31 //------------------------------------------------------------------------
32 const char path_tokenizer::s_commands[] = "+-MmZzLlHhVvCcSsQqTtAaFfPp";
33 const char path_tokenizer::s_numeric[] = ".Ee0123456789";
34 const char path_tokenizer::s_separators[] = " ,\t\n\r";
35
36 //------------------------------------------------------------------------
37 path_tokenizer::path_tokenizer()
38 : m_path(0), m_last_number(0.0), m_last_command(0)
39 {
40 init_char_mask(m_commands_mask, s_commands);
41 init_char_mask(m_numeric_mask, s_numeric);
42 init_char_mask(m_separators_mask, s_separators);
43 }
44
45
46 //------------------------------------------------------------------------
47 void path_tokenizer::set_path_str(const char* str)
48 {
49 m_path = str;
50 m_last_command = 0;
51 m_last_number = 0.0;
52 }
53
54
55 //------------------------------------------------------------------------
56 void path_tokenizer::init_char_mask(char* mask, const char* char_set)
57 {
58 memset(mask, 0, 256/8);
59 while(*char_set)
60 {
61 unsigned c = unsigned(*char_set++) & 0xFF;
62 mask[c >> 3] |= 1 << (c & 7);
63 }
64 }
65
66
67 //------------------------------------------------------------------------
68 bool path_tokenizer::next()
69 {
70 if(m_path == 0) return false;
71
72 // Skip all white spaces and other garbage
73 while(*m_path && !is_command(*m_path) && !is_numeric(*m_path))
74 {
75 if(!is_separator(*m_path))
76 {
77 char buf[100];
78 sprintf(buf, "path_tokenizer::next : Invalid Character %c", *m_path);
79 throw exception(buf);
80 }
81 m_path++;
82 }
83
84 if(*m_path == 0) return false;
85
86 if(is_command(*m_path))
87 {
88 // Check if the command is a numeric sign character
89 if(*m_path == '-' || *m_path == '+')
90 {
91 return parse_number();
92 }
93 m_last_command = *m_path++;
94 while(*m_path && is_separator(*m_path)) m_path++;
95 if(*m_path == 0) return true;
96 }
97 return parse_number();
98 }
99
100
101
102 //------------------------------------------------------------------------
103 double path_tokenizer::next(char cmd)
104 {
105 if(!next()) throw exception("parse_path: Unexpected end of path");
106 if(last_command() != cmd)
107 {
108 char buf[100];
109 sprintf(buf, "parse_path: Command %c: bad or missing parameters", cmd);
110 throw exception(buf);
111 }
112 return last_number();
113 }
114
115
116 //------------------------------------------------------------------------
117 bool path_tokenizer::parse_number()
118 {
119 char buf[256]; // Should be enough for any number
120 char* buf_ptr = buf;
121
122 // Copy all sign characters
123 while(buf_ptr < buf+255 && (*m_path == '-' || *m_path == '+'))
124 {
125 *buf_ptr++ = *m_path++;
126 }
127
128 // Copy all numeric characters
129 while(buf_ptr < buf+255 && (is_numeric(*m_path)))
130 {
131 *buf_ptr++ = *m_path++;
132 }
133 *buf_ptr = 0;
134 m_last_number = atof(buf);
135 return true;
136 }
137
138
139 } //namespace svg
140 } //namespace agg
141
142
143
144