Browse Source

Simple files (without flags) work.

tastytea 3 weeks ago
parent
commit
d058f1f012
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: CFC39497F1B26E07
3 changed files with 117 additions and 3 deletions
  1. 6
    2
      CMakeLists.txt
  2. 110
    0
      src/main.cpp
  3. 1
    1
      xdgcfg

+ 6
- 2
CMakeLists.txt View File

@@ -1,12 +1,13 @@
1 1
 cmake_minimum_required (VERSION 3.2)
2 2
 project(cppscript
3
-        VERSION 0.7.1
3
+        VERSION 0
4 4
         LANGUAGES CXX
5 5
 )
6 6
 
7 7
 include(GNUInstallDirs)
8 8
 find_package(PkgConfig REQUIRED)
9 9
 pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
10
+pkg_check_modules(LIBCONFIG REQUIRED libconfig++)
10 11
 
11 12
 set(CMAKE_CXX_STANDARD 14)
12 13
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -17,8 +18,10 @@ set(CMAKE_CXX_FLAGS_DEBUG
17 18
 
18 19
 include_directories(${PROJECT_BINARY_DIR})
19 20
 include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
21
+include_directories(${LIBCONFIG_INCLUDE_DIRS})
20 22
 
21 23
 link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
24
+link_directories(${LIBCONFIG_LIBRARY_DIRS})
22 25
 
23 26
 # Write version in header
24 27
 configure_file(
@@ -29,6 +32,7 @@ configure_file(
29 32
 file(GLOB sources src/*.cpp)
30 33
 add_executable(${CMAKE_PROJECT_NAME} "${sources}")
31 34
 target_link_libraries(${CMAKE_PROJECT_NAME}
32
-                      "${LIBXDG_BASEDIR_LDFLAGS} -lstdc++fs")
35
+                      "${LIBXDG_BASEDIR_LDFLAGS} ${LIBCONFIG_LDFLAGS}"
36
+                      "-lstdc++fs")
33 37
 install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
34 38
 #install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})

+ 110
- 0
src/main.cpp View File

@@ -1,6 +1,116 @@
1
+/*  Public Domain / CC-0
2
+ *  Author: tastytea <tastytea@tastytea.de>
3
+ */
4
+
5
+#if __cplusplus >= 201703L
6
+    #include <filesystem>
7
+#else
8
+    #include <experimental/filesystem>
9
+#endif
10
+#include <iostream>
11
+#include <string>
12
+#include <fstream>
13
+#include <libconfig.h++>
14
+#include <basedir.h>
15
+#include <cstdlib>
16
+#include <unistd.h>
17
+#include "xdgcfg.hpp"
1 18
 #include "version.hpp"
2 19
 
20
+#if __cplusplus >= 201703L
21
+    namespace fs = std::filesystem;
22
+#else
23
+    namespace fs = std::experimental::filesystem;
24
+#endif
25
+using std::cout;
26
+using std::cerr;
27
+using std::endl;
28
+
29
+bool need_save = false;
30
+string compiler = "g++";
31
+fs::path cache_dir;
32
+
33
+void read_settings()
34
+{
35
+    xdgcfg config("cppscript.cfg");
36
+    if (config.read() != 0)
37
+    {
38
+        config.write();
39
+    }
40
+    libconfig::Setting &cfg = config.get_cfg().getRoot();
41
+
42
+    if (cfg.exists("compiler"))
43
+    {
44
+        compiler = cfg["compiler"].c_str();
45
+    }
46
+    else
47
+    {
48
+        cfg.add("compiler", libconfig::Setting::TypeString) = compiler;
49
+        need_save = true;
50
+    }
51
+
52
+    if (cfg.exists("cache_dir"))
53
+    {
54
+        cache_dir = cfg["cache_dir"].c_str();
55
+    }
56
+    else
57
+    {
58
+        xdgHandle xdg;
59
+        xdgInitHandle(&xdg);
60
+        cache_dir = xdgCacheHome(&xdg);
61
+        cache_dir /= "cppscript";
62
+        xdgWipeHandle(&xdg);
63
+    }
64
+    if (!fs::is_directory(cache_dir))
65
+    {
66
+        fs::create_directories(cache_dir);
67
+    }
68
+
69
+    if (need_save)
70
+    {
71
+        config.write();
72
+    }
73
+}
74
+
3 75
 int main(int argc, char *argv[])
4 76
 {
77
+    read_settings();
78
+
79
+    if (argc > 1)
80
+    {
81
+        const fs::path path(argv[1]);
82
+        const fs::path binary = cache_dir / path.stem();
83
+        const fs::path source = binary.string() + ".cpp";
84
+
85
+        std::ifstream in(path);
86
+        if (in.is_open())
87
+        {
88
+            std::ofstream out(source);
89
+            if (out.is_open())
90
+            {
91
+                string buf;
92
+                std::getline(in, buf);
93
+                while (!in.eof())
94
+                {
95
+                    std::getline(in, buf);
96
+                    out << buf << endl;
97
+                }
98
+                in.close();
99
+                out.close();
100
+            }
101
+            else
102
+            {
103
+                cerr << "ERROR: Could not open file: " << source << endl;
104
+            }
105
+        }
106
+        else
107
+        {
108
+            cerr << "ERROR: Could not open file: " << path << endl;
109
+        }
110
+
111
+        std::system((compiler + " " + source.string()
112
+                     + " -o " + binary.string()).c_str());
113
+        execv(binary.c_str(), argv);
114
+    }
5 115
     return 0;
6 116
 }

+ 1
- 1
xdgcfg

@@ -1 +1 @@
1
-Subproject commit 6c0976baa74f959ed3218af56e56ff58202a5a05
1
+Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528

Loading…
Cancel
Save