Newer
Older
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
// [io-guts.cpp]
// -------------------------------------------------------------------------------------------- //
// input/output tech subroutines
// -------------------------------------------------------------------------------------------- //
#define _CRT_SECURE_NO_DEPRECATE
#include "io-guts.h"
#include <stdio.h>
#include <string.h>
#include <sstream>
// --- including for directory operations
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <errno.h>
// -------------------------------------------------------------------------------------------- //
// * Implementation *
// -------------------------------------------------------------------------------------------- //
// * append index * //
// -------------------------------------------------------------------------------------------- //
const std::string scm::append_index(
const std::string& name, const int index)
{
std::ostringstream sidx;
sidx << index;
const int ext_pos = name.find_last_of(".");
std::string app_name = name;
if (ext_pos == std::string::npos)
app_name.append(sidx.str());
else
app_name.insert(ext_pos, sidx.str());
return app_name;
}
// -------------------------------------------------------------------------------------------- //
// * create dir * //
// -------------------------------------------------------------------------------------------- //
bool scm::create_dir(const std::string& path)
{
// empty path is OK [local DIR]
if (path.empty()) return true;
char *c_path;
c_path = new char[path.length() + 1];
strcpy(c_path, path.c_str());
int status;
char *ptr = c_path + 1; // not empty string
while (*ptr != '\0')
{
if (*ptr == '/')
{
*ptr = '\0';
#ifdef _WIN32
status = _mkdir(c_path);
#else
status = mkdir(c_path, S_IRWXU); // add group?: S_IRWXG
#endif
if (!((status == 0) ||
((status == -1) && (errno == EEXIST))))
{
delete[] c_path;
return false;
}
*ptr = '/';
}
ptr++;
}
#ifdef _WIN32
status = _mkdir(c_path);
#else
status = mkdir(c_path, S_IRWXU); // add group?: S_IRWXG
#endif
delete[] c_path;
return ((status == 0) ||
((status == -1) && (errno == EEXIST)));
}
// -------------------------------------------------------------------------------------------- //
// * getline * //
// -------------------------------------------------------------------------------------------- //
char* scm::getline(char** buf, int* size, FILE* ptr)
{
const int c_min_size = 512;
if (*size < c_min_size) {
if (*size > 0) delete[](*buf);
*size = c_min_size;
*buf = new char[*size];
}
if (fgets(&(*buf)[0], *size, ptr) == NULL) return NULL;
int bidx = strlen(*buf);
while (!feof(ptr) && ((*buf)[bidx - 1] != '\n'))
{
// --- reading more ...
// --- allocating additional memory
*size += c_min_size;
char *hbuf = new char[*size];
strcpy(hbuf, *buf);
// --- pointing buf to new memory
delete[](*buf);
*buf = hbuf;
// --- reading & removing termination character
if (fgets(&(*buf)[bidx], *size - bidx, ptr) == NULL) {
if (!feof(ptr)) return NULL;
}
bidx = strlen(*buf);
}
return *buf;
}
// -------------------------------------------------------------------------------------------- //
// * convert to string * //
// -------------------------------------------------------------------------------------------- //
std::string scm::convert_to_string(const int value)
{
std::ostringstream stream;
stream << value;
return stream.str();
}
std::string scm::convert_to_string(const double value)
{
std::ostringstream stream;
stream << value;
return stream.str();
}
// -------------------------------------------------------------------------------------------- //