File tree 7 files changed +26
-33
lines changed
7 files changed +26
-33
lines changed Original file line number Diff line number Diff line change 1
1
CXX =clang++
2
2
CPPFLAGS += -MMD -MP
3
3
CXXFLAGS += --std=c++20 -pedantic-errors -Weverything -Wno-c++98-compat -Wno-pre-c++20-compat-pedantic
4
- LDFLAGS += -Llib -fvisibility=default
4
+ LDFLAGS += -Llib -fvisibility=default
5
5
LDLIBS += -lgetargv
6
6
7
7
.PHONY := run db clean
@@ -10,7 +10,7 @@ LDLIBS += -lgetargv
10
10
run : bin/main
11
11
bin/main
12
12
13
- lib/libgetargv++.dylib : obj/libgetargv++.o obj/ argv.o obj/argvargc.o | lib
13
+ lib/libgetargv++.dylib : obj/argv.o obj/argvargc.o | lib
14
14
$(CXX ) -dynamiclib $(CXXFLAGS ) $(LDFLAGS ) $(LDLIBS ) $^ -o $@
15
15
16
16
bin/main : lib/libgetargv++.dylib obj/main.o | bin
@@ -24,7 +24,7 @@ bin lib obj:
24
24
25
25
db : compile_commands.json
26
26
27
- compile_commands.json :
27
+ compile_commands.json : Makefile
28
28
bear -- make -B bin/main
29
29
30
30
clean :
Original file line number Diff line number Diff line change 1
1
#include < cerrno>
2
- #include < cstdint>
3
2
#include < stdexcept>
4
- #include < string>
5
3
#include < system_error>
6
4
7
5
#include " libgetargv++.hpp"
Original file line number Diff line number Diff line change 1
1
#include < cerrno>
2
2
#include < stdexcept>
3
- #include < string>
4
3
#include < system_error>
5
4
6
5
#include " libgetargv++.hpp"
Original file line number Diff line number Diff line change 2
2
#define ITERGEN
3
3
4
4
#include < iterator>
5
+ namespace IterGen {
5
6
6
7
template <typename T> struct Iterator {
7
8
using iterator_concept [[maybe_unused]] = std::contiguous_iterator_tag;
8
9
using difference_type = std::ptrdiff_t ;
9
10
using element_type = T;
10
- // using value_type = element_type; // ← compiler doesn't seem to need this, but everyone online says it's needed…
11
+ // using value_type = element_type; // ← compiler doesn't seem to need this,
12
+ // but everyone online says it's needed…
11
13
using pointer = element_type *;
12
14
using reference = element_type &;
13
15
@@ -65,4 +67,5 @@ template <typename T> struct Iterator {
65
67
private:
66
68
pointer _ptr;
67
69
};
70
+ } // namespace IterGen
68
71
#endif
Original file line number Diff line number Diff line change 1
- #include " iter.hpp"
2
- #include < cstddef>
3
-
4
1
#ifndef LIBGETARGVPLUSPLUS_H
5
2
#define LIBGETARGVPLUSPLUS_H
6
3
4
+ #include " iter.hpp"
5
+ #include < cstddef>
6
+ #include < string>
7
+ #include < vector>
8
+
7
9
namespace ffi {
8
10
#include < libgetargv.h>
9
11
}
10
12
11
- /* TODO:
12
- - tests
13
- */
13
+ // TODO: tests
14
14
15
15
namespace Getargv {
16
16
17
17
struct Argv : protected ffi ::ArgvResult {
18
- using Iterator = Iterator<char >;
18
+ using Iterator = IterGen:: Iterator<char >;
19
19
20
20
static Argv as_bytes (pid_t pid, unsigned int skip = 0 ,
21
21
bool nuls = false ) noexcept (false );
@@ -35,15 +35,15 @@ struct Argv : protected ffi::ArgvResult {
35
35
};
36
36
37
37
struct ArgvArgc : protected ffi ::ArgvArgcResult {
38
- using Iterator = Iterator<char *>;
38
+ using Iterator = IterGen:: Iterator<char *>;
39
39
40
40
static ArgvArgc as_array (pid_t pid) noexcept (false );
41
41
static std::vector<std::string> as_string_array (pid_t pid) noexcept (false );
42
42
ArgvArgc (ArgvArgc &r) = delete ;
43
43
ArgvArgc (ffi::ArgvArgcResult &r);
44
44
~ArgvArgc ();
45
45
46
- char *&operator [](const ptrdiff_t index) const ;
46
+ char *&operator [](const ptrdiff_t index) const ;// auto converts to std::string
47
47
ptrdiff_t size () const ;
48
48
bool empty () const ;
49
49
Iterator begin () const ;
Original file line number Diff line number Diff line change 1
1
#include < algorithm>
2
+ #include < iostream>
2
3
#include < type_traits>
3
4
#include < unistd.h>
4
5
5
6
#include " libgetargv++.hpp"
6
7
using Getargv::Argv;
7
8
using Getargv::ArgvArgc;
8
9
9
- #include < iostream>
10
+ int main (int argc, char *argv[]) {
11
+ char **end = argv + argc;
12
+ bool nuls =
13
+ (argc > 1 ) && std::find_if (argv, end, [](auto c) { return c == " -0" ; });
10
14
11
- int main () {
12
- pid_t pid = getpid ();
15
+ char **itr = std::find (argv, end, " -s" );
16
+ unsigned int skip = (itr != end && ++itr != end) ? static_cast <unsigned int >(std::stoul (*itr)) : 0 ;
17
+
18
+ pid_t pid = (argc > 1 ) ? std::stoi (argv[argc - 1 ]) : getpid ();
13
19
14
20
static_assert (std::contiguous_iterator<Argv::Iterator>);
15
21
static_assert (std::contiguous_iterator<ArgvArgc::Iterator>);
16
22
17
- ArgvArgc array = ArgvArgc::as_array (pid);
18
- for (auto s : array) {
19
- std::cout << std::string (s) << " \n " ;
20
- }
21
- std::cout << " length: " << array.size () << " \n " ;
22
- std::cout << " length: " << std::distance (array.begin (), array.end ()) << " \n " ;
23
-
24
- Argv bytes = Argv::as_bytes (pid);
25
- for (auto c : bytes) {
26
- std::cout << c << " \n " ;
27
- }
28
-
29
- std::cout << " length: " << bytes.size () << " \n " ;
30
- std::cout << " length: " << std::distance (bytes.begin (), bytes.end ()) << " \n " ;
23
+ Argv bytes = Argv::as_bytes (pid, skip, nuls);
31
24
32
25
if (!bytes.print ()) {
33
26
std::cerr << " printing fucked up\n " ;
You can’t perform that action at this time.
0 commit comments