| 1 |
#include <cups/cups.h> |
| 2 |
#include <cups/ppd.h> |
| 3 |
#include <iostream> |
| 4 |
#include <iomanip> |
| 5 |
#include <map> |
| 6 |
|
| 7 |
using namespace std; |
| 8 |
|
| 9 |
map <string, string> gPaperNames; |
| 10 |
typedef pair<string, string> str_pair; |
| 11 |
|
| 12 |
static void |
| 13 |
FindPapersOptions(ppd_option_t& o) |
| 14 |
{ |
| 15 |
// cout << "Option " << o.keyword << endl; |
| 16 |
if (!strcmp(o.keyword, "PageSize")) |
| 17 |
{ |
| 18 |
for (int i = 0; i < o.num_choices; ++i) |
| 19 |
{ |
| 20 |
ppd_choice_t& c = o.choices[i]; |
| 21 |
|
| 22 |
gPaperNames.insert(str_pair(c.choice, c.text)); |
| 23 |
// cout << c.choice << " " << c.text << endl; |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static void |
| 29 |
FindPapersGroup(ppd_group_t& g) |
| 30 |
{ |
| 31 |
// cout << g.name << " " << g.text << " " << g.num_options << endl; |
| 32 |
|
| 33 |
for (int i = 0; i < g.num_options; ++i) |
| 34 |
FindPapersOptions(g.options[i]); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
FindPapers(ppd_group_t* group, int num_groups) |
| 39 |
{ |
| 40 |
for (int i = 0; i < num_groups; ++i) |
| 41 |
FindPapersGroup(group[i]); |
| 42 |
} |
| 43 |
|
| 44 |
int main(int argc, char** argv) |
| 45 |
{ |
| 46 |
if (argc < 2) |
| 47 |
{ |
| 48 |
cerr << "Usage: paperlist <PrinterName>" << endl; |
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
const char* ppdFileName = cupsGetPPD(argv[1]); |
| 54 |
if (!ppdFileName) |
| 55 |
{ |
| 56 |
cerr << "Unknown printer '" << argv[1] << "'" << endl; |
| 57 |
return 2; |
| 58 |
} |
| 59 |
|
| 60 |
ppd_file_t* ppd = ppdOpenFile(ppdFileName); |
| 61 |
if (!ppd) |
| 62 |
{ |
| 63 |
cerr << "unable to open ppd file '" << ppdFileName << "'" << endl; |
| 64 |
return 3; |
| 65 |
} |
| 66 |
|
| 67 |
FindPapers(ppd->groups, ppd->num_groups); |
| 68 |
|
| 69 |
|
| 70 |
cout << setw(30) << setiosflags(ios_base::left) << "Paper Name" << resetiosflags(ios_base::left) |
| 71 |
<< setw(10) << "Width" |
| 72 |
<< setw(10) << "Height" |
| 73 |
<< setw(10) << "Left" |
| 74 |
<< setw(10) << "Bottom" |
| 75 |
<< setw(10) << "Right" |
| 76 |
<< setw(10) << "Top" |
| 77 |
<< endl; |
| 78 |
|
| 79 |
cout << setw(30) << setiosflags(ios_base::left) << "----- ----" << resetiosflags(ios_base::left) |
| 80 |
<< setw(10) << "-----" |
| 81 |
<< setw(10) << "------" |
| 82 |
<< setw(10) << "----" |
| 83 |
<< setw(10) << "---" |
| 84 |
<< setw(10) << "-----" |
| 85 |
<< setw(10) << "------" |
| 86 |
<< endl; |
| 87 |
|
| 88 |
|
| 89 |
for (int i = 0; i < ppd->num_sizes; ++i) |
| 90 |
{ |
| 91 |
ppd_size_t size = ppd->sizes[i]; |
| 92 |
|
| 93 |
cout << setw(30) << setiosflags(ios_base::left) << gPaperNames[size.name] << setprecision(2) << setiosflags(ios_base::fixed) << resetiosflags(ios_base::left) |
| 94 |
<< setw(10) << size.width |
| 95 |
<< setw(10) << size.length |
| 96 |
<< setw(10) << size.left |
| 97 |
<< setw(10) << size.bottom |
| 98 |
<< setw(10) << size.right |
| 99 |
<< setw(10) << size.top |
| 100 |
<< endl; |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
ppdClose(ppd); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |