summaryrefslogtreecommitdiffstats
path: root/loadSongs.cpp
blob: 094821233c84b3b08fb760759aeea12035a6bdf9 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <dirent.h>
#include <sys/stat.h>
#include <ctype.h>
#include <sstream>
#include <algorithm>
#include <string>

#include "logging.h"
#include "files.h"

const char *songsPath = "/sdcard/Songs/";

void split(std::vector<std::string> &tokens, const std::string &text, const char *sep) {
  int start = 0, end = 0;
  while ((end = text.find(sep, start)) != std::string::npos) {
    tokens.push_back(text.substr(start, end - start));
    start = end + 1;
  }
  tokens.push_back(text.substr(start));
}

// trim from start
static inline std::string &ltrim(std::string &s) {
        s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(isspace))));
        return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
        s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(isspace))).base(), s.end());
        return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
        return ltrim(rtrim(s));
}

/* Returns a list of directories (except the ones that begin with a dot) */

void GetDirectories(std::vector<std::string> &out, const char *directory)
{
#ifdef WINDOWS
    HANDLE dir;
    WIN32_FIND_DATA file_data;

    if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
    	return; /* No files found */

    do {
    	const string file_name = file_data.cFileName;
    	const string full_file_name = directory + "/" + file_name;
    	const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

    	if (file_name[0] == '.')
    		continue;

    	if (!is_directory)
    		continue;

    	out.push_back(full_file_name);
    } while (FindNextFile(dir, &file_data));

    FindClose(dir);
#else
    DIR *dir;
    class dirent *ent;
    class stat st;

    dir = opendir(directory);
	if(dir == NULL)
	{
		std::string errmsg = "Cannot open directory ";
		logstdError("GetDirectories", errmsg + directory);
	}
    else
	{
		while ((ent = readdir(dir)) != NULL) {
			const std::string file_name = ent->d_name;
			std::string full_file_name = directory;
			full_file_name.append("/" + file_name + "/");

			if (file_name[0] == '.')
				continue;

			if (stat(full_file_name.c_str(), &st) == -1)
				continue;

			if (!S_ISDIR(st.st_mode))
				continue;

			out.push_back(full_file_name);
		}
		closedir(dir);
		if(out.empty())
		{
			std::string errmsg = "Directory empty: ";
			logstdError("GetDirectories", errmsg + directory);
		}
	}
#endif
} // GetFilesInDirectory

/* Returns a list of sm files */

void GetSmFiles(std::vector<std::string> &out, const std::string directory)
{
#ifdef WINDOWS
    HANDLE dir;
    WIN32_FIND_DATA file_data;

    if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
    	return; /* No files found */

    do {
    	const string file_name = file_data.cFileName;
    	const string full_file_name = directory + "/" + file_name;
    	const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

		std::vector<std::string> tokens;
		split(tokens, file_name, ".");

    	if (strcmp(tokens.back(), "sm"))
    		continue;

    	if (is_directory)
    		continue;

    	out.push_back(full_file_name);
    } while (FindNextFile(dir, &file_data));

    FindClose(dir);
#else
    DIR *dir;
    class dirent *ent;
    class stat st;

    dir = opendir(directory.c_str());
	if(dir == NULL)
	{
		std::string errmsg = "Cannot open directory ";
		logstdError("GetDirectories", errmsg + directory);
	}
    else
	{
		while ((ent = readdir(dir)) != NULL) {
			const std::string file_name = ent->d_name;
			std::string full_file_name = directory;
			full_file_name.append("/" + file_name);

			std::vector<std::string> tokens;
			split(tokens, file_name, ".");

			if (tokens.back() != "sm")
				continue;

			if (stat(full_file_name.c_str(), &st) == -1)
				continue;

			const bool is_readable = (st.st_mode & S_IRUSR) != 0;

			if (!is_readable)
				continue;

			out.push_back(file_name);
		}
		closedir(dir);
		if(out.empty())
		{
			std::string errmsg = "Directory empty: ";
			logstdError("GetDirectories", errmsg + directory);
		}
	}
#endif
} // GetSmFiles


