Deleted Added
full compact
fselect.c (8548) fselect.c (8858)
1/*
2 * program: fselect.c
3 * author: Marc van Kempen (wmbfmk@urc.tue.nl)
1/*
2 * program: fselect.c
3 * author: Marc van Kempen (wmbfmk@urc.tue.nl)
4 * Desc: File selection routine
4 * Desc: File selection routine
5 *
6 * Copyright (c) 1995, Marc van Kempen
7 *
8 * All rights reserved.
9 *
10 * This software may be used, modified, copied, distributed, and
11 * sold, in both source and binary form provided that the above
12 * copyright and these terms are retained, verbatim, as the first
13 * lines of this file. Under no circumstances is the author
14 * responsible for the proper functioning of this software, nor does
15 * the author assume any responsibility for damages incurred with
16 * its use.
5 *
6 * Copyright (c) 1995, Marc van Kempen
7 *
8 * All rights reserved.
9 *
10 * This software may be used, modified, copied, distributed, and
11 * sold, in both source and binary form provided that the above
12 * copyright and these terms are retained, verbatim, as the first
13 * lines of this file. Under no circumstances is the author
14 * responsible for the proper functioning of this software, nor does
15 * the author assume any responsibility for damages incurred with
16 * its use.
17 *
17 *
18 */
19
20#include <stdlib.h>
21#include <unistd.h>
22#include <sys/param.h>
23#include <dialog.h>
24#include "ui_objects.h"
25#include "dir.h"
26#include "dialog.priv.h"
27
28/*
29 * Local prototypes
30 */
31
32char *dialog_dfselect(char *dir, char *fmask, int is_fselect);
33
18 */
19
20#include <stdlib.h>
21#include <unistd.h>
22#include <sys/param.h>
23#include <dialog.h>
24#include "ui_objects.h"
25#include "dir.h"
26#include "dialog.priv.h"
27
28/*
29 * Local prototypes
30 */
31
32char *dialog_dfselect(char *dir, char *fmask, int is_fselect);
33
34/*
34/*
35 * Functions
36 */
37
38void
39get_directories(DirList *d, int n, char ***names, int *nd)
40/*
41 * Desc: return the directorienames in <dir> as an array in
42 * <names>, the # of entries in <nd>, memory allocated

--- 12 unchanged lines hidden (view full) ---

55 }
56
57 return;
58} /* get_directories() */
59
60void
61get_filenames(DirList *d, int n, char ***names, int *nf)
62/*
35 * Functions
36 */
37
38void
39get_directories(DirList *d, int n, char ***names, int *nd)
40/*
41 * Desc: return the directorienames in <dir> as an array in
42 * <names>, the # of entries in <nd>, memory allocated

--- 12 unchanged lines hidden (view full) ---

55 }
56
57 return;
58} /* get_directories() */
59
60void
61get_filenames(DirList *d, int n, char ***names, int *nf)
62/*
63 * Desc: return the filenames in <dir> as an arry in
63 * Desc: return the filenames in as an arry in
64 * <names>, the # of entries in <nf>, memory allocated
65 * to *names should be freed when done.
66 */
67{
68 int nd, i;
69
70 /* the # of regular files is the total # of files - # of directories */
71 /* count the # of directories */
72 nd = 0;
73 while ((nd < n) && (S_ISDIR(d[nd].filestatus.st_mode))) nd++;
74
75 *names = (char **) malloc( (n-nd) * sizeof(char *) );
76 *nf = n - nd;
77 for (i=0; i<*nf; i++) {
78 (*names)[i] = (char *) malloc( strlen(d[i+nd].filename) + 1);
79 strcpy((*names)[i], d[i+nd].filename);
80 }
64 * <names>, the # of entries in <nf>, memory allocated
65 * to *names should be freed when done.
66 */
67{
68 int nd, i;
69
70 /* the # of regular files is the total # of files - # of directories */
71 /* count the # of directories */
72 nd = 0;
73 while ((nd < n) && (S_ISDIR(d[nd].filestatus.st_mode))) nd++;
74
75 *names = (char **) malloc( (n-nd) * sizeof(char *) );
76 *nf = n - nd;
77 for (i=0; i<*nf; i++) {
78 (*names)[i] = (char *) malloc( strlen(d[i+nd].filename) + 1);
79 strcpy((*names)[i], d[i+nd].filename);
80 }
81
81
82 return;
83} /* get_filenames() */
84
85void
86FreeNames(char **names, int n)
87/*
88 * Desc: free the space occupied by names
89 */

--- 7 unchanged lines hidden (view full) ---

97 free(names);
98
99 return;
100} /* FreeNames() */
101
102int
103dialog_dselect_old(void)
104/*
82 return;
83} /* get_filenames() */
84
85void
86FreeNames(char **names, int n)
87/*
88 * Desc: free the space occupied by names
89 */

