3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "bcsignals.h"
31 #include <sys/types.h>
35 #include "filesystem.h"
44 FileItem::FileItem(char *path,
51 int64_t calendar_time)
53 this->path = new char[strlen(path)];
54 this->name = new char[strlen(name)];
55 if(this->path) strcpy(this->path, path);
56 if(this->name) strcpy(this->name, name);
57 this->is_dir = is_dir;
62 this->calendar_time = calendar_time;
72 if(this->path) delete [] this->path;
73 if(this->name) delete [] this->name;
85 int FileItem::set_path(char *path)
87 if(this->path) delete [] this->path;
88 this->path = new char[strlen(path) + 1];
89 strcpy(this->path, path);
93 int FileItem::set_name(char *name)
95 if(this->name) delete [] this->name;
96 this->name = new char[strlen(name) + 1];
97 strcpy(this->name, name);
101 const char* FileItem::get_path()
106 const char* FileItem::get_name()
111 int FileItem::get_is_dir()
118 FileSystem::FileSystem()
121 (void)getcwd(current_dir, BCTEXTLEN);
124 FileSystem::~FileSystem()
129 int FileSystem::reset_parameters()
134 strcpy(current_dir, "");
135 sort_order = SORT_ASCENDING;
136 sort_field = SORT_PATH;
140 int FileSystem::delete_directory()
142 for(int i = 0; i < dir_list.total; i++)
144 delete dir_list.values[i];
146 dir_list.remove_all();
150 int FileSystem::set_sort_order(int value)
152 this->sort_order = value;
156 int FileSystem::set_sort_field(int field)
158 this->sort_field = field;
162 // filename.with.dots.extension
164 // extension.dots.with.filename
166 int FileSystem::dot_reverse_filename(char *out, const char *in)
168 int i, i2, j=0, lastdot;
169 lastdot = strlen(in);
170 for ( i=strlen(in); --i >= 0; ) {
179 if (in[++i] != '.') {
180 while (i < lastdot) out[j++] = in[i++];
186 int FileSystem::path_ascending(const void *ptr1, const void *ptr2)
188 FileItem *item1 = *(FileItem**)ptr1;
189 FileItem *item2 = *(FileItem**)ptr2;
190 //printf("path_ascending %p %p\n", ptr1, ptr2);
191 return strcasecmp(item1->name, item2->name);
194 int FileSystem::path_descending(const void *ptr1, const void *ptr2)
196 FileItem *item1 = *(FileItem**)ptr1;
197 FileItem *item2 = *(FileItem**)ptr2;
198 return strcasecmp(item2->name, item1->name);
202 int FileSystem::size_ascending(const void *ptr1, const void *ptr2)
204 FileItem *item1 = *(FileItem**)ptr1;
205 FileItem *item2 = *(FileItem**)ptr2;
206 return item1->size >= item2->size;
209 int FileSystem::size_descending(const void *ptr1, const void *ptr2)
211 FileItem *item1 = *(FileItem**)ptr1;
212 FileItem *item2 = *(FileItem**)ptr2;
213 return item1->size <= item2->size;
217 int FileSystem::date_ascending(const void *ptr1, const void *ptr2)
219 FileItem *item1 = *(FileItem**)ptr1;
220 FileItem *item2 = *(FileItem**)ptr2;
221 return item1->calendar_time >= item2->calendar_time;
224 int FileSystem::date_descending(const void *ptr1, const void *ptr2)
226 FileItem *item1 = *(FileItem**)ptr1;
227 FileItem *item2 = *(FileItem**)ptr2;
228 return item1->calendar_time <= item2->calendar_time;
231 int FileSystem::ext_ascending(const void *ptr1, const void *ptr2)
233 char dotreversedname1[BCTEXTLEN], dotreversedname2[BCTEXTLEN];
234 FileItem *item1 = *(FileItem**)ptr1;
235 FileItem *item2 = *(FileItem**)ptr2;
236 dot_reverse_filename(dotreversedname1,item1->name);
237 dot_reverse_filename(dotreversedname2,item2->name);
238 return strcasecmp(dotreversedname1, dotreversedname2);
241 int FileSystem::ext_descending(const void *ptr1, const void *ptr2)
243 char dotreversedname1[BCTEXTLEN], dotreversedname2[BCTEXTLEN];
244 FileItem *item1 = *(FileItem**)ptr1;
245 FileItem *item2 = *(FileItem**)ptr2;
246 dot_reverse_filename(dotreversedname1,item1->name);
247 dot_reverse_filename(dotreversedname2,item2->name);
248 return strcasecmp(dotreversedname2, dotreversedname1);
251 int FileSystem::sort_table(ArrayList<FileItem*> *dir_list)
253 #define SORT_MACRO(compare) \
254 qsort(dir_list->values, dir_list->size(), sizeof(FileItem*), compare);
256 if(!dir_list || !dir_list->size()) return 0;
258 //printf("FileSystem::sort_table %p\n", dir_list->values);
262 if(sort_order == SORT_ASCENDING)
263 SORT_MACRO(path_ascending)
265 SORT_MACRO(path_descending)
268 if(sort_order == SORT_ASCENDING)
269 SORT_MACRO(size_ascending)
271 SORT_MACRO(size_descending)
274 if(sort_order == SORT_ASCENDING)
275 SORT_MACRO(date_ascending)
277 SORT_MACRO(date_descending)
280 if(sort_order == SORT_ASCENDING)
281 SORT_MACRO(ext_ascending)
283 SORT_MACRO(ext_descending)
290 int FileSystem::combine(ArrayList<FileItem*> *dir_list, ArrayList<FileItem*> *file_list)
294 sort_table(dir_list);
295 for(i = 0; i < dir_list->total; i++)
297 this->dir_list.append(dir_list->values[i]);
300 sort_table(file_list);
301 for(i = 0; i < file_list->total; i++)
303 this->dir_list.append(file_list->values[i]);
308 void FileSystem::alphabetize()
310 sort_table(&dir_list);
313 int FileSystem::is_root_dir(char *path)
315 if(!strcmp(current_dir, "/")) return 1;
319 int FileSystem::test_filter(FileItem *file)
321 char *filter1 = 0, *filter2 = filter, *subfilter1, *subfilter2;
323 int done = 0, token_done;
324 int token_number = 0;
326 // Don't filter directories
327 if(file->is_dir) return 0;
329 // Empty filename string
330 if(!file->name) return 1;
335 filter1 = strchr(filter2, '[');
342 filter2 = strchr(filter1, ']');
347 for(i = 0; filter1 + i < filter2; i++)
348 string[i] = filter1[i];
353 strcpy(string, filter1);
360 strcpy(string, filter);
368 char *path = file->name;
376 subfilter2 = strchr(subfilter1, '*');
381 for(i = 0; subfilter1 + i < subfilter2; i++)
382 string2[i] = subfilter1[i];
388 strcpy(string2, subfilter1);
394 // Subfilter must exist at some later point in the string
395 if(subfilter1 > string)
397 if(!strstr(path, string2))
403 path = strstr(path, string2) + strlen(string2);
406 // Subfilter must exist at this point in the string
408 if(strncmp(path, string2, strlen(string2)))
409 // if(strncasecmp(path, string2, strlen(string2)))
415 path += strlen(string2);
418 // String must terminate after subfilter
428 subfilter1 = subfilter2 + 1;
429 // Let pass if no subfilter
430 }while(!token_done && !result);
433 }while(!done && result);
439 int FileSystem::update(const char *new_dir)
442 struct dirent64 *new_filename;
447 char full_path[BCTEXTLEN], name_only[BCTEXTLEN];
448 ArrayList<FileItem*>directories;
449 ArrayList<FileItem*>files;
453 if(new_dir != 0) strcpy(current_dir, new_dir);
454 dirstream = opendir(current_dir);
455 if(!dirstream) return 1; // failed to open directory
457 while( (new_filename = readdir64(dirstream)) != 0 )
461 // File is directory heirarchy
462 if(!strcmp(new_filename->d_name, ".") ||
463 !strcmp(new_filename->d_name, "..")) include_this = 0;
465 // File is hidden and we don't want all files
468 new_filename->d_name[0] == '.') include_this = 0;
473 new_file = new FileItem;
474 sprintf(full_path, "%s", current_dir);
475 if(!is_root_dir(current_dir)) strcat(full_path, "/");
476 strcat(full_path, new_filename->d_name);
477 strcpy(name_only, new_filename->d_name);
478 new_file->set_path(full_path);
479 new_file->set_name(name_only);
481 // Get information about the file.
482 if(!stat(full_path, &ostat))
484 new_file->size = ostat.st_size;
485 mod_time = localtime(&(ostat.st_mtime));
486 new_file->month = mod_time->tm_mon + 1;
487 new_file->day = mod_time->tm_mday;
488 new_file->year = mod_time->tm_year + 1900;
489 new_file->calendar_time = ostat.st_mtime;
491 if(S_ISDIR(ostat.st_mode))
493 strcat(name_only, "/"); // is a directory
494 new_file->is_dir = 1;
497 // File is excluded from filter
498 if(include_this && test_filter(new_file)) include_this = 0;
499 //printf("FileSystem::update 3 %d %d\n", include_this, test_filter(new_file));
501 // File is not a directory and we just want directories
502 if(include_this && want_directory && !new_file->is_dir) include_this = 0;
506 printf("FileSystem::update %s %s\n", full_path, strerror(errno));
513 if(new_file->is_dir) directories.append(new_file);
514 else files.append(new_file);
520 //printf("FileSystem::update %d\n", __LINE__);
523 // combine the directories and files in the master list
524 combine(&directories, &files);
526 directories.remove_all();
528 //printf("FileSystem::update %d\n", __LINE__);
534 int FileSystem::set_filter(const char *new_filter)
536 strcpy(filter, new_filter);
540 int FileSystem::set_show_all()
546 int FileSystem::set_want_directory()
552 int FileSystem::is_dir(const char *path) // return 0 if the text is a directory
554 if(!strlen(path)) return 0;
556 char new_dir[BCTEXTLEN];
557 struct stat ostat; // entire name is a directory
559 strcpy(new_dir, path);
560 complete_path(new_dir);
561 if(!stat(new_dir, &ostat) && S_ISDIR(ostat.st_mode))
567 int FileSystem::create_dir(const char *new_dir_)
569 char new_dir[BCTEXTLEN];
570 strcpy(new_dir, new_dir_);
571 complete_path(new_dir);
573 mkdir(new_dir, S_IREAD | S_IWRITE | S_IEXEC);
577 int FileSystem::parse_tildas(char *new_dir)
579 if(new_dir[0] == 0) return 1;
581 // Our home directory
582 if(new_dir[0] == '~')
585 if(new_dir[1] == '/' || new_dir[1] == 0)
587 // user's home directory
589 char string[BCTEXTLEN];
590 home = getenv("HOME");
592 // print starting after tilda
593 if(home) sprintf(string, "%s%s", home, &new_dir[1]);
594 strcpy(new_dir, string);
598 // Another user's home directory
600 char string[BCTEXTLEN], new_user[BCTEXTLEN];
604 for(i = 1, j = 0; new_dir[i] != 0 && new_dir[i] != '/'; i++, j++)
606 new_user[j] = new_dir[i];
611 while( (pw = getpwent()) != 0 )
614 if(!strcmp(pw->pw_name, new_user))
616 // print starting after tilda
617 sprintf(string, "%s%s", pw->pw_dir, &new_dir[i]);
618 strcpy(new_dir, string);
629 int FileSystem::parse_directories(char *new_dir)
631 //printf("FileSystem::parse_directories 1 %s\n", new_dir);
632 if(new_dir[0] != '/')
634 // extend path completely
635 char string[BCTEXTLEN];
636 //printf("FileSystem::parse_directories 2 %s\n", current_dir);
637 if(!strlen(current_dir))
639 // no current directory
640 strcpy(string, new_dir);
643 if(!is_root_dir(current_dir))
645 // current directory is not root
646 if(current_dir[strlen(current_dir) - 1] == '/')
647 // current_dir already has ending /
648 sprintf(string, "%s%s", current_dir, new_dir);
651 sprintf(string, "%s/%s", current_dir, new_dir);
654 sprintf(string, "%s%s", current_dir, new_dir);
656 //printf("FileSystem::parse_directories 3 %s %s\n", new_dir, string);
657 strcpy(new_dir, string);
658 //printf("FileSystem::parse_directories 4\n");
663 int FileSystem::parse_dots(char *new_dir)
665 // recursively remove ..s
670 len = strlen(new_dir);
672 for(i = 0, j = 1; !changed && j < len; i++, j++)
675 if(new_dir[i] == '.' && new_dir[j] == '.')
677 // Ignore if character after .. doesn't qualify
679 new_dir[j + 1] != ' ' &&
680 new_dir[j + 1] != '/')
683 // Ignore if character before .. doesn't qualify
685 new_dir[i - 1] != '/') continue;
688 while(new_dir[i] != '/' && i > 0)
690 // look for first / before ..
694 // find / before this /
696 while(new_dir[i] != '/' && i > 0)
698 // look for first / before first / before ..
702 // i now equals /first filename before ..
703 // look for first / after ..
704 while(new_dir[j] != '/' && j < len)
709 // j now equals /first filename after ..
712 new_dir[i++] = new_dir[j++];
716 // default to root directory
717 if((new_dir[0]) == 0) sprintf(new_dir, "/");
725 int FileSystem::complete_path(char *filename)
727 //printf("FileSystem::complete_path 1\n");
728 if(!strlen(filename)) return 1;
729 //printf("FileSystem::complete_path 1\n");
730 parse_tildas(filename);
731 //printf("FileSystem::complete_path 1\n");
732 parse_directories(filename);
733 //printf("FileSystem::complete_path 1\n");
734 parse_dots(filename);
735 // don't add end slash since this requires checking if dir
736 //printf("FileSystem::complete_path 2\n");
740 int FileSystem::extract_dir(char *out, const char *in)
745 // complete string is not directory
750 for(i = strlen(out); i > 0 && out[i - 1] != '/'; i--)
754 if(i >= 0) out[i] = 0;
759 int FileSystem::extract_name(char *out, const char *in, int test_dir)
763 if(test_dir && is_dir(in))
764 out[0] = 0; // complete string is directory
767 for(i = strlen(in)-1; i > 0 && in[i] != '/'; i--)
771 if(in[i] == '/') i++;
777 int FileSystem::join_names(char *out, const char *dir_in, const char *name_in)
780 int len = strlen(out);
784 if(len == 0 || out[len] != 0) result = 1; else len--;
788 if(out[len] != '/') strcat(out, "/");
791 strcat(out, name_in);
795 int64_t FileSystem::get_date(char *filename)
797 struct stat file_status;
798 bzero(&file_status, sizeof(struct stat));
799 int result = stat(filename, &file_status);
800 return !result ? file_status.st_mtime : -1;
803 int64_t FileSystem::get_size(char *filename)
805 struct stat file_status;
806 bzero(&file_status, sizeof(struct stat));
807 int result = stat(filename, &file_status);
808 return !result ? file_status.st_size : -1;
811 int FileSystem::change_dir(const char *new_dir, int update)
813 char new_dir_full[BCTEXTLEN];
815 strcpy(new_dir_full, new_dir);
817 complete_path(new_dir_full);
819 if(strcmp(new_dir_full, "/") &&
820 new_dir_full[strlen(new_dir_full) - 1] == '/')
821 new_dir_full[strlen(new_dir_full) - 1] = 0;
824 this->update(new_dir_full);
827 strcpy(current_dir, new_dir_full);
832 int FileSystem::set_current_dir(const char *new_dir)
834 strcpy(current_dir, new_dir);
838 int FileSystem::add_end_slash(char *new_dir)
840 if(new_dir[strlen(new_dir) - 1] != '/') strcat(new_dir, "/");
844 char* FileSystem::get_current_dir()
849 int FileSystem::total_files()
851 return dir_list.total;
855 FileItem* FileSystem::get_entry(int entry)
857 return dir_list.values[entry];
860 // collapse ".", "..", "//" eg. x/./..//y = y
861 char *FileSystem::basepath(const char *path)
863 char fpath[BCTEXTLEN];
864 unsigned len = strlen(path);
865 if( len >= sizeof(fpath) ) return 0;
867 char *flat = cstrdup("");
870 char *fn = fpath + len;
871 while( fn > fpath ) {
872 while( --fn >= fpath )
873 if( *fn == '/' ) { *fn = 0; break; }
874 fn = fn < fpath ? fpath : fn+1;
875 if( !*fn || !strcmp(fn, ".") ) continue;
876 if( !strcmp(fn, "..") ) { ++r; continue; }
877 if( r < 0 ) continue;
878 if( r > 0 ) { --r; continue; }
879 char *cp = cstrcat(3, "/",fn,flat);
880 delete [] flat; flat = cp;
884 char *cp = cstrcat(2, ".",flat);
885 delete [] flat; flat = cp;