int readSongFile(std::string &path, musicfile &songFile)
{
	bool reading_note = false;
	int current_note_line = 0;
	std::vector<Note> currentNoteData;
	std::vector<std::vector<Note>> currentLineNoteData;
	notes currentNotes;

    std::string line;
    std::ifstream file;
    file.open(path.c_str(), std::ifstream::in);
    if (!file)
    {
        std::string msg = "Could not open ";
        msg.append(path);
        logstdError("loadFile", msg);
        return 1;
    }
    while(file.good())
    {
        std::getline(file, line);
        line = trim(line);
        if(line.length() == 0)
            continue;
		else if(line[0] == '/')
			continue;
        else if(line[0] == '#')
        {
            std::vector<std::string> tokens;
            split(tokens, line, ":");

            if(tokens[0].compare("#NOTES") == 0)
            {
				reading_note = true;
				continue;
            }
			else
				reading_note = false;

            if(*tokens[1].rbegin() == ';')
                tokens[1].pop_back();

            if(tokens[0].compare("#TITLE") == 0)
            {
                songFile.title = tokens[1];
            }
            else if(tokens[0].compare("#ARTIST") == 0)
            {
                songFile.artist = tokens[1];
            }
            else if(tokens[0].compare("#BANNER") == 0)
            {
                songFile.banner = tokens[1];
            }
            else if(tokens[0].compare("#BACKGROUND") == 0)
            {
                songFile.background = tokens[1];
            }
            else if(tokens[0].compare("#MUSIC") == 0)
            {
                songFile.file = tokens[1];
            }
            else if(tokens[0].compare("#OFFSET") == 0)
            {
		std::istringstream(tokens[1]) >> songFile.offset;
            }
            else if(tokens[0].compare("#BPMS") == 0)
            {
				std::vector<std::string> listBpms;
				split(listBpms, tokens[1], ",");
				for(std::vector<std::string>::iterator bpmData = listBpms.begin(); bpmData != listBpms.end(); ++bpmData)
				{
					std::vector<std::string> bpm;
					split(bpm, *bpmData, "=");
					songBpm bpms;
					std::istringstream(bpm[0]) >> bpms.beat;
					std::istringstream(bpm[1]) >> bpms.bpm;
					songFile.bpms.push_back(bpms);
				}
            }
        }
		else if(reading_note)
		{
            std::vector<std::string> tokens;
            split(tokens, line, ":");

			if((*line.rbegin() == ':') || (tokens.size() > 1))
			{
				switch(current_note_line)
				{
					case 0:
						currentNotes.type = tokens[0];
						break;
					case 1:
						currentNotes.description = tokens[0];
						break;
					case 2:
						currentNotes.difficultyClass = tokens[0];
						break;
					case 3:
						std::istringstream ( tokens[1] ) >> currentNotes.difficultyMeter;
						break;
					case 4:
						currentNotes.radarValues = tokens[0];
						break;
					default:
						std::string msg = "Too much ':' for #NOTES in ";
      						msg.append(path);
						logstdError("readSongFile", msg);
						break;
				}
				current_note_line += 1;
			}
			else if(line[0] == ',')
			{
				currentNotes.noteData.push_back(currentLineNoteData);
				currentLineNoteData.clear();
			}
			else if(line[0] == ';')
			{
				current_note_line = 0;
				songFile.songNotes.push_back(currentNotes);
				static const notes tempStruct;
				currentNotes = tempStruct;
			}
			else
			{
				for(std::string::iterator c = line.begin(); c != line.end(); ++c) {
					switch(*c)
					{
						case '0':
							currentNoteData.push_back(None);
							break;
						case '1':
 						        currentNoteData.push_back(Tap);
							break;
						case '2':
 						        currentNoteData.push_back(Hold_begin);
							break;
						case '3':
   						        currentNoteData.push_back(Hold_end);
							break;
						case '4':
   		  				        currentNoteData.push_back(Roll);
							break;
						case 'M':
    							currentNoteData.push_back(Mine);
							break;
						case 'L':
     							currentNoteData.push_back(Lift);
							break;
						case 'F':
      							currentNoteData.push_back(Fake);
							break;
					}
				}
				currentLineNoteData.push_back(currentNoteData);
				currentNoteData.clear();
			}
		}
    }
    if(!file.eof())
    {
        logstdError("readSongFile", "Line cannot be read");
    }
    file.close();
    return 0;
}

std::vector<musicfile> loadSongs()
{
	std::vector<std::string> directories;
	std::vector<std::string> smfiles;
	std::vector<musicfile> songs;
	
	GetDirectories(directories, songsPath);

	for(std::vector<std::string>::iterator it = directories.begin(); it != directories.end(); ++it) {
	    GetSmFiles(smfiles, *it);
		for(std::vector<std::string>::iterator ite = smfiles.begin(); ite != smfiles.end(); ++ite) {
			musicfile songData;
			std::string file = *it + *ite;
			if(!readSongFile(file, songData))
				songData.path = *it;
				songs.push_back(songData);
		}
	}
	return songs;
}