Prison

prison-datamatrix.cpp
1/*
2 SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include <Prison/Barcode>
8
9#include <QCoreApplication>
10#include <QImageWriter>
11#include <QStringList>
12
13#include <iostream>
14#include <qtextstream.h>
15#include <stdlib.h>
16
17void error(const QString &error, const QString &errormessage)
18{
19 QTextStream str(stdout);
20 str << error << ": " << errormessage << '\n';
21 str.flush();
22 exit(0);
23}
24
25int main(int argc, char *argv[])
26{
27 QCoreApplication app(argc, argv);
28
29 QString size;
30 QString outputfile;
31 QString outputformat;
32 QStringList arguments = app.arguments();
33
34 arguments.takeFirst(); // Program (argv[0]).
35 while (!arguments.isEmpty()) {
36 QString argument = arguments.takeFirst();
37 if (argument == QLatin1String("--")) {
38 break; // rest is data
39 } else if (argument == QLatin1String("--size") || argument == QLatin1String("-s")) {
40 size = arguments.takeFirst();
41 } else if (argument == QLatin1String("--outputfile") || argument == QLatin1String("--output-file") || argument == QLatin1String("-o")) {
42 outputfile = arguments.takeFirst();
43 } else if (argument == QLatin1String("--output-format") || argument == QLatin1String("--output-format") || argument == QLatin1String("-f")) {
44 outputformat = arguments.takeFirst();
45 } else if (argument.startsWith(QLatin1String("-"))) {
46 error(QStringLiteral("unknown argument"), argument);
47 } else {
48 break;
49 }
50 }
51
52 if (outputformat.isEmpty()) {
53 outputformat = QStringLiteral("png");
54 }
55
56 if (!QImageWriter::supportedImageFormats().contains(outputformat.toLocal8Bit())) {
57 error(QStringLiteral("unsupported output format"), outputformat);
58 }
59
60 if (outputfile.isEmpty()) {
61 error(QStringLiteral("outputfile is missing"), QString());
62 }
63
64 bool ok = false;
65 int intsize = size.toInt(&ok);
66 if (!ok) {
67 error(QStringLiteral("size not a int"), size);
68 }
69 if (intsize < 10) {
70 error(QStringLiteral("needs a larger output size"), size);
71 }
72
73 QString data = arguments.join(QLatin1Char(' '));
74 if (data.size() == 0) {
75 QTextStream in(stdin);
76 data = in.readAll();
77 if (data.size() == 0) {
78 error(QStringLiteral("No data, neither on commandline nor on stdin"), QString());
79 }
80 }
81
83 if (!barcode) {
84 error(QStringLiteral("unsupported barcode type"), QString());
85 }
86
87 barcode->setData(data);
88 QImage result = barcode->toImage(QSizeF(intsize, intsize));
89 QImageWriter w(outputfile, outputformat.toLocal8Bit());
90 if (!w.write(result)) {
91 error(QStringLiteral("writing failed"), w.errorString());
92 }
93 return EXIT_SUCCESS;
94}
static std::optional< Prison::Barcode > create(Prison::BarcodeType type)
Create a new barcode generator.
Definition barcode.cpp:29
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
@ DataMatrix
DataMatrix 2d barcode.
Definition prison.h:26
QList< QByteArray > supportedImageFormats()
bool isEmpty() const const
value_type takeFirst()
bool isEmpty() const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QByteArray toLocal8Bit() const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.