cgv
Loading...
Searching...
No Matches
cmdline_tools.cxx
1#include "cmdline_tools.h"
2#include <cgv/utils/file.h>
3#include <cgv/utils/scan.h>
4#include <cgv/utils/progression.h>
5#include <stdio.h>
6
7#if WIN32
8#define popen(...) _popen(__VA_ARGS__);
9#define pclose(...) _pclose(__VA_ARGS__);
10#endif
11
12namespace cgv {
13 namespace os {
14
15 bool expand_archive(const std::string& file_path, const std::string& destination_path)
16 {
17 std::string command;
18#ifdef WIN32
19 command = "PowerShell -Command \"Expand-Archive -Path '";
20 command += file_path + "' -DestinationPath '" + destination_path + "'\"";
21 cgv::utils::replace(command, '/', '\\');
22#else
23 command = "unzip '";
24 command += file_path + "' -d '" + destination_path + "'";
25 command = cgv::utils::file::clean_path(command);
26#endif
27 int result = system(command.c_str());
28 return result != -1;
29 }
30
31 bool compress_archive(const std::string& file_filter_path, const std::string& archive_path)
32 {
33 std::string command;
34#ifdef WIN32
35 command = "PowerShell -Command \"Compress-Archive -Path '";
36 command += file_filter_path + "' -DestinationPath '" + archive_path + "'\"";
37#else
38 command = "zip '";
39 command += archive_path + "' '" + file_filter_path + "'";
40#endif
41 int result = system(command.c_str());
42 return result != -1;
43 }
44
45
46 std::string query_system_output(std::string cmd, bool cerr)
47 {
48 std::string data;
49 FILE* stream;
50 const int max_buffer = 256;
51 char buffer[max_buffer];
52 if (cerr)
53 cmd.append(" 2>&1");
54
55 stream = popen(cmd.c_str(), "r");
56
57 if (stream) {
58 while (!feof(stream))
59 if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
60 pclose(stream);
61 }
62 return data;
63 }
64 size_t read_system_output(std::string cmd, uint8_t* buffer, size_t buffer_size, const char* progression_text, bool use_cerr, void (*on_progress_update)(int, void*), void* user_data, size_t block_size, bool cycle_till_eof)
65 {
66 cgv::utils::progression* prog_ptr = 0;
67 if (progression_text != 0)
68 prog_ptr = new cgv::utils::progression(progression_text, buffer_size / block_size, 20);
69 FILE* fp;
70 if (use_cerr)
71 cmd.append(" 2>&1");
72#ifdef WIN32
73 const char* mode = "rb";
74#else
75 const char* mode = "r";
76#endif
77 fp = popen(cmd.c_str(), mode);
78 size_t nr_bytes_read = 0;
79 int block_cnt = 0;
80 if (fp) {
81 while ((nr_bytes_read < buffer_size || cycle_till_eof) && !feof(fp)) {
82 size_t nr_bytes = block_size;
83 if (!cycle_till_eof)
84 nr_bytes = std::min(nr_bytes, buffer_size - nr_bytes_read);
85 size_t offset = nr_bytes_read % buffer_size;
86 size_t nr_read = fread(buffer + offset, 1, nr_bytes, fp);
87 if (prog_ptr)
88 prog_ptr->step();
89 if (on_progress_update)
90 on_progress_update(++block_cnt, user_data);
91 nr_bytes_read += nr_read;
92 if (nr_read < nr_bytes)
93 break;
94 }
95 pclose(fp);
96 }
97 return nr_bytes_read;
98 }
99 FILE* open_system_input(const std::string& cmd, bool in_binary_mode)
100 {
101#ifdef WIN32
102 const char* mode = in_binary_mode ? "wb" : "w";
103#else
104 const char* mode = "w";
105#endif
106 return popen(cmd.c_str(), mode);
107 }
108 FILE* open_system_output(const std::string& cmd, bool in_binary_mode)
109 {
110#ifdef WIN32
111 const char* mode = in_binary_mode ? "rb" : "r";
112#else
113 const char* mode = "r";
114#endif
115 return popen(cmd.c_str(), mode);
116 }
117 int close_system_input(FILE* fp)
118 {
119 return pclose(fp);
120 }
121
122 }
123}
unsigned int replace(std::string &s, char c1, char c2)
replace char c1 with c2 in the given string _s and return number of replacements
Definition scan.cxx:159
the cgv namespace
Definition print.h:11
Helper functions to process strings.
progression provides a simple possibility to show progression of process in console.
Definition progression.h:14
void step()
next iteration