--- 7 unchanged lines hidden (view full) ---

97 free(names);
98
99 return;
100} /* FreeNames() */
101
102int
103dialog_dselect_old(void)
104/*
105 * Desc: starting from the current directory,
106 * choose a new current directory
105 * Desc: starting from the current directory,
106 * choose a new current directory
107 */
108{
109 DirList *d = NULL;
110 char **names, old_dir[MAXPATHLEN];
111 WINDOW *ds_win;
112 ButtonObj *okbut, *cancelbut;
113 ListObj *dirs_obj;
114 StringObj *dir_obj;
115 char o_dir[MAXPATHLEN];
116 struct ComposeObj *obj = NULL;
107 */
108{
109 DirList *d = NULL;
110 char **names, old_dir[MAXPATHLEN];
111 WINDOW *ds_win;
112 ButtonObj *okbut, *cancelbut;
113 ListObj *dirs_obj;
114 StringObj *dir_obj;
115 char o_dir[MAXPATHLEN];
116 struct ComposeObj *obj = NULL;
117 int n, nd, okbutton, cancelbutton,
117 int n, nd, okbutton, cancelbutton,
118 quit, cancel, ret;
119
120 ds_win = newwin(LINES-8, COLS-30, 4, 15);
121 if (ds_win == NULL) {
118 quit, cancel, ret;
119
120 ds_win = newwin(LINES-8, COLS-30, 4, 15);
121 if (ds_win == NULL) {
122 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
122 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
123 LINES-8, COLS-30, 4, 15);
124 exit(1);
125 }
126 draw_box(ds_win, 0, 0, LINES-8, COLS-30, dialog_attr, border_attr);
127 wattrset(ds_win, dialog_attr);
128 mvwaddstr(ds_win, 0, (COLS-30)/2 - 9, " Directory Select ");
129 draw_shadow(stdscr, 4, 15, LINES-8, COLS-30);
130 display_helpline(ds_win, LINES-9, COLS-30);
131
132 /* the Directory string input field */
133 getcwd(o_dir, MAXPATHLEN);
134 dir_obj = NewStringObj(ds_win, "Directory:", o_dir, 1, 2, COLS-34, MAXPATHLEN-1);
135 AddObj(&obj, STRINGOBJ, (void *) dir_obj);
136
137 /* the list of directories */
138 get_dir(".", "*", &d, &n);
139 get_directories(d, n, &names, &nd);
123 LINES-8, COLS-30, 4, 15);
124 exit(1);
125 }
126 draw_box(ds_win, 0, 0, LINES-8, COLS-30, dialog_attr, border_attr);
127 wattrset(ds_win, dialog_attr);
128 mvwaddstr(ds_win, 0, (COLS-30)/2 - 9, " Directory Select ");
129 draw_shadow(stdscr, 4, 15, LINES-8, COLS-30);
130 display_helpline(ds_win, LINES-9, COLS-30);
131
132 /* the Directory string input field */
133 getcwd(o_dir, MAXPATHLEN);
134 dir_obj = NewStringObj(ds_win, "Directory:", o_dir, 1, 2, COLS-34, MAXPATHLEN-1);
135 AddObj(&obj, STRINGOBJ, (void *) dir_obj);
136
137 /* the list of directories */
138 get_dir(".", "*", &d, &n);
139 get_directories(d, n, &names, &nd);
140 dirs_obj = NewListObj(ds_win, "Directories:", names, o_dir, 5, 2,
140 dirs_obj = NewListObj(ds_win, "Directories:", names, o_dir, 5, 2,
141 LINES-15, COLS-48, nd);
142 AddObj(&obj, LISTOBJ, (void *) dirs_obj);
143
144 /* the Ok-button */
145 okbutton = FALSE;
146 okbut = NewButtonObj(ds_win, "Continue", &okbutton, 7, COLS-45);
147 AddObj(&obj, BUTTONOBJ, (void *) okbut);
141 LINES-15, COLS-48, nd);
142 AddObj(&obj, LISTOBJ, (void *) dirs_obj);
143
144 /* the Ok-button */
145 okbutton = FALSE;
146 okbut = NewButtonObj(ds_win, "Continue", &okbutton, 7, COLS-45);
147 AddObj(&obj, BUTTONOBJ, (void *) okbut);
148
148
149 /* the Cancel-button */
150 cancelbutton = FALSE;
151 cancelbut = NewButtonObj(ds_win, "Return", &cancelbutton, 11, COLS-44);
152 AddObj(&obj, BUTTONOBJ, (void *) cancelbut);
153
154 quit = FALSE;
155 cancel = FALSE;
156 strcpy(old_dir, o_dir);

