22#include <tqstringlist.h>
23#include "pscommentlexer.h"
25#define CATEGORY_WHITESPACE -1
26#define CATEGORY_ALPHA -2
27#define CATEGORY_DIGIT -3
28#define CATEGORY_SPECIAL -4
29#define CATEGORY_LETTERHEX -5
30#define CATEGORY_INTTOOLONG -6
32#define CATEGORY_ANY -127
39int iswhitespace(
char c){
40 return (c==
' ')||(c==
'\n')||(c==
'\t')||(c==
'\r');
44 return (c==
'*')||(c==
'_')||(c==
'?')||(c==
'~')||(c==
'-')||(c==
'^')||(c==
'`')||(c==
'!')||(c==
'.')||(c==
'@')||(c==
'&')||(c==
'$')||(c==
'=');
47int isletterhex(
char c){
48 return (c==
'A')||(c==
'B')||(c==
'C')||(c==
'D')||(c==
'E')||(c==
'F');
51const char*statetoa (State state){
54 case State_Comment :
return "comment";
55 case State_CommentEncodedChar :
return "encoded char (comment)";
56 default :
return "unknown";
67static Transition transitions[] = {
68 { State_Comment,
'\n', State_Start, Action_Output},
69 { State_Comment,
'\r', State_Start, Action_Output},
70 { State_Comment,
'\\', State_CommentEncodedChar, Action_InitTemp},
71 { State_Comment, CATEGORY_ANY, State_Comment, Action_Copy},
72 { State_CommentEncodedChar,
'\\', State_Comment, Action_Copy},
73 { State_CommentEncodedChar, CATEGORY_DIGIT, State_CommentEncodedChar, Action_CopyTemp},
74 { State_CommentEncodedChar, CATEGORY_ANY, State_Comment, Action_DecodeUnget},
75 { State_Start,
'%', State_Comment, Action_Ignore},
76 { State_Start, CATEGORY_ANY, State_Start, Action_Ignore},
77 { State_Start, STOP, State_Start, Action_Abort}
80PSCommentLexer::PSCommentLexer(){
82PSCommentLexer::~PSCommentLexer(){
85bool PSCommentLexer::parse (TQIODevice& fin){
89 m_curState = State_Start;
102 nextStep (c, &newState, &action);
109 case Action_CopyOutput :
116 case Action_OutputUnget :
124 tqWarning (
"state %s / %s char %c (%d)" , statetoa(m_curState), statetoa(newState), c, c );
128 case Action_InitTemp :
131 case Action_CopyTemp :
134 case Action_DecodeUnget :
135 m_buffer.append (decode());
139 tqWarning (
"unknown action: %d ", action);
142 m_curState = newState;
149void PSCommentLexer::doOutput ()
151 if (m_buffer.length() == 0)
return;
155 gotComment (m_buffer.latin1());
158 tqWarning (
"unknown state: %d", m_curState );
164void PSCommentLexer::gotComment (
const char *value) {
165 tqDebug (
"gotComment: %s ", value );
168void PSCommentLexer::parsingStarted() {
169 tqDebug (
"parsing started" );
172void PSCommentLexer::parsingFinished() {
173 tqDebug (
"parsing finished" );
176void PSCommentLexer::parsingAborted() {
177 tqDebug (
"parsing aborted" );
180void PSCommentLexer::nextStep (
char c, State *newState, Action *newAction) {
184 Transition trans = transitions[i];
186 if (trans.c == STOP) {
187 *newState = trans.newState;
188 *newAction = trans.action;
194 if (trans.oldState == m_curState) {
196 case CATEGORY_WHITESPACE : found = isspace(c);
break;
197 case CATEGORY_ALPHA : found = isalpha(c);
break;
198 case CATEGORY_DIGIT : found = isdigit(c);
break;
199 case CATEGORY_SPECIAL : found = isSpecial(c);
break;
200 case CATEGORY_LETTERHEX : found = isletterhex(c);
break;
201 case CATEGORY_INTTOOLONG : found = m_buffer.length() > MAX_INTLEN;
break;
202 case CATEGORY_ANY : found =
true;
break;
203 default : found = (trans.c == c);
207 *newState = trans.newState;
208 *newAction = trans.action;
219uchar PSCommentLexer::decode()
221 uchar value = m_temp.toString().toShort(NULL, 8);
231StringBuffer::StringBuffer () {
232 m_buffer = (
char*)calloc (initialSize,
sizeof(
char));
234 m_capacity = initialSize;
237StringBuffer::~StringBuffer (){
241void StringBuffer::append (
char c){
242 ensureCapacity(m_length + 1);
243 m_buffer[m_length] = c;
247void StringBuffer::clear(){
248 for (uint i=0; i<m_length; i++) m_buffer[i] =
'\0';
252TQString StringBuffer::toString()
const {
253 TQString ret(m_buffer);
257void StringBuffer::ensureCapacity (
int p_capacity) {
258 if (m_capacity >= p_capacity)
return;
260 int newSize = m_capacity + addSize;
261 if (p_capacity > newSize) newSize = p_capacity;
263 char* oldBuffer = m_buffer;
264 char *newBuffer = (
char*)calloc (newSize,
sizeof(
char));
265 strcpy (newBuffer, m_buffer);
267 m_buffer = newBuffer;
268 m_capacity = newSize;
271uint StringBuffer::length() {
275double StringBuffer::toFloat() {
276 TQString data = toString();
277 return data.toFloat();
280int StringBuffer::toInt() {
281 TQString data = toString();
285const char *StringBuffer::latin1() {
289TQString StringBuffer::mid( uint index, uint len)
const {
290 TQString data = toString();
291 return data.mid(index,len);
295BoundingBoxExtractor:: BoundingBoxExtractor() : m_llx(0), m_lly(0), m_urx(0), m_ury(0) {}
296BoundingBoxExtractor::~BoundingBoxExtractor() {}
298void BoundingBoxExtractor::gotComment (
const char *value)
300 TQString data (value);
301 if (data.find(
"%BoundingBox:")==-1)
return;
303 getRectangle (value, m_llx, m_lly, m_urx, m_ury);
306bool BoundingBoxExtractor::getRectangle (
const char* input,
int &llx,
int &lly,
int &urx,
int &ury)
308 if (input == NULL)
return false;
311 if (s.contains (
"(atend)"))
return false;
313 TQString s2 = s.remove(
"%BoundingBox:");
314 TQStringList values = TQStringList::split (
" ", s2.latin1());
315 tqDebug(
"size is %d",values.size());
317 llx = values[0].toInt();
318 lly = values[1].toInt();
319 urx = values[2].toInt();
320 ury = values[3].toInt();