Changeset 4378
- Timestamp:
- 09/20/06 17:59:39 (2 years ago)
- Files:
-
- wire/trunk/NEWS (modified) (1 diff)
- wire/trunk/wire/client.c (modified) (4 diffs)
- wire/trunk/wire/files.c (modified) (9 diffs)
- wire/trunk/wire/files.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
wire/trunk/NEWS
r4373 r4378 3 3 4 4 1.1.2 5 - Add globbing, e.g. /get *.mp3 5 6 - Clear the chat action indicator when scrolling to the bottom of a window 6 7 wire/trunk/wire/client.c
r4197 r4378 1083 1083 file->name = wi_retain(wi_string_last_path_component(file->path)); 1084 1084 1085 wi_ list_append_data(wr_files, file);1085 wi_array_add_data(wr_files, file); 1086 1086 wi_release(file); 1087 1087 } … … 1090 1090 1091 1091 static void wr_msg_411(wi_array_t *arguments) { 1092 wi_list_node_t *node;1093 1092 wr_file_t *file; 1094 1093 wi_file_offset_t free; 1095 u nsigned intmax_length = 0;1094 uint32_t i, count, max_length = 0; 1096 1095 1097 1096 if(wr_ls_state == WR_LS_LISTING) { 1098 1097 free = wi_string_uint64(WI_ARRAY(arguments, 1)); 1098 count = wi_array_count(wr_files); 1099 1099 1100 1100 wr_printf_prefix(WI_STR("Listing of %@ (%@ available):"), wr_files_ld, wr_files_string_for_size(free)); 1101 1101 1102 if( wi_list_count(wr_files)== 0) {1102 if(count == 0) { 1103 1103 wr_printf_block(WI_STR("(empty)")); 1104 1104 } else { 1105 WI_LIST_FOREACH(wr_files, node, file) 1105 for(i = 0; i < count; i++) { 1106 file = WI_ARRAY(wr_files, i); 1106 1107 max_length = WI_MAX(max_length, wi_string_length(file->name)); 1107 1108 WI_LIST_FOREACH(wr_files, node, file) 1108 } 1109 1110 for(i = 0; i < count; i++) { 1111 file = WI_ARRAY(wr_files, i); 1109 1112 wr_print_file(file, false, max_length); 1113 } 1110 1114 } 1111 1115 } … … 1123 1127 file->name = wi_retain(wi_string_last_path_component(file->path)); 1124 1128 1125 wi_ list_append_data(wr_files, file);1129 wi_array_add_data(wr_files, file); 1126 1130 wi_release(file); 1127 1131 } … … 1130 1134 1131 1135 static void wr_msg_421(wi_array_t *arguments) { 1132 wi_list_node_t *node;1133 1136 wr_file_t *file; 1134 unsigned int max_length = 0; 1137 uint32_t i, count, max_length = 0; 1138 1139 count = wi_array_count(wr_files); 1135 1140 1136 1141 wr_printf_prefix(WI_STR("Search results:")); 1137 1142 1138 if( wi_list_count(wr_files)== 0) {1143 if(count == 0) { 1139 1144 wr_printf_block(WI_STR("(none)")); 1140 1145 } else { 1141 WI_LIST_FOREACH(wr_files, node, file) 1146 for(i = 0; i < count; i++) { 1147 file = WI_ARRAY(wr_files, i); 1142 1148 max_length = WI_MAX(max_length, wi_string_length(file->name)); 1143 1144 WI_LIST_FOREACH(wr_files, node, file) 1145 wr_print_file(file, true, max_length); 1146 } 1147 } 1149 } 1150 1151 for(i = 0; i < count; i++) { 1152 file = WI_ARRAY(wr_files, i); 1153 wr_print_file(file, false, max_length); 1154 } 1155 } 1156 } wire/trunk/wire/files.c
r4377 r4378 29 29 #include "config.h" 30 30 31 #include <sys/stat.h> 31 32 #include <stdio.h> 32 33 #include <stdlib.h> 34 #include <dirent.h> 35 #include <glob.h> 33 36 #include <readline/readline.h> 34 37 #include <wired/wired.h> … … 38 41 #include "main.h" 39 42 43 #include "windows.h" 44 45 struct _wr_files_glob_dir { 46 wi_string_t *path; 47 struct dirent **entries; 48 uint32_t count, offset; 49 }; 50 51 typedef struct _wr_files_glob_dir wr_files_glob_dir_t; 52 53 54 static wi_array_t * wr_files_glob(wi_string_t *); 55 static void * wr_files_glob_opendir(const char *); 56 static struct dirent * wr_files_glob_readdir(void *); 57 static void wr_files_glob_closedir(void *); 58 static int wr_files_glob_stat(const char *, struct stat *); 59 40 60 static void wr_file_dealloc(wi_runtime_instance_t *); 41 61 static wi_string_t * wr_file_description(wi_runtime_instance_t *); … … 43 63 wi_string_t * wr_files_cwd; 44 64 wi_string_t * wr_files_ld; 45 wi_ list_t *wr_files;65 wi_array_t *wr_files; 46 66 47 67 wr_ls_state_t wr_ls_state; … … 62 82 wr_file_runtime_id = wi_runtime_register_class(&wr_file_runtime_class); 63 83 64 wr_files = wi_ list_init(wi_list_alloc());84 wr_files = wi_array_init(wi_array_alloc()); 65 85 66 86 wr_files_cwd = wi_string_init_with_cstring(wi_string_alloc(), "/"); … … 70 90 71 91 void wr_clear_files(void) { 72 wi_ list_remove_all_data(wr_files);92 wi_array_remove_all_data(wr_files); 73 93 74 94 wi_release(wr_files_ld); … … 81 101 82 102 char * wr_readline_filename_generator(const char *text, int state) { 83 static wi_list_node_t *node;84 103 static wi_string_t *directory_path; 104 static uint32_t index; 85 105 wi_string_t *path, *full_path, *name; 86 106 wr_file_t *file; 87 107 char *cname; 88 108 wi_boolean_t root; 109 uint32_t count; 89 110 90 111 cname = ((*rl_filename_dequoting_function) ((char *) text, 0)); … … 97 118 directory_path = wi_retain(wi_string_by_deleting_last_path_component(path)); 98 119 full_path = wr_files_full_path(directory_path); 120 index = 0; 99 121 100 122 wr_clear_files(); 101 123 wr_send_command(WI_STR("LIST %@"), full_path); 102 124 wr_runloop_run_for_socket(wr_socket, 3.0, 411); 103 104 node = wi_list_first_node(wr_files);105 125 } 106 126 107 127 name = wi_string_last_path_component(path); 108 128 root = wi_is_equal(name, WI_STR("/")); 109 110 while(node) { 111 file = wi_list_node_data(node);112 node = wi_list_node_next_node(node);129 count = wi_array_count(wr_files); 130 131 while(index < count) { 132 file = WI_ARRAY(wr_files, index++); 113 133 114 134 if(file->type == WR_FILE_FILE && wr_ls_state == WR_LS_COMPLETING_DIRECTORY) … … 147 167 148 168 wi_array_t * wr_files_full_paths(wi_array_t *paths) { 149 wi_array_t *fullpaths ;150 uint32_t i, count ;169 wi_array_t *fullpaths, *globpaths; 170 uint32_t i, count, j, globcount; 151 171 152 172 count = wi_array_count(paths); 153 173 fullpaths = wi_array_init_with_capacity(wi_array_alloc(), count); 154 174 155 for(i = 0; i < count; i++) 156 wi_array_add_data(fullpaths, wr_files_full_path(WI_ARRAY(paths, i))); 175 for(i = 0; i < count; i++) { 176 globpaths = wr_files_glob(WI_ARRAY(paths, i)); 177 globcount = wi_array_count(globpaths); 178 179 for(j = 0; j < globcount; j++) 180 wi_array_add_data(fullpaths, wr_files_full_path(WI_ARRAY(globpaths, j))); 181 } 157 182 158 183 return wi_autorelease(fullpaths); … … 214 239 #pragma mark - 215 240 241 static wi_array_t * wr_files_glob(wi_string_t *pattern) { 242 wi_array_t *array; 243 glob_t gl; 244 uint32_t i; 245 246 gl.gl_opendir = wr_files_glob_opendir; 247 gl.gl_readdir = wr_files_glob_readdir; 248 gl.gl_closedir = wr_files_glob_closedir; 249 gl.gl_lstat = wr_files_glob_stat; 250 gl.gl_stat = wr_files_glob_stat; 251 252 if(glob(wi_string_cstring(pattern), GLOB_ALTDIRFUNC, NULL, &gl) != 0) { 253 wr_printf_prefix(WI_STR("glob: %m")); 254 255 return NULL; 256 } 257 258 array = wi_array_init_with_capacity(wi_array_alloc(), gl.gl_pathc); 259 260 for(i = 0; i < gl.gl_pathc; i++) 261 wi_array_add_data(array, wi_string_with_cstring(gl.gl_pathv[i])); 262 263 globfree(&gl); 264 265 return wi_autorelease(array); 266 } 267 268 269 270 static void * wr_files_glob_opendir(const char *path) { 271 wr_files_glob_dir_t *dir; 272 wr_file_t *file; 273 uint32_t i; 274 275 dir = wi_malloc(sizeof(wr_files_glob_dir_t)); 276 dir->path = wi_retain(wr_files_full_path(wi_string_with_cstring(path))); 277 278 wr_ls_state = WR_LS_GLOBBING; 279 wr_clear_files(); 280 wr_send_command(WI_STR("LIST %@"), dir->path); 281 wr_runloop_run_for_socket(wr_socket, 3.0, 411); 282 283 dir->count = wi_array_count(wr_files); 284 dir->entries = wi_malloc(sizeof(struct dirent) * (dir->count + 1)); 285 286 for(i = 0; i < dir->count; i++) { 287 file = WI_ARRAY(wr_files, i); 288 289 dir->entries[i] = wi_malloc(sizeof(struct dirent)); 290 dir->entries[i]->d_ino = 0; 291 dir->entries[i]->d_reclen = 0; 292 dir->entries[i]->d_type = (file->type == WR_FILE_FILE) ? DT_REG : DT_DIR; 293 dir->entries[i]->d_namlen = wi_string_length(file->name); 294 295 strlcpy(dir->entries[i]->d_name, wi_string_cstring(file->name), sizeof(dir->entries[i]->d_name)); 296 } 297 298 return dir; 299 } 300 301 302 303 static struct dirent * wr_files_glob_readdir(void *p) { 304 wr_files_glob_dir_t *dir = p; 305 306 return dir->entries[dir->offset++]; 307 } 308 309 310 311 static void wr_files_glob_closedir(void *p) { 312 wr_files_glob_dir_t *dir = p; 313 uint32_t i; 314 315 wi_release(dir->path); 316 317 for(i = 0; i < dir->count; i++) 318 wi_free(dir->entries[i]); 319 320 wi_free(dir->entries); 321 wi_free(dir); 322 } 323 324 325 326 static int wr_files_glob_stat(const char *path, struct stat *sp) { 327 memset(sp, 0, sizeof(struct stat)); 328 329 return 0; 330 } 331 332 333 334 #pragma mark - 335 216 336 wr_file_t * wr_file_alloc(void) { 217 337 return wi_runtime_create_instance(wr_file_runtime_id, sizeof(wr_file_t)); wire/trunk/wire/files.h
r4377 r4378 55 55 WR_LS_LISTING, 56 56 WR_LS_COMPLETING, 57 WR_LS_COMPLETING_DIRECTORY 57 WR_LS_COMPLETING_DIRECTORY, 58 WR_LS_GLOBBING 58 59 }; 59 60 typedef enum _wr_ls_state wr_ls_state_t; … … 84 85 extern wi_string_t *wr_files_cwd; 85 86 extern wi_string_t *wr_files_ld; 86 extern wi_ list_t *wr_files;87 extern wi_array_t *wr_files; 87 88 88 89 extern wr_ls_state_t wr_ls_state;
