-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cc
163 lines (132 loc) · 3.07 KB
/
util.cc
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
159
160
161
162
163
/* Utility functions for use with TagReport. */
#include "config.h"
#include <cerrno>
#include <ctime>
#include <cstring>
#include <cstdio>
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#ifdef HAVE_LIBIBERTY_H
#include <libiberty.h>
#endif
#include "tagreport.h"
using namespace std;
/* Allocates stuff on the heap - free it! */
char* get_time_string (void)
{
struct tm *now;
time_t now_secs;
/* Length: "HH:MM, YYYY-MM-DD" == 17 + NUL == 18 */
char *now_date = new char[18];
/* Get the current time */
if (!time(&now_secs))
perror("time");
now = localtime(&now_secs);
strftime (now_date, 18, "%H:%M, %Y-%m-%d", now);
return now_date;
}
char* guess_fn (char* a)
{
char *ext, *s;
ostringstream t;
struct stat ex;
if (stat(a, &ex) == 0)
return strdup(a);
/* Conditions:
*
* 1. Path is not absolute (prefixed with a forward slash)
*
* and either
*
* 2. The filename has no extension
*
* or
*
* 3. The filename has an empty extension
* 4. The filename's extension is NOT 'def'
*
* then we should attempt to guess from our hardcoded paths and
* the current path with def/.
*/
else if (*a != '/' && ((ext = strrchr(a, '.')) == NULL ||
((a[strlen(a) - 1] != *ext) && strcmp(ext+1, "def"))))
{
size_t slen = strlen(a) + 8;
s = (char*)malloc(slen + 1);
snprintf(s, slen + 1, "def/%s.def", a);
if (stat (s, &ex) == 0)
return s;
else
{
/* Note: sizeof(DATADIR) also counts the NUL, so no +1 needed */
size_t tdlen = sizeof(DATADIR) + slen;
char *td = (char*)malloc(tdlen);
snprintf(td, tdlen, "%s%s", DATADIR, s);
free (s);
if (stat(td, &ex) == 0)
return td;
else
{
free (td);
return strdup(a);
}
}
}
else
{
cerr << "Error reading template " << a << ": " << strerror(errno) << endl;
if (!force)
{
cerr << "Continuing with default template." << endl;
return NULL;
}
else
exit(1);
}
}
char* comma_delineate (const vector<char*> & in)
{
vector<char*>::const_iterator i;
ostringstream a;
for (i = in.begin(); i != in.end(); i++)
{
if (i + 1 != in.end())
a << *i << ", ";
else
a << *i;
}
return strdup(a.str().c_str());
}
void verify (vector<char*> & targets)
{
vector<char*>::iterator t;
struct stat id;
for (t = targets.begin(); t != targets.end(); t++)
{
DEBUG("Now at", *t);
if (stat(*t, &id) == -1 || !S_ISDIR(id.st_mode))
{
cerr << "Error opening " << *t << ": not a directory!" << endl;
targets.erase(t);
t--;
}
}
}
/* Simply purges the entries in a vector. */
void clean (vector<struct Song *> * root)
{
vector<struct Song *>::iterator v;
/* All strings are on the stack, we don't need to do anything with them */
for (v = root->begin(); v != root->end(); v++)
delete *v;
delete root;
}
void clean (vector<char*> & dirs)
{
vector<char*>::iterator t;
for (t = dirs.begin(); t != dirs.end(); t++)
free (*t);
}