--- 14 unchanged lines hidden (view full) ---

171 /* the directory was changed, cd into it */
172 if (chdir(o_dir)) {
173 dialog_notify("Could not change into directory");
174 } else {
175 getcwd(o_dir, MAXPATHLEN);
176 strcpy(old_dir, o_dir);
177 RefreshStringObj(dir_obj);
178 }
149 /* the Cancel-button */
150 cancelbutton = FALSE;
151 cancelbut = NewButtonObj(ds_win, "Return", &cancelbutton, 11, COLS-44);
152 AddObj(&obj, BUTTONOBJ, (void *) cancelbut);
153
154 quit = FALSE;
155 cancel = FALSE;
156 strcpy(old_dir, o_dir);

--- 14 unchanged lines hidden (view full) ---

171 /* the directory was changed, cd into it */
172 if (chdir(o_dir)) {
173 dialog_notify("Could not change into directory");
174 } else {
175 getcwd(o_dir, MAXPATHLEN);
176 strcpy(old_dir, o_dir);
177 RefreshStringObj(dir_obj);
178 }
179 }
179 }
180 get_dir(".", "*", &d, &n);
181 FreeNames(names, nd);
182 get_directories(d, n, &names, &nd);
183 UpdateListObj(dirs_obj, names, nd);
184 if (((obj->prev)->obj == (void *) dirs_obj)) {
185 obj=obj->prev;
186 }
187 break;
188 case SEL_ESC:
189 quit = TRUE;
190 cancel = TRUE;
191 break;
192 case KEY_F(1):
193 display_helpfile();
194 break;
195 }
196 }
180 get_dir(".", "*", &d, &n);
181 FreeNames(names, nd);
182 get_directories(d, n, &names, &nd);
183 UpdateListObj(dirs_obj, names, nd);
184 if (((obj->prev)->obj == (void *) dirs_obj)) {
185 obj=obj->prev;
186 }
187 break;
188 case SEL_ESC:
189 quit = TRUE;
190 cancel = TRUE;
191 break;
192 case KEY_F(1):
193 display_helpfile();
194 break;
195 }
196 }
197
197
198 FreeNames(names, nd);
199 DelObj(obj);
200 delwin(ds_win);
201
202 return(cancel);
203
204} /* dialog_dselect() */
205

--- 18 unchanged lines hidden (view full) ---

224{
225 return(dialog_dfselect(dir, fmask, TRUE));
226} /* dialog_fselect() */
227
228char *
229dialog_dfselect(char *dir, char *fmask, int is_fselect)
230/*
231 * Desc: choose a file from the directory <dir>, which
198 FreeNames(names, nd);
199 DelObj(obj);
200 delwin(ds_win);
201
202 return(cancel);
203
204} /* dialog_dselect() */
205

--- 18 unchanged lines hidden (view full) ---

224{
225 return(dialog_dfselect(dir, fmask, TRUE));
226} /* dialog_fselect() */
227
228char *
229dialog_dfselect(char *dir, char *fmask, int is_fselect)
230/*
231 * Desc: choose a file from the directory <dir>, which
232 * initially display files with the mask <filemask>
232 * initially display files with the mask
233 * pre: <dir> is the initial directory
234 * only files corresponding to the mask <fmask> are displayed
235 * post: returns NULL if no file was selected
236 * else returns pointer to filename, space is allocated, should
237 * be freed after use.
238 */
239{
240 DirList *d = NULL;

--- 16 unchanged lines hidden (view full) ---

257 return(NULL);
258 }
259 getcwd(o_dir, MAXPATHLEN);
260
261 /* setup the fileselect-window and initialize its components */
262 fs_win = newwin(LINES-2, COLS-20, 1, 10);
263 if (fs_win == NULL) {
264 endwin();
233 * pre: <dir> is the initial directory
234 * only files corresponding to the mask <fmask> are displayed
235 * post: returns NULL if no file was selected
236 * else returns pointer to filename, space is allocated, should
237 * be freed after use.
238 */
239{
240 DirList *d = NULL;

--- 16 unchanged lines hidden (view full) ---

257 return(NULL);
258 }
259 getcwd(o_dir, MAXPATHLEN);
260
261 /* setup the fileselect-window and initialize its components */
262 fs_win = newwin(LINES-2, COLS-20, 1, 10);
263 if (fs_win == NULL) {
264 endwin();
265 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
265 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
266 LINES-2, COLS-20, 2, 10);
267 exit(1);
268 }
269 draw_box(fs_win, 0, 0, LINES-2, COLS-20, dialog_attr, border_attr);
270 wattrset(fs_win, dialog_attr);
271 if (is_fselect) {
272 mvwaddstr(fs_win, 0, (COLS-20)/2 - 7, " File Select ");
273 } else {

--- 10 unchanged lines hidden (view full) ---

284 /* Directory entry */
285 dir_obj = NewStringObj(fs_win, "Directory:", o_dir, 1, 22, COLS-44, 255);
286 AddObj(&obj, STRINGOBJ, (void *) dir_obj);
287
288 /* Directory list */
289 get_dir(".", fmask, &d, &n); /* read the entire directory */
290 get_directories(d, n, &dnames, &nd); /* extract the dir-entries */
291 if (is_fselect) {
266 LINES-2, COLS-20, 2, 10);
267 exit(1);
268 }
269 draw_box(fs_win, 0, 0, LINES-2, COLS-20, dialog_attr, border_attr);
270 wattrset(fs_win, dialog_attr);
271 if (is_fselect) {
272 mvwaddstr(fs_win, 0, (COLS-20)/2 - 7, " File Select ");
273 } else {

--- 10 unchanged lines hidden (view full) ---

284 /* Directory entry */
285 dir_obj = NewStringObj(fs_win, "Directory:", o_dir, 1, 22, COLS-44, 255);
286 AddObj(&obj, STRINGOBJ, (void *) dir_obj);
287
288 /* Directory list */
289 get_dir(".", fmask, &d, &n); /* read the entire directory */
290 get_directories(d, n, &dnames, &nd); /* extract the dir-entries */
291 if (is_fselect) {
292 dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
292 dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
293 LINES-16, (COLS-20)/2-2, nd);
294 } else {
295 dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
296 LINES-12, (COLS-20)/2-2, nd);
297 }
298 AddObj(&obj, LISTOBJ, (void *) dirs_obj);
299
300 /* Filenames list */

--- 34 unchanged lines hidden (view full) ---

335 strcpy(old_dir, o_dir);
336 quit = FALSE;
337 cancel = FALSE;
338 while (!quit) {
339 ret = PollObj(&o);
340 switch(ret) {
341 case SEL_CR:
342 if (strcmp(old_fmask, o_fm) || strcmp(old_dir, o_dir)) {
293 LINES-16, (COLS-20)/2-2, nd);
294 } else {
295 dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
296 LINES-12, (COLS-20)/2-2, nd);
297 }
298 AddObj(&obj, LISTOBJ, (void *) dirs_obj);
299
300 /* Filenames list */

--- 34 unchanged lines hidden (view full) ---

335 strcpy(old_dir, o_dir);
336 quit = FALSE;
337 cancel = FALSE;
338 while (!quit) {
339 ret = PollObj(&o);
340 switch(ret) {
341 case SEL_CR:
342 if (strcmp(old_fmask, o_fm) || strcmp(old_dir, o_dir)) {
343 /* reread directory and update the listobjects */
343 /* reread directory and update the listobjects */
344 if (strcmp(old_dir, o_dir)) { /* dir entry was changed */
345 if (chdir(o_dir)) {
346 dialog_notify("Could not change into directory");
347 } else {
348 getcwd(o_dir, MAXPATHLEN);
349 strcpy(old_dir, o_dir);
350 RefreshStringObj(dir_obj);
351 }

--- 49 unchanged lines hidden ---
344 if (strcmp(old_dir, o_dir)) { /* dir entry was changed */
345 if (chdir(o_dir)) {
346 dialog_notify("Could not change into directory");
347 } else {
348 getcwd(o_dir, MAXPATHLEN);
349 strcpy(old_dir, o_dir);
350 RefreshStringObj(dir_obj);
351 }

--- 49 unchanged lines hidden ---