-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilename.cpp
More file actions
94 lines (84 loc) · 1.97 KB
/
filename.cpp
File metadata and controls
94 lines (84 loc) · 1.97 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
/* ************************************************************************ *
* Written by Alex de Kruijff 21 April 2009 *
* ************************************************************************ *
* This source was written with a tabstop every four characters *
* In vi type :set ts=4 *
* ************************************************************************ */
#include "filename.h"
#include <string.h>
#ifdef DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef WITH_REGEX
static const char *empty = "";
#endif
#include <new>
Filename::Filename(size_t size) throw (std::bad_alloc)
{
this->str = new char[++size]; // throws bad_alloc
#ifdef DEBUG
if (size == 0)
{
fprintf(stderr, "%s:%d size shouldn't be 0\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
this->str[0] = 0;
}
Filename::Filename(const char *str) throw (std::bad_alloc)
{
#ifdef DEBUG
if (str == NULL)
{
fprintf(stderr, "%s:%d str shouldn't be NULL\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
size_t len = strlen(str);
this->str = new char[++len]; // throws bad_alloc
#ifdef DEBUG
if (len == 0)
{
fprintf(stderr, "%s:%d len shouldn't be 0\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
memcpy(this->str, str, len);
}
void Filename::resize(size_t size) throw (std::bad_alloc)
{
size_t len = strlen(str);
if (size < len)
size = len;
char *tmp = new char[++size]; // throws bad_alloc
#ifdef DEBUG
if (size == 0)
{
fprintf(stderr, "%s:%d size shouldn't be 0\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
memcpy(tmp, str, ++len);
delete[] str;
str = tmp;
}
void Filename::renew(size_t size) throw (std::bad_alloc)
{
char *tmp = new char[++size]; // throws bad_alloc
#ifdef DEBUG
if (size == 0)
{
fprintf(stderr, "%s:%d size shouldn't be 0\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
delete[] str;
str = tmp;
str[0] = 0;
}