Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (127 loc) · 3.94 KB
/
Copy pathmain.cpp
File metadata and controls
159 lines (127 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* /////////////////////////////////////////////////////////////////////////
* File: examples/by_concept/resizeable_buffer/main.cpp
*
* Purpose: C++ example program demonstrating the Resizeable Buffer
* concept: probe-then-fill into `stlsoft::auto_buffer` and
* `std::vector` via `platformstl` traits `rb` overloads (see
* CONCEPTS.md).
*
* Created: 15th October 2026
* Updated: 29th October 2026
*
* ////////////////////////////////////////////////////////////////////// */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#include <platformstl/filesystem/filesystem_traits.hpp>
#include <platformstl/system/system_traits.hpp>
#include <stlsoft/memory/auto_buffer.hpp>
#include <stlsoft/util/resizeable_buffer_helpers.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
/* /////////////////////////////////////////////////////////////////////////
* types
*/
typedef platformstl::system_traits<char> system_traits_t;
typedef platformstl::filesystem_traits<char> filesystem_traits_t;
typedef stlsoft::auto_buffer<char> ab_t;
typedef std::vector<char> vec_t;
/* /////////////////////////////////////////////////////////////////////////
* helpers
*/
static
void
print_cstr_field(
char const* label
, char const* s
, size_t n
)
{
std::cout
<< label
<< " ("
<< n
<< " chars): '"
<< (NULL != s ? s : "")
<< "'"
<< std::endl;
}
/* /////////////////////////////////////////////////////////////////////////
* main()
*/
int main(int /* argc */, char* /* argv */[])
{
/* Preferred model: stlsoft::auto_buffer — empty demand, traits resize.
*/
{
ab_t home(0);
size_t const n = system_traits_t::get_home_directory(home);
if (0 == n)
{
std::cerr << "get_home_directory failed" << std::endl;
}
else
{
print_cstr_field("home (auto_buffer)", home.data(), n);
}
}
/* Also a model: std::vector — same rb overload contract.
*/
{
vec_t cwd;
size_t const n = filesystem_traits_t::get_current_directory(cwd);
if (0 == n)
{
std::cerr << "get_current_directory failed" << std::endl;
}
else
{
print_cstr_field("cwd (vector)", &cwd[0], n);
}
}
/* Environment value into an empty auto_buffer (traits resize via
* resizeable_buffer_resize internally).
*/
{
char const name[] = "PATH";
ab_t path(0);
size_t const n = system_traits_t::get_environment_variable(name, path);
if (0 == n)
{
std::cerr << "get_environment_variable(\"" << name << "\") failed" << std::endl;
}
else
{
print_cstr_field("PATH (auto_buffer)", path.data(), n);
}
}
/* Direct use of the helper: grow a vector before writing into it.
*/
{
vec_t scratch;
if (!stlsoft::resizeable_buffer_resize(scratch, 8))
{
std::cerr << "resizeable_buffer_resize failed" << std::endl;
return EXIT_FAILURE;
}
scratch[0] = 'r';
scratch[1] = 'b';
scratch[2] = '\0';
print_cstr_field("helper grown vector", &scratch[0], 2);
}
/* Path helper on an rb: append a directory separator when missing.
*/
{
vec_t dir;
size_t const n = filesystem_traits_t::get_current_directory(dir);
if (0 != n)
{
size_t const n2 = filesystem_traits_t::ensure_dir_end(dir);
print_cstr_field("cwd with dir-end", &dir[0], n2);
}
}
return EXIT_SUCCESS;
}
/* ///////////////////////////// end of file //////////////////////////// */