2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: file.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
16 #pragma on (unreferenced)
27 #include <sys\types.h>
40 #define TICKER (*(volatile int *)0x46C)
42 char filename_list[300][13];
43 char directory_list[100][13];
45 static char *Message[] = {
46 "Disk is write protected",
51 "Bad drive-request stuct length",
55 "Printer out of paper",
60 static int error_mode = 0;
62 int __far critical_error_handler( unsigned deverr, unsigned errcode, unsigned far * devhdr )
66 devhdr = devhdr; deverr = deverr;
68 if (error_mode==1) return _HARDERR_FAIL;
70 x = MessageBox( -2, -2, 2, Message[errcode], "Retry", "Fail" );
74 case 1: return _HARDERR_RETRY;
75 case 2: return _HARDERR_FAIL;
76 default: return _HARDERR_FAIL;
80 void InstallErrorHandler()
82 _harderr( critical_error_handler );
85 void file_sort( int n, char list[][13] )
93 for (i=incr; i<n; i++ )
98 if (strncmp(list[j], list[j+incr], 12) > 0)
100 memcpy( t, list[j], 13 );
101 memcpy( list[j], list[j+incr], 13 );
102 memcpy( list[j+incr], t, 13 );
116 int FloppyPresent, FloppyNumber;
119 b = *((unsigned char *)0x410);
121 FloppyPresent = b & 1;
122 FloppyNumber = ((b&0xC0)>>6)+1;
123 if (FloppyPresent && (FloppyNumber==1) )
129 void SetFloppy(int d)
134 *((unsigned char *)0x504) = 0;
136 *((unsigned char *)0x504) = 1;
141 void file_capitalize( char * s )
143 while( *s++ = toupper(*s) );
147 // Changes to a drive if valid.. 1=A, 2=B, etc
148 // If flag, then changes to it.
149 // Returns 0 if not-valid, 1 if valid.
150 int file_chdrive( int DriveNum, int flag )
152 unsigned NumDrives, n, org;
156 _dos_getdrive( &org );
158 _dos_setdrive( DriveNum, &NumDrives );
164 if ( (!flag) && (n != org) )
165 _dos_setdrive( org, &NumDrives );
171 // Changes to directory in dir. Even drive is changed.
172 // Returns 1 if failed.
174 // 1 = Invalid disk drive.
175 // 2 = Invalid directory.
177 int file_chdir( char * dir )
180 char OriginalDirectory[100];
181 char * Drive, * Path;
184 getcwd( OriginalDirectory, 100 );
186 file_capitalize( dir );
188 Drive = strchr(dir, ':');
192 if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
197 SetFloppy(*(Drive - 1) - 'A' + 1);
210 // This chdir might get a critical error!
214 file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
223 int file_getdirlist( int MaxNum, char list[][13] )
226 int NumDirs = 0, i, CurDrive;
232 if (strlen(cwd) >= 4)
234 sprintf( list[NumDirs++], ".." );
237 CurDrive = cwd[0] - 'A' + 1;
239 if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
241 if ( find.attrib & _A_SUBDIR ) {
242 if (strcmp( "..", find.name) && strcmp( ".", find.name))
243 strncpy(list[NumDirs++], find.name, 13 );
246 while( !_dos_findnext( &find ) )
248 if ( find.attrib & _A_SUBDIR ) {
249 if (strcmp( "..", find.name) && strcmp( ".", find.name))
253 MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
256 strncpy(list[NumDirs++], find.name, 13 );
263 file_sort( NumDirs, list );
265 for (i=1; i<=26; i++ )
267 if (file_chdrive(i,0) && (i!=CurDrive))
269 if (!((i==2) && SingleDrive()))
270 sprintf( list[NumDirs++], "%c:", i+'A'-1 );
277 int file_getfilelist( int MaxNum, char list[][13], char * filespec )
284 if( !_dos_findfirst( filespec, 0, &find ) )
286 //if ( !(find.attrib & _A_SUBDIR) )
287 strncpy(list[NumFiles++], find.name, 13 );
289 while( !_dos_findnext( &find ) )
291 //if ( !(find.attrib & _A_SUBDIR) )
295 MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
298 strncpy(list[NumFiles++], find.name, 13 );
305 file_sort( NumFiles, list );
310 static int FirstTime = 1;
311 static char CurDir[128];
313 int ui_get_filename( char * filename, char * Filespec, char * message )
316 int NumFiles, NumDirs,i;
319 char ErrorMessage[100];
321 UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
322 UI_GADGET_LISTBOX * ListBox1;
323 UI_GADGET_LISTBOX * ListBox2;
324 UI_GADGET_INPUTBOX * UserFile;
327 char drive[ _MAX_DRIVE ];
328 char dir[ _MAX_DIR ];
329 char fname[ _MAX_FNAME ];
330 char ext[ _MAX_EXT ];
331 char fulldir[ _MAX_DIR + _MAX_DRIVE ];
332 char fullfname[ _MAX_FNAME + _MAX_EXT ];
337 getcwd( OrgDir, 128 );
340 getcwd( CurDir, 128 );
343 file_chdir( CurDir );
345 //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
350 NumFiles = file_getfilelist( 300, filename_list, Filespec );
352 NumDirs = file_getdirlist( 100, directory_list );
354 wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
356 ui_wprintf_at( wnd, 10, 5, message );
358 _splitpath( filename, drive, dir, fname, ext );
360 sprintf( InputText, "%s%s", fname, ext );
362 ui_wprintf_at( wnd, 20, 32,"N&ame" );
363 UserFile = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
365 ui_wprintf_at( wnd, 20, 86,"&Files" );
366 ui_wprintf_at( wnd, 210, 86,"&Dirs" );
368 ListBox1 = ui_add_gadget_listbox( wnd, 20, 110, 125, 200, NumFiles, filename_list, 13 );
369 ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list, 13 );
371 Button1 = ui_add_gadget_button( wnd, 20, 330, 60, 25, "Ok", NULL );
372 Button2 = ui_add_gadget_button( wnd, 100, 330, 60, 25, "Cancel", NULL );
373 HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
375 wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
377 Button1->hotkey = KEY_CTRLED + KEY_ENTER;
378 Button2->hotkey = KEY_ESC;
379 HelpButton->hotkey = KEY_F1;
380 ListBox1->hotkey = KEY_ALTED + KEY_F;
381 ListBox2->hotkey = KEY_ALTED + KEY_D;
382 UserFile->hotkey = KEY_ALTED + KEY_A;
384 ui_gadget_calc_keys(wnd);
386 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
387 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
394 ui_window_do_gadgets(wnd);
396 if ( Button2->pressed )
398 file_chdir( OrgDir );
399 ui_close_window(wnd);
403 if ( HelpButton->pressed )
404 MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
406 if (ListBox1->moved || new_listboxes)
408 if (ListBox1->current_item >= 0 )
410 strcpy(UserFile->text, filename_list[ListBox1->current_item] );
411 UserFile->position = strlen(UserFile->text);
412 UserFile->oldposition = UserFile->position;
414 UserFile->first_time = 1;
418 if (ListBox2->moved || new_listboxes)
420 if (ListBox2->current_item >= 0 )
422 if (strrchr( directory_list[ListBox2->current_item], ':' ))
423 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
425 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->current_item], Filespec );
426 UserFile->position = strlen(UserFile->text);
427 UserFile->oldposition = UserFile->position;
429 UserFile->first_time = 1;
434 if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
438 if (ListBox2->selected_item > -1 ) {
439 if (strrchr( directory_list[ListBox2->selected_item], ':' ))
440 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
442 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->selected_item], Filespec );
445 error_mode = 1; // Critical error handler automatically fails.
447 TempFile = fopen( UserFile->text, "r" );
450 // Looks like a valid filename that already exists!
455 // File doesn't exist, but can we create it?
456 TempFile = fopen( UserFile->text, "w" );
459 // Looks like a valid filename!
461 remove( UserFile->text );
465 _splitpath( UserFile->text, drive, dir, fname, ext );
466 sprintf( fullfname, "%s%s", fname, ext );
468 //mprintf( 0, "----------------------------\n" );
469 //mprintf( 0, "Full text: '%s'\n", UserFile->text );
470 //mprintf( 0, "Drive: '%s'\n", drive );
471 //mprintf( 0, "Dir: '%s'\n", dir );
472 //mprintf( 0, "Filename: '%s'\n", fname );
473 //mprintf( 0, "Extension: '%s'\n", ext );
474 //mprintf( 0, "Full dir: '%s'\n", fulldir );
475 //mprintf( 0, "Full fname: '%s'\n", fname );
477 if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )
479 sprintf( fulldir, "%s%s.", drive, dir );
481 sprintf( fullfname, "%s", Filespec );
482 sprintf( fulldir, "%s", UserFile->text );
485 //mprintf( 0, "----------------------------\n" );
486 //mprintf( 0, "Full dir: '%s'\n", fulldir );
487 //mprintf( 0, "Full fname: '%s'\n", fullfname );
489 if (file_chdir( fulldir )==0)
491 NumFiles = file_getfilelist( 300, filename_list, fullfname );
493 strcpy(UserFile->text, fullfname );
494 UserFile->position = strlen(UserFile->text);
495 UserFile->oldposition = UserFile->position;
497 UserFile->first_time = 1;
499 NumDirs = file_getdirlist( 100, directory_list );
501 ui_listbox_change( wnd, ListBox1, NumFiles, filename_list, 13 );
502 ui_listbox_change( wnd, ListBox2, NumDirs, directory_list, 13 );
505 getcwd( CurDir, 35 );
506 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
507 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
510 while ( TICKER < i+2 );
513 sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
514 MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
515 UserFile->first_time = 1;
528 _splitpath( UserFile->text, drive, dir, fname, ext );
529 sprintf( fulldir, "%s%s.", drive, dir );
530 sprintf( fullfname, "%s%s", fname, ext );
532 if ( strlen(fulldir) > 1 )
533 file_chdir( fulldir );
535 getcwd( CurDir, 35 );
537 if ( strlen(CurDir) > 0 )
539 if ( CurDir[strlen(CurDir)-1] == '\\' )
540 CurDir[strlen(CurDir)-1] = 0;
543 sprintf( filename, "%s\\%s", CurDir, fullfname );
544 //MessageBox( -2, -2, 1, filename, "Ok" );
546 file_chdir( OrgDir );
548 ui_close_window(wnd);
555 int ui_get_file( char * filename, char * Filespec )
560 NumFiles = file_getfilelist( 200, filename_list, Filespec );
562 for (i=0; i< NumFiles; i++ )
564 MALLOC( text[i], char, 15 );
565 strcpy(text[i], filename_list[i] );
568 x = MenuX( -1, -1, NumFiles, text );
571 strcpy(filename, filename_list[x-1] );
573 for (i=0; i< NumFiles; i++ )