Debian Codegraph @

podofo/test/ContentParser/main.cpp

raw symbols SCIP
1 #include "podofo.h"
2 #include "../PdfTest.h"
3
4 #include <iostream>
5 #include <stack>
6 #include <algorithm>
7 #include <string>
8 #include <iomanip>
9 #include <cstdio>
10
11 using namespace std;
12 using namespace PoDoFo;
13
14 static bool print_output = false;
15
16 void parse_contents( PdfContentsTokenizer* pTokenizer )
17 {
18 const char* pszToken = NULL;
19 PdfVariant var;
20 EPdfContentsType eType;
21 std::string str;
22
23 int numKeywords = 0;
24 int numVariants = 0;
25
26 std::stack<PdfVariant> stack;
27
28 while( pTokenizer->ReadNext( eType, pszToken, var ) )
29 {
30 if( eType == ePdfContentsType_Keyword )
31 {
32 ++numKeywords;
33 if (print_output) std::cout << setw(12) << (numKeywords+numVariants)
34 << " Keyword: " << pszToken << std::endl;
35
36 // support 'l' and 'm' tokens
37 if( strcmp( pszToken, "l" ) == 0 )
38 {
39 double dPosY = stack.top().GetReal();
40 stack.pop();
41 double dPosX = stack.top().GetReal();
42 stack.pop();
43
44 if(print_output) std::cout << string(12,' ') << " LineTo: " << dPosX << " " << dPosY << std::endl;
45 }
46 else if( strcmp( pszToken, "m" ) == 0 )
47 {
48 double dPosY = stack.top().GetReal();
49 stack.pop();
50 double dPosX = stack.top().GetReal();
51 stack.pop();
52
53 if(print_output) std::cout << string(12,' ') << " MoveTo: " << dPosX << " " << dPosY << std::endl;
54 }
55 }
56 else if ( eType == ePdfContentsType_Variant )
57 {
58 ++numVariants;
59 var.ToString( str );
60 if(print_output) std::cout << setw(12) << (numKeywords+numVariants)
61 << " Variant: " << str << std::endl;
62 stack.push( var );
63 }
64 else if (eType == ePdfContentsType_ImageData)
65 {
66 if (print_output) {
67 std::string d ( var.GetRawData().data() );
68 std::cout << string(13, ' ') << "Inline image data: " << d.size() << " bytes. Hex follows." << std::hex << std::endl;
69 std::cout << std::hex << std::setfill('0');
70 for ( std::string::iterator i = d.begin(); i != d.end(); i ++) {
71 std::cout << std::setw(2) << (static_cast<unsigned short>(*i) & 0x00FF);
72 }
73 std::cout << std::dec << std::setfill(' ') << std::endl;
74 }
75 }
76 else
77 {
78 // Impossible; type must be keyword or variant
79 PODOFO_RAISE_ERROR( ePdfError_InternalLogic );
80 }
81 }
82 cout << ' ' << setw(12) << numKeywords << " keywords, " << setw(12) << numVariants << " variants";
83 }
84
85 void parse_page( PdfMemDocument*, PdfPage* pPage )
86 {
87 PdfContentsTokenizer tokenizer( pPage );
88 parse_contents( &tokenizer );
89 }
90
91 void usage()
92 {
93 printf("Usage: ContentParser [-g] [-a] [-p] input_filename\n");
94 printf(" -a Process all pages of input, not just first\n");
95 printf(" -p Print parsed content stream to stdout\n");
96 }
97
98 int main( int argc, char* argv[] )
99 {
100 bool all_pages = false;
101 int firstPageNo = 0;
102 string inputFileName;
103 ++argv;
104 --argc;
105 while (argc)
106 {
107 if( argv[0][0] == '-' )
108 {
109 // Single character flag
110 switch( argv[0][1] )
111 {
112 case 'a':
113 // Process all pages, not just first page
114 all_pages = true;
115 break;
116 case 'p':
117 // Print output, rather than parsing & checking
118 // silently.
119 print_output = true;
120 break;
121 case 'n':
122 // Page number request. Chars 2+ are page number int. Let's do
123 // this the quick and dirty way...
124 firstPageNo = atoi(argv[0]+2) - 1;
125 cerr << "Will process page: " << (firstPageNo+1) << endl;
126 break;
127 default:
128 usage();
129 return 1;
130 }
131 }
132 else
133 {
134 // Input filename
135 if (inputFileName.empty())
136 {
137 inputFileName = argv[0];
138 }
139 else
140 {
141 usage();
142 return 1;
143 }
144 }
145 ++argv;
146 --argc;
147 }
148
149 if (inputFileName.empty())
150 {
151 usage();
152 return 1;
153 }
154
155 try
156 {
157 PdfMemDocument doc( inputFileName.c_str() );
158 if( !doc.GetPageCount() )
159 {
160 std::cerr << "This document contains no page!" << std::endl;
161 return 1;
162 }
163
164 int toPage = all_pages ? doc.GetPageCount() : firstPageNo + 1 ;
165 for ( int i = firstPageNo; i < toPage; ++i )
166 {
167 cout << "Processing page " << setw(6) << (i+1) << "..." << std::flush;
168 PdfPage* page = doc.GetPage( i );
169 PODOFO_RAISE_LOGIC_IF( !page, "Got null page pointer within valid page range" );
170
171 parse_page( &doc, page );
172
173 cout << " - page ok" << endl;
174 }
175 }
176 catch( PdfError & e )
177 {
178 e.PrintErrorMsg();
179 return e.GetError();
180 }
181
182 cout << endl;
183 return 0;
184 }