summaryrefslogtreecommitdiff
path: root/plugingui/directory.cc
blob: 48e67c6a0925dfeed13c480c3a17bd50c5dbfbb4 (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            directory.cc
 *
 *  Tue Apr 23 22:01:07 CEST 2013
 *  Copyright 2013 Jonas Suhr Christensen
 *  jsc@umbraculum.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "directory.h"

#include <dirent.h>
#include <stdio.h>
#include <string> 
#include <algorithm>
#include <vector>
#include <string.h>

#ifdef WIN32
#include <direct.h>
#include <windows.h>
#endif

#include <hugin.hpp>

#define DRUMKIT_SUFFIX ".xml"

// http://en.wikipedia.org/wiki/Path_(computing)
#ifdef WIN32
  #define SEP "\\"
#else
  #define SEP "/"
#endif

Directory::Directory(std::string path)
{
  setPath(path);
}

Directory::~Directory()
{
}

std::string Directory::seperator()
{
  return SEP;
}

void Directory::setPath(std::string path)
{
  DEBUG(directory, "Setting path to '%s'\n", path.c_str());
  this->_path = cleanPath(path);
  refresh();
}

size_t Directory::count()
{
  return _files.size();
}

void Directory::refresh()
{
  _files = listFiles(_path, DIRECTORY_HIDDEN);
//  _files = listFiles(_path);
}

bool Directory::cd(std::string dir)
{
  //TODO: Should this return true or false?
  if(dir.empty() || dir == ".") return true;

  DEBUG(directory, "Changing to '%s'\n", dir.c_str());
  if(exists(_path + SEP + dir)) {
    std::string path = _path + SEP + dir;
    setPath(path);
    refresh();
    return true;
  } else {
    return false;
  }
}

bool Directory::cdUp()
{
  return this->cd("..");
}

std::string Directory::path()
{
  return cleanPath(_path);
}

Directory::EntryList Directory::entryList()
{
  return _files;
}

#define MAX_FILE_LENGTH 1024
std::string Directory::cwd() {
  char path[MAX_FILE_LENGTH];
  char* c = getcwd(path, MAX_FILE_LENGTH);

  if(c) return c;
  else return "";
}

std::string Directory::cleanPath(std::string path)
{
  DEBUG(directory, "Cleaning path '%s'\n", path.c_str());

  Directory::Path pathlst = parsePath(path);
  return Directory::pathToStr(pathlst);  
}

Directory::EntryList Directory::listFiles(std::string path, unsigned char filter)
{
  DEBUG(directory, "Listing files in '%s'\n", path.c_str());

  Directory::EntryList entries;
  DIR *dir = opendir(path.c_str());
  if(!dir) {
    DEBUG(directory, "Couldn't open directory '%s\n", path.c_str()); 
    return entries;
  }

  std::vector<std::string> directories;
  std::vector<std::string> files;

  struct dirent *entry;
  while((entry = readdir(dir)) != NULL) {
    std::string name = entry->d_name;
    if(name == ".") continue;

    if(Directory::isRoot(path) && name == "..") continue;
    
    unsigned char entryinfo = 0;
    if(isHidden(path + SEP + name)) {
      entryinfo |= DIRECTORY_HIDDEN;
    }

    std::string entrypath = path;
    entrypath += SEP;
    entrypath += entry->d_name;
    if(Directory::isDir(entrypath)) {
      if(!(entryinfo && filter)) {
        if(name == "..") directories.push_back(entry->d_name);
        else directories.push_back(std::string(SEP) + entry->d_name);
      }
    }
    else {
      int drumkit_suffix_length = strlen(DRUMKIT_SUFFIX);
      if((int)name.size() < drumkit_suffix_length) continue;
      if(name.substr(name.length() - drumkit_suffix_length, 
          drumkit_suffix_length) != DRUMKIT_SUFFIX) {
        continue;
      }


//      if(!(entryinfo && filter)) {
        files.push_back(entry->d_name);
//      }
    }
  }


#ifdef WIN32
	DEBUG(directory, "Root is %s\n", Directory::root(path).c_str()); 
	DEBUG(directory, "Current path %s is root? %d", path.c_str(), Directory::isRoot(path)); 
if(Directory::isRoot(path)) entries.push_back(".."); 
#endif

  // sort
  for(int x = 0; x < (int)directories.size(); x++) {
    for(int y = 0; y < (int)directories.size(); y++) {
      if(directories[x] < directories[y]) {
        
        std::string tmp = directories[x];
        directories[x] = directories[y];
        directories[y] = tmp;
      }
    }
  }

  for(int x = 0; x < (int)files.size(); x++) {
    for(int y = 0; y < (int)files.size(); y++) {
      if(files[x] < files[y]) {
        
        std::string tmp = files[x];
        files[x] = files[y];
        files[y] = tmp;
      }
    }
  }


  for(std::vector<std::string>::iterator it = directories.begin(); it != directories.end(); it++) {
    entries.push_back(*it);
  }

  for(std::vector<std::string>::iterator it = files.begin(); it != files.end(); it++) {
    entries.push_back(*it);
  }


  return entries;
}

bool Directory::isRoot(std::string path)
{
#ifdef WIN32
  std::transform(path.begin(), path.end(), path.begin(), ::tolower);
  std::string root_str = Directory::root(path); 
  std::transform(root_str.begin(), root_str.end(), root_str.begin(), ::tolower);
  // TODO: This is not a correct root calculation, but works with partitions
  if(path.size() == 2) {
    if(path == root_str) return true;
    else return false;
  } else if (path.size() == 3) {
    if(path == root_str + SEP) return true;
    return false;
  } else {
    return false;
  }
#else
  if(path == SEP) return true;
  else return false;
#endif
}

std::string Directory::root()
{
  return root(cwd());
}

std::string Directory::root(std::string path)
{
#ifdef WIN32
  if(path.size() < 2) {
    return "c:"; // just something default when input is bad
  } else {
    return path.substr(0, 2);
  }
#else
  return SEP;
#endif
}

Directory::DriveList Directory::drives()
{
  Directory::DriveList drives;
#ifdef WIN32
  unsigned int d = GetLogicalDrives();
  for(int i = 0; i < 32; i++) {
    if(d & (1 << i)) {
      drive_t drive;
      char name[] = "x:";
      name[0] = i + 'a';

      drive.name = name;
      drive.number = i;
      drives.push_back(drive);
    }
  }
#endif
  return drives;
}

bool Directory::isDir()
{
  return isDir(_path);
}

bool Directory::isDir(std::string path)
{
  DEBUG(directory, "Is '%s' a directory?\n", path.c_str());
  struct stat st;
  if(stat(path.c_str(), &st) == 0) {
    if((st.st_mode & S_IFDIR) != 0) {
      DEBUG(directory, "\t...yes!\n");
      return true;
    }
  }
  DEBUG(directory, "\t...no!\n");
  return false;
}

bool Directory::fileExists(std::string filename)
{
  return !isDir(_path + SEP + filename);
}

bool Directory::exists(std::string path)
{
  struct stat st;
  if(stat(path.c_str(), &st) == 0) {
    return true;
  } else {
    return false;
  }
}

bool Directory::isHidden(std::string path) 
{
  DEBUG(directory, "Is '%s' hidden?\n", path.c_str());
  // TODO: Handle hidden and system files in windows
#ifdef WIN32
  DWORD fattribs = GetFileAttributes(path.c_str());
  if(fattribs & FILE_ATTRIBUTE_HIDDEN) {
    DEBUG(directory, "\t...yes!\n");
    return true;
  }
  else {
    DEBUG(directory, "\t...no!\n");
    return false;
  }
#else
  unsigned pos = path.find_last_of("/\\");
  std::string entry = path.substr(pos+1);
  if(entry.size() > 1 &&
     entry.at(0) == '.' &&
     entry.at(1) != '.') {
    DEBUG(directory, "\t...yes!\n");
    return true;
  }
  else {
    DEBUG(directory, "\t...no!\n");
    return false;
  }
#endif
}

Directory::Path Directory::parsePath(std::string path_str)
{
  //TODO: Handle "." input and propably other special cases

  DEBUG(directory, "Parsing path '%s'", path_str.c_str());
  Directory::Path path;
  
  std::string current_char;
  std::string prev_char;
  std::string dir;
  for(size_t c = 0; c < path_str.size(); c++) {
    current_char = path_str.at(c);

    if(current_char == SEP) {
      if(prev_char == SEP) {
        dir.clear();
        prev_char = current_char;
        continue;
      } else if(prev_char == ".") {
        prev_char = current_char;
        continue;
      }
      
      if(!dir.empty()) path.push_back(dir);
      dir.clear();
      continue;
    } else if(current_char == ".") {
      if(prev_char == ".") {
        dir.clear();
        if(!path.empty()) path.pop_back();
        continue;
      }
    }
    
    dir += current_char;
    prev_char = current_char;
  }

  if(!dir.empty()) path.push_back(dir);

  return path;
}

std::string Directory::pathToStr(Directory::Path& path)
{
  std::string cleaned_path;
  DEBUG(directory, "Number of directories in path is %d\n", (int)path.size());

  for(Directory::Path::iterator it = path.begin();
      it != path.end(); it++) {
    std::string dir = *it;
    DEBUG(directory, "\tDir '%s'\n", dir.c_str());
#ifdef WIN32
    if(it != path.begin()) cleaned_path += SEP;
    cleaned_path += dir;
#else
    cleaned_path += SEP + dir;
#endif
  }

  DEBUG(directory, "Cleaned path '%s'\n", cleaned_path.c_str());

  if(cleaned_path.empty()) { 
    cleaned_path = Directory::root();
#ifdef WIN32
    cleaned_path += SEP;
#endif  
  }

#ifdef WIN32
  if(cleaned_path.size() == 2) cleaned_path += SEP;
#endif

  return cleaned_path; 
}

std::string Directory::pathDirectory(std::string filepath)
{
  if(Directory::isDir(filepath)) return filepath;

  Directory::Path path = parsePath(filepath);
  if(path.size() > 0) path.pop_back();

  return Directory::pathToStr(path);
}