]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/qstd.cpp
Tweak update version + date
[bacula/bacula] / bacula / src / qt-console / qstd.cpp
1 //start id=namespace
2 #include "qstd.h"
3
4 /* QTextStreams look a lot like iostreams,
5 we just have to point them to the right place. */
6
7 //start id=streamdefs
8 QTextStream qstd::cin(stdin, QIODevice::ReadOnly);
9 QTextStream qstd::cout(stdout, QIODevice::WriteOnly);
10 QTextStream qstd::cerr(stderr, QIODevice::WriteOnly);
11 //end
12
13
14 /* Namespace members are like static class members */
15 bool qstd::yes(QString question) {
16     QString ans;
17     cout << QString(" %1 [y/n]? ").arg(question);
18     cout.flush();
19     ans = cin.readLine();
20     return (ans.toUpper().startsWith("Y", Qt::CaseInsensitive));
21 }
22 //end
23
24 bool qstd::more(QString s) {
25     return yes(QString("Another %1").arg(s));
26 }
27
28
29 int qstd::promptInt(int base /* =10 */) { /* Usage: int n = promptInt(); */
30     QString numstr;
31     int result;
32     bool ok;
33     cout << ": " << flush;
34     while (1) {
35         numstr = cin.readLine();
36         result = numstr.toInt(&ok, base);
37         if (!ok) {
38             cout << "Invalid number. Try again: ";
39             cout.flush();
40         }
41         else
42             return result;
43     }
44 }
45
46
47 double qstd::promptDouble() { /* Usage: double d = promptDouble(); */
48     QString numstr;
49     double result;
50     bool ok;
51     while (1) {
52         numstr = cin.readLine();
53         result = numstr.toDouble(&ok);
54         if (!ok) {
55             cout << "Invalid number. Try again: ";
56             cout.flush();
57         }
58         else
59             return result;
60     }
61 }
62
63
64 void qstd::promptOutputFile(QFile& outfile) {
65     QString filename;
66     while (1) {
67         cout << "Please enter the file name for saving this data: ";
68         cout.flush();
69         filename = cin.readLine();
70         outfile.setFileName(filename);
71         bool fileExists = outfile.open(QIODevice::ReadOnly);
72         if (!fileExists)
73             break;
74         if (yes("File already exists ... Ok to overwrite"))
75             break;
76         outfile.close();
77         outfile.reset();
78     }
79     outfile.close();
80     outfile.reset();
81     outfile.open(QIODevice::WriteOnly);
82     cout << filename << " open for writing ...\n";
83     cout.flush();
84 }
85
86
87 void qstd::promptInputFile(QFile& infile) {
88     QString filename;
89     while (1) {
90         cout << "Name of the file to be read:  ";
91         cout.flush();
92         filename = cin.readLine();
93         infile.setFileName(filename);
94         bool fileExists = infile.open(QIODevice::ReadOnly);
95         if (fileExists)
96             break;
97         cout << "File does not exist ... Please try again. \n";
98         cout.flush();
99         infile.reset();
100     }
101     cout << filename << " open for reading ...\n";
102     cout.flush();
103 }
104