1 /* $Id: file.c,v 1.9 2005-03-05 09:14:59 chris Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 static char rcsid[] = "$Id: file.c,v 1.9 2005-03-05 09:14:59 chris Exp $";
28 #include <sys\types.h>
42 char filename_list[300][13];
43 char directory_list[100][13];
45 int file_sort_func(char *e0, char *e1)
47 return stricmp(e0, e1);
53 int FloppyPresent, FloppyNumber;
56 b = *((unsigned char *)0x410);
58 FloppyPresent = b & 1;
59 FloppyNumber = ((b&0xC0)>>6)+1;
60 if (FloppyPresent && (FloppyNumber==1) )
71 *((unsigned char *)0x504) = 0;
73 *((unsigned char *)0x504) = 1;
78 void file_capitalize( char * s )
80 while( *s++ = toupper(*s) );
84 // Changes to a drive if valid.. 1=A, 2=B, etc
85 // If flag, then changes to it.
86 // Returns 0 if not-valid, 1 if valid.
87 int file_chdrive( int DriveNum, int flag )
89 unsigned NumDrives, n, org;
93 _dos_getdrive( &org );
95 _dos_setdrive( DriveNum, &NumDrives );
101 if ( (!flag) && (n != org) )
102 _dos_setdrive( org, &NumDrives );
108 // Changes to directory in dir. Even drive is changed.
109 // Returns 1 if failed.
111 // 1 = Invalid disk drive.
112 // 2 = Invalid directory.
114 int file_chdir( char * dir )
117 char OriginalDirectory[100];
118 char * Drive, * Path;
121 getcwd( OriginalDirectory, 100 );
123 file_capitalize( dir );
125 Drive = strchr(dir, ':');
129 if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
134 SetFloppy(*(Drive - 1) - 'A' + 1);
147 // This chdir might get a critical error!
151 file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
160 int file_getdirlist( int MaxNum, char list[][13] )
163 int NumDirs = 0, i, CurDrive;
169 if (strlen(cwd) >= 4)
171 sprintf( list[NumDirs++], ".." );
174 CurDrive = cwd[0] - 'A' + 1;
176 if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
178 if ( find.attrib & _A_SUBDIR ) {
179 if (strcmp( "..", find.name) && strcmp( ".", find.name))
180 strncpy(list[NumDirs++], find.name, 13 );
183 while( !_dos_findnext( &find ) )
185 if ( find.attrib & _A_SUBDIR ) {
186 if (strcmp( "..", find.name) && strcmp( ".", find.name))
190 MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
193 strncpy(list[NumDirs++], find.name, 13 );
200 qsort(list, NumDirs, 13, (int (*)( const void *, const void * ))file_sort_func);
202 for (i=1; i<=26; i++ )
204 if (file_chdrive(i,0) && (i!=CurDrive))
206 if (!((i==2) && SingleDrive()))
207 sprintf( list[NumDirs++], "%c:", i+'A'-1 );
214 int file_getfilelist( int MaxNum, char list[][13], char * filespec )
221 if( !_dos_findfirst( filespec, 0, &find ) )
223 //if ( !(find.attrib & _A_SUBDIR) )
224 strncpy(list[NumFiles++], find.name, 13 );
226 while( !_dos_findnext( &find ) )
228 //if ( !(find.attrib & _A_SUBDIR) )
232 MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
235 strncpy(list[NumFiles++], find.name, 13 );
242 qsort(list, NumFiles, 13, (int (*)( const void *, const void * ))file_sort_func);
247 static int FirstTime = 1;
248 static char CurDir[128];
250 int ui_get_filename( char * filename, char * Filespec, char * message )
253 int NumFiles, NumDirs,i;
256 char ErrorMessage[100];
258 UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
259 UI_GADGET_LISTBOX * ListBox1;
260 UI_GADGET_LISTBOX * ListBox2;
261 UI_GADGET_INPUTBOX * UserFile;
264 char drive[ _MAX_DRIVE ];
265 char dir[ _MAX_DIR ];
266 char fname[ _MAX_FNAME ];
267 char ext[ _MAX_EXT ];
268 char fulldir[ _MAX_DIR + _MAX_DRIVE ];
269 char fullfname[ _MAX_FNAME + _MAX_EXT ];
274 getcwd( OrgDir, 128 );
277 getcwd( CurDir, 128 );
280 file_chdir( CurDir );
282 //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
287 NumFiles = file_getfilelist( 300, filename_list, Filespec );
289 NumDirs = file_getdirlist( 100, directory_list );
291 wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
293 ui_wprintf_at( wnd, 10, 5, message );
295 _splitpath( filename, drive, dir, fname, ext );
297 sprintf( InputText, "%s%s", fname, ext );
299 ui_wprintf_at( wnd, 20, 32,"N&ame" );
300 UserFile = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
302 ui_wprintf_at( wnd, 20, 86,"&Files" );
303 ui_wprintf_at( wnd, 210, 86,"&Dirs" );
305 ListBox1 = ui_add_gadget_listbox( wnd, 20, 110, 125, 200, NumFiles, filename_list, 13 );
306 ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list, 13 );
308 Button1 = ui_add_gadget_button( wnd, 20, 330, 60, 25, "Ok", NULL );
309 Button2 = ui_add_gadget_button( wnd, 100, 330, 60, 25, "Cancel", NULL );
310 HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
312 wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
314 Button1->hotkey = KEY_CTRLED + KEY_ENTER;
315 Button2->hotkey = KEY_ESC;
316 HelpButton->hotkey = KEY_F1;
317 ListBox1->hotkey = KEY_ALTED + KEY_F;
318 ListBox2->hotkey = KEY_ALTED + KEY_D;
319 UserFile->hotkey = KEY_ALTED + KEY_A;
321 ui_gadget_calc_keys(wnd);
323 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
324 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
331 ui_window_do_gadgets(wnd);
333 if ( Button2->pressed )
335 file_chdir( OrgDir );
336 ui_close_window(wnd);
340 if ( HelpButton->pressed )
341 MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
343 if (ListBox1->moved || new_listboxes)
345 if (ListBox1->current_item >= 0 )
347 strcpy(UserFile->text, filename_list[ListBox1->current_item] );
348 UserFile->position = strlen(UserFile->text);
349 UserFile->oldposition = UserFile->position;
351 UserFile->first_time = 1;
355 if (ListBox2->moved || new_listboxes)
357 if (ListBox2->current_item >= 0 )
359 if (strrchr( directory_list[ListBox2->current_item], ':' ))
360 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
362 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->current_item], Filespec );
363 UserFile->position = strlen(UserFile->text);
364 UserFile->oldposition = UserFile->position;
366 UserFile->first_time = 1;
371 if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
375 if (ListBox2->selected_item > -1 ) {
376 if (strrchr( directory_list[ListBox2->selected_item], ':' ))
377 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
379 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->selected_item], Filespec );
382 TempFile = fopen( UserFile->text, "r" );
385 // Looks like a valid filename that already exists!
390 // File doesn't exist, but can we create it?
391 TempFile = fopen( UserFile->text, "w" );
394 // Looks like a valid filename!
396 remove( UserFile->text );
400 _splitpath( UserFile->text, drive, dir, fname, ext );
401 sprintf( fullfname, "%s%s", fname, ext );
403 //mprintf( 0, "----------------------------\n" );
404 //mprintf( 0, "Full text: '%s'\n", UserFile->text );
405 //mprintf( 0, "Drive: '%s'\n", drive );
406 //mprintf( 0, "Dir: '%s'\n", dir );
407 //mprintf( 0, "Filename: '%s'\n", fname );
408 //mprintf( 0, "Extension: '%s'\n", ext );
409 //mprintf( 0, "Full dir: '%s'\n", fulldir );
410 //mprintf( 0, "Full fname: '%s'\n", fname );
412 if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )
414 sprintf( fulldir, "%s%s.", drive, dir );
416 sprintf( fullfname, "%s", Filespec );
417 sprintf( fulldir, "%s", UserFile->text );
420 //mprintf( 0, "----------------------------\n" );
421 //mprintf( 0, "Full dir: '%s'\n", fulldir );
422 //mprintf( 0, "Full fname: '%s'\n", fullfname );
424 if (file_chdir( fulldir )==0)
426 NumFiles = file_getfilelist( 300, filename_list, fullfname );
428 strcpy(UserFile->text, fullfname );
429 UserFile->position = strlen(UserFile->text);
430 UserFile->oldposition = UserFile->position;
432 UserFile->first_time = 1;
434 NumDirs = file_getdirlist( 100, directory_list );
436 ui_listbox_change( wnd, ListBox1, NumFiles, filename_list, 13 );
437 ui_listbox_change( wnd, ListBox2, NumDirs, directory_list, 13 );
440 getcwd( CurDir, 35 );
441 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
442 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
445 //while ( TICKER < i+2 );
448 sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
449 MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
450 UserFile->first_time = 1;
463 _splitpath( UserFile->text, drive, dir, fname, ext );
464 sprintf( fulldir, "%s%s.", drive, dir );
465 sprintf( fullfname, "%s%s", fname, ext );
467 if ( strlen(fulldir) > 1 )
468 file_chdir( fulldir );
470 getcwd( CurDir, 35 );
472 if ( strlen(CurDir) > 0 )
474 if ( CurDir[strlen(CurDir)-1] == '\\' )
475 CurDir[strlen(CurDir)-1] = 0;
478 sprintf( filename, "%s\\%s", CurDir, fullfname );
479 //MessageBox( -2, -2, 1, filename, "Ok" );
481 file_chdir( OrgDir );
483 ui_close_window(wnd);
490 int ui_get_file( char * filename, char * Filespec )
495 NumFiles = file_getfilelist( 200, filename_list, Filespec );
497 for (i=0; i< NumFiles; i++ )
499 MALLOC( text[i], char, 15 );
500 strcpy(text[i], filename_list[i] );
503 x = MenuX( -1, -1, NumFiles, text );
506 strcpy(filename, filename_list[x-1] );
508 for (i=0; i< NumFiles; i++ )