Changeset View
Changeset View
Standalone View
Standalone View
util/kdevformatsource.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2016 Anton Anikin <anton.anikin@htower.ru> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License along | ||||
15 | with this program; if not, write to the Free Software Foundation, Inc., | ||||
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kdevformatfile.h" | ||||
20 | | ||||
21 | #include <QTextStream> | ||||
22 | #include <QFileInfo> | ||||
23 | | ||||
24 | inline QTextStream& qStdOut() | ||||
25 | { | ||||
26 | static QTextStream r{stdout}; | ||||
27 | return r; | ||||
28 | } | ||||
29 | | ||||
30 | int main(int argc, char** argv) | ||||
31 | { | ||||
32 | using namespace KDevelop; | ||||
33 | | ||||
34 | static const QString formatFileName = QStringLiteral("format_sources"); | ||||
35 | | ||||
36 | if (argc == 1) { | ||||
37 | qStdOut() << "Usage:" << argv[0] << "ORIGFILE [TMPFILE]\n\n"; | ||||
38 | qStdOut() << "Where ORIGFILE represents the original location of the formatted contents,\n"; | ||||
39 | qStdOut() << "and TMPFILE is used as the actual, potentially different,\n"; | ||||
40 | qStdOut() << "contents of the file.\n"; | ||||
41 | return EXIT_FAILURE; | ||||
42 | } | ||||
43 | | ||||
44 | QFileInfo origFileInfo(argv[1]); | ||||
45 | if (!origFileInfo.exists()) { | ||||
46 | qStdOut() << "orig file \"" << origFileInfo.absoluteFilePath() << "\" does not exits\n"; | ||||
47 | return EXIT_FAILURE; | ||||
48 | } | ||||
49 | | ||||
50 | QString origFilePath = origFileInfo.canonicalFilePath(); | ||||
51 | QString tempFilePath; | ||||
52 | | ||||
53 | if (argc > 2) | ||||
54 | tempFilePath = QFileInfo(argv[2]).canonicalFilePath(); | ||||
55 | else { | ||||
56 | tempFilePath = origFilePath; | ||||
57 | qStdOut() << "no temp file given, formatting the original file\n"; | ||||
58 | } | ||||
59 | | ||||
60 | KDevFormatFile formatFile(origFilePath, tempFilePath); | ||||
61 | | ||||
62 | if (!formatFile.find()) | ||||
63 | return EXIT_FAILURE; | ||||
64 | | ||||
65 | if (!formatFile.read()) | ||||
66 | return EXIT_FAILURE; | ||||
67 | | ||||
68 | if (!formatFile.apply()) | ||||
69 | return EXIT_FAILURE; | ||||
70 | | ||||
71 | return EXIT_SUCCESS; | ||||
72 | } |