1 /* $Id: file.c,v 1.5 2005-01-25 19:44:27 schaffner 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.5 2005-01-25 19:44:27 schaffner Exp $";
28 #include <sys\types.h>
41 char filename_list[300][13];
42 char directory_list[100][13];
44 static char *Message[] = {
45 "Disk is write protected",
50 "Bad drive-request stuct length",
54 "Printer out of paper",
59 static int error_mode = 0;
61 int __far critical_error_handler( unsigned deverr, unsigned errcode, unsigned far * devhdr )
65 devhdr = devhdr; deverr = deverr;
67 if (error_mode==1) return _HARDERR_FAIL;
69 x = MessageBox( -2, -2, 2, Message[errcode], "Retry", "Fail" );
73 case 1: return _HARDERR_RETRY;
74 case 2: return _HARDERR_FAIL;
75 default: return _HARDERR_FAIL;
79 void InstallErrorHandler()
81 _harderr( critical_error_handler );
84 void file_sort( int n, char list[][13] )
92 for (i=incr; i<n; i++ )
97 if (strncmp(list[j], list[j+incr], 12) > 0)
99 memcpy( t, list[j], 13 );
100 memcpy( list[j], list[j+incr], 13 );
101 memcpy( list[j+incr], t, 13 );
115 int FloppyPresent, FloppyNumber;
118 b = *((unsigned char *)0x410);
120 FloppyPresent = b & 1;
121 FloppyNumber = ((b&0xC0)>>6)+1;
122 if (FloppyPresent && (FloppyNumber==1) )
128 void SetFloppy(int d)
133 *((unsigned char *)0x504) = 0;
135 *((unsigned char *)0x504) = 1;
140 void file_capitalize( char * s )
142 while( *s++ = toupper(*s) );
146 // Changes to a drive if valid.. 1=A, 2=B, etc
147 // If flag, then changes to it.
148 // Returns 0 if not-valid, 1 if valid.
149 int file_chdrive( int DriveNum, int flag )
151 unsigned NumDrives, n, org;
155 _dos_getdrive( &org );
157 _dos_setdrive( DriveNum, &NumDrives );
163 if ( (!flag) && (n != org) )
164 _dos_setdrive( org, &NumDrives );
170 // Changes to directory in dir. Even drive is changed.
171 // Returns 1 if failed.
173 // 1 = Invalid disk drive.
174 // 2 = Invalid directory.
176 int file_chdir( char * dir )
179 char OriginalDirectory[100];
180 char * Drive, * Path;
183 getcwd( OriginalDirectory, 100 );
185 file_capitalize( dir );
187 Drive = strchr(dir, ':');
191 if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
196 SetFloppy(*(Drive - 1) - 'A' + 1);
209 // This chdir might get a critical error!
213 file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
222 int file_getdirlist( int MaxNum, char list[][13] )
225 int NumDirs = 0, i, CurDrive;
231 if (strlen(cwd) >= 4)
233 sprintf( list[NumDirs++], ".." );
236 CurDrive = cwd[0] - 'A' + 1;
238 if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
240 if ( find.attrib & _A_SUBDIR ) {
241 if (strcmp( "..", find.name) && strcmp( ".", find.name))
242 strncpy(list[NumDirs++], find.name, 13 );
245 while( !_dos_findnext( &find ) )
247 if ( find.attrib & _A_SUBDIR ) {
248 if (strcmp( "..", find.name) && strcmp( ".", find.name))
252 MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
255 strncpy(list[NumDirs++], find.name, 13 );
262 file_sort( NumDirs, list );
264 for (i=1; i<=26; i++ )
266 if (file_chdrive(i,0) && (i!=CurDrive))
268 if (!((i==2) && SingleDrive()))
269 sprintf( list[NumDirs++], "%c:", i+'A'-1 );
276 int file_getfilelist( int MaxNum, char list[][13], char * filespec )
283 if( !_dos_findfirst( filespec, 0, &find ) )
285 //if ( !(find.attrib & _A_SUBDIR) )
286 strncpy(list[NumFiles++], find.name, 13 );
288 while( !_dos_findnext( &find ) )
290 //if ( !(find.attrib & _A_SUBDIR) )
294 MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
297 strncpy(list[NumFiles++], find.name, 13 );
304 file_sort( NumFiles, list );
309 static int FirstTime = 1;
310 static char CurDir[128];
312 int ui_get_filename( char * filename, char * Filespec, char * message )
315 int NumFiles, NumDirs,i;
318 char ErrorMessage[100];
320 UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
321 UI_GADGET_LISTBOX * ListBox1;
322 UI_GADGET_LISTBOX * ListBox2;
323 UI_GADGET_INPUTBOX * UserFile;
326 char drive[ _MAX_DRIVE ];
327 char dir[ _MAX_DIR ];
328 char fname[ _MAX_FNAME ];
329 char ext[ _MAX_EXT ];
330 char fulldir[ _MAX_DIR + _MAX_DRIVE ];
331 char fullfname[ _MAX_FNAME + _MAX_EXT ];
336 getcwd( OrgDir, 128 );
339 getcwd( CurDir, 128 );
342 file_chdir( CurDir );
344 //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
349 NumFiles = file_getfilelist( 300, filename_list, Filespec );
351 NumDirs = file_getdirlist( 100, directory_list );
353 wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
355 ui_wprintf_at( wnd, 10, 5, message );
357 _splitpath( filename, drive, dir, fname, ext );
359 sprintf( InputText, "%s%s", fname, ext );
361 ui_wprintf_at( wnd, 20, 32,"N&ame" );
362 UserFile = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
364 ui_wprintf_at( wnd, 20, 86,"&Files" );
365 ui_wprintf_at( wnd, 210, 86,"&Dirs" );
367 ListBox1 = ui_add_gadget_listbox( wnd, 20, 110, 125, 200, NumFiles, filename_list, 13 );
368 ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list, 13 );
370 Button1 = ui_add_gadget_button( wnd, 20, 330, 60, 25, "Ok", NULL );
371 Button2 = ui_add_gadget_button( wnd, 100, 330, 60, 25, "Cancel", NULL );
372 HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
374 wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
376 Button1->hotkey = KEY_CTRLED + KEY_ENTER;
377 Button2->hotkey = KEY_ESC;
378 HelpButton->hotkey = KEY_F1;
379 ListBox1->hotkey = KEY_ALTED + KEY_F;
380 ListBox2->hotkey = KEY_ALTED + KEY_D;
381 UserFile->hotkey = KEY_ALTED + KEY_A;
383 ui_gadget_calc_keys(wnd);
385 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
386 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
393 ui_window_do_gadgets(wnd);
395 if ( Button2->pressed )
397 file_chdir( OrgDir );
398 ui_close_window(wnd);
402 if ( HelpButton->pressed )
403 MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
405 if (ListBox1->moved || new_listboxes)
407 if (ListBox1->current_item >= 0 )
409 strcpy(UserFile->text, filename_list[ListBox1->current_item] );
410 UserFile->position = strlen(UserFile->text);
411 UserFile->oldposition = UserFile->position;
413 UserFile->first_time = 1;
417 if (ListBox2->moved || new_listboxes)
419 if (ListBox2->current_item >= 0 )
421 if (strrchr( directory_list[ListBox2->current_item], ':' ))
422 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
424 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->current_item], Filespec );
425 UserFile->position = strlen(UserFile->text);
426 UserFile->oldposition = UserFile->position;
428 UserFile->first_time = 1;
433 if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
437 if (ListBox2->selected_item > -1 ) {
438 if (strrchr( directory_list[ListBox2->selected_item], ':' ))
439 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
441 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->selected_item], Filespec );
444 error_mode = 1; // Critical error handler automatically fails.
446 TempFile = fopen( UserFile->text, "r" );
449 // Looks like a valid filename that already exists!
454 // File doesn't exist, but can we create it?
455 TempFile = fopen( UserFile->text, "w" );
458 // Looks like a valid filename!
460 remove( UserFile->text );
464 _splitpath( UserFile->text, drive, dir, fname, ext );
465 sprintf( fullfname, "%s%s", fname, ext );
467 //mprintf( 0, "----------------------------\n" );
468 //mprintf( 0, "Full text: '%s'\n", UserFile->text );
469 //mprintf( 0, "Drive: '%s'\n", drive );
470 //mprintf( 0, "Dir: '%s'\n", dir );
471 //mprintf( 0, "Filename: '%s'\n", fname );
472 //mprintf( 0, "Extension: '%s'\n", ext );
473 //mprintf( 0, "Full dir: '%s'\n", fulldir );
474 //mprintf( 0, "Full fname: '%s'\n", fname );
476 if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )
478 sprintf( fulldir, "%s%s.", drive, dir );
480 sprintf( fullfname, "%s", Filespec );
481 sprintf( fulldir, "%s", UserFile->text );
484 //mprintf( 0, "----------------------------\n" );
485 //mprintf( 0, "Full dir: '%s'\n", fulldir );
486 //mprintf( 0, "Full fname: '%s'\n", fullfname );
488 if (file_chdir( fulldir )==0)
490 NumFiles = file_getfilelist( 300, filename_list, fullfname );
492 strcpy(UserFile->text, fullfname );
493 UserFile->position = strlen(UserFile->text);
494 UserFile->oldposition = UserFile->position;
496 UserFile->first_time = 1;
498 NumDirs = file_getdirlist( 100, directory_list );
500 ui_listbox_change( wnd, ListBox1, NumFiles, filename_list, 13 );
501 ui_listbox_change( wnd, ListBox2, NumDirs, directory_list, 13 );
504 getcwd( CurDir, 35 );
505 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
506 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
509 //while ( TICKER < i+2 );
512 sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
513 MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
514 UserFile->first_time = 1;
527 _splitpath( UserFile->text, drive, dir, fname, ext );
528 sprintf( fulldir, "%s%s.", drive, dir );
529 sprintf( fullfname, "%s%s", fname, ext );
531 if ( strlen(fulldir) > 1 )
532 file_chdir( fulldir );
534 getcwd( CurDir, 35 );
536 if ( strlen(CurDir) > 0 )
538 if ( CurDir[strlen(CurDir)-1] == '\\' )
539 CurDir[strlen(CurDir)-1] = 0;
542 sprintf( filename, "%s\\%s", CurDir, fullfname );
543 //MessageBox( -2, -2, 1, filename, "Ok" );
545 file_chdir( OrgDir );
547 ui_close_window(wnd);
554 int ui_get_file( char * filename, char * Filespec )
559 NumFiles = file_getfilelist( 200, filename_list, Filespec );
561 for (i=0; i< NumFiles; i++ )
563 MALLOC( text[i], char, 15 );
564 strcpy(text[i], filename_list[i] );
567 x = MenuX( -1, -1, NumFiles, text );
570 strcpy(filename, filename_list[x-1] );
572 for (i=0; i< NumFiles; i++ )