1 /* $Id: file.c,v 1.4 2005-01-24 22:19:10 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.4 2005-01-24 22:19:10 schaffner Exp $";
28 #include <sys\types.h>
41 #define TICKER (*(volatile int *)0x46C)
43 char filename_list[300][13];
44 char directory_list[100][13];
46 static char *Message[] = {
47 "Disk is write protected",
52 "Bad drive-request stuct length",
56 "Printer out of paper",
61 static int error_mode = 0;
63 int __far critical_error_handler( unsigned deverr, unsigned errcode, unsigned far * devhdr )
67 devhdr = devhdr; deverr = deverr;
69 if (error_mode==1) return _HARDERR_FAIL;
71 x = MessageBox( -2, -2, 2, Message[errcode], "Retry", "Fail" );
75 case 1: return _HARDERR_RETRY;
76 case 2: return _HARDERR_FAIL;
77 default: return _HARDERR_FAIL;
81 void InstallErrorHandler()
83 _harderr( critical_error_handler );
86 void file_sort( int n, char list[][13] )
94 for (i=incr; i<n; i++ )
99 if (strncmp(list[j], list[j+incr], 12) > 0)
101 memcpy( t, list[j], 13 );
102 memcpy( list[j], list[j+incr], 13 );
103 memcpy( list[j+incr], t, 13 );
117 int FloppyPresent, FloppyNumber;
120 b = *((unsigned char *)0x410);
122 FloppyPresent = b & 1;
123 FloppyNumber = ((b&0xC0)>>6)+1;
124 if (FloppyPresent && (FloppyNumber==1) )
130 void SetFloppy(int d)
135 *((unsigned char *)0x504) = 0;
137 *((unsigned char *)0x504) = 1;
142 void file_capitalize( char * s )
144 while( *s++ = toupper(*s) );
148 // Changes to a drive if valid.. 1=A, 2=B, etc
149 // If flag, then changes to it.
150 // Returns 0 if not-valid, 1 if valid.
151 int file_chdrive( int DriveNum, int flag )
153 unsigned NumDrives, n, org;
157 _dos_getdrive( &org );
159 _dos_setdrive( DriveNum, &NumDrives );
165 if ( (!flag) && (n != org) )
166 _dos_setdrive( org, &NumDrives );
172 // Changes to directory in dir. Even drive is changed.
173 // Returns 1 if failed.
175 // 1 = Invalid disk drive.
176 // 2 = Invalid directory.
178 int file_chdir( char * dir )
181 char OriginalDirectory[100];
182 char * Drive, * Path;
185 getcwd( OriginalDirectory, 100 );
187 file_capitalize( dir );
189 Drive = strchr(dir, ':');
193 if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
198 SetFloppy(*(Drive - 1) - 'A' + 1);
211 // This chdir might get a critical error!
215 file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
224 int file_getdirlist( int MaxNum, char list[][13] )
227 int NumDirs = 0, i, CurDrive;
233 if (strlen(cwd) >= 4)
235 sprintf( list[NumDirs++], ".." );
238 CurDrive = cwd[0] - 'A' + 1;
240 if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
242 if ( find.attrib & _A_SUBDIR ) {
243 if (strcmp( "..", find.name) && strcmp( ".", find.name))
244 strncpy(list[NumDirs++], find.name, 13 );
247 while( !_dos_findnext( &find ) )
249 if ( find.attrib & _A_SUBDIR ) {
250 if (strcmp( "..", find.name) && strcmp( ".", find.name))
254 MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
257 strncpy(list[NumDirs++], find.name, 13 );
264 file_sort( NumDirs, list );
266 for (i=1; i<=26; i++ )
268 if (file_chdrive(i,0) && (i!=CurDrive))
270 if (!((i==2) && SingleDrive()))
271 sprintf( list[NumDirs++], "%c:", i+'A'-1 );
278 int file_getfilelist( int MaxNum, char list[][13], char * filespec )
285 if( !_dos_findfirst( filespec, 0, &find ) )
287 //if ( !(find.attrib & _A_SUBDIR) )
288 strncpy(list[NumFiles++], find.name, 13 );
290 while( !_dos_findnext( &find ) )
292 //if ( !(find.attrib & _A_SUBDIR) )
296 MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
299 strncpy(list[NumFiles++], find.name, 13 );
306 file_sort( NumFiles, list );
311 static int FirstTime = 1;
312 static char CurDir[128];
314 int ui_get_filename( char * filename, char * Filespec, char * message )
317 int NumFiles, NumDirs,i;
320 char ErrorMessage[100];
322 UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
323 UI_GADGET_LISTBOX * ListBox1;
324 UI_GADGET_LISTBOX * ListBox2;
325 UI_GADGET_INPUTBOX * UserFile;
328 char drive[ _MAX_DRIVE ];
329 char dir[ _MAX_DIR ];
330 char fname[ _MAX_FNAME ];
331 char ext[ _MAX_EXT ];
332 char fulldir[ _MAX_DIR + _MAX_DRIVE ];
333 char fullfname[ _MAX_FNAME + _MAX_EXT ];
338 getcwd( OrgDir, 128 );
341 getcwd( CurDir, 128 );
344 file_chdir( CurDir );
346 //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
351 NumFiles = file_getfilelist( 300, filename_list, Filespec );
353 NumDirs = file_getdirlist( 100, directory_list );
355 wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
357 ui_wprintf_at( wnd, 10, 5, message );
359 _splitpath( filename, drive, dir, fname, ext );
361 sprintf( InputText, "%s%s", fname, ext );
363 ui_wprintf_at( wnd, 20, 32,"N&ame" );
364 UserFile = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
366 ui_wprintf_at( wnd, 20, 86,"&Files" );
367 ui_wprintf_at( wnd, 210, 86,"&Dirs" );
369 ListBox1 = ui_add_gadget_listbox( wnd, 20, 110, 125, 200, NumFiles, filename_list, 13 );
370 ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list, 13 );
372 Button1 = ui_add_gadget_button( wnd, 20, 330, 60, 25, "Ok", NULL );
373 Button2 = ui_add_gadget_button( wnd, 100, 330, 60, 25, "Cancel", NULL );
374 HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
376 wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
378 Button1->hotkey = KEY_CTRLED + KEY_ENTER;
379 Button2->hotkey = KEY_ESC;
380 HelpButton->hotkey = KEY_F1;
381 ListBox1->hotkey = KEY_ALTED + KEY_F;
382 ListBox2->hotkey = KEY_ALTED + KEY_D;
383 UserFile->hotkey = KEY_ALTED + KEY_A;
385 ui_gadget_calc_keys(wnd);
387 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
388 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
395 ui_window_do_gadgets(wnd);
397 if ( Button2->pressed )
399 file_chdir( OrgDir );
400 ui_close_window(wnd);
404 if ( HelpButton->pressed )
405 MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
407 if (ListBox1->moved || new_listboxes)
409 if (ListBox1->current_item >= 0 )
411 strcpy(UserFile->text, filename_list[ListBox1->current_item] );
412 UserFile->position = strlen(UserFile->text);
413 UserFile->oldposition = UserFile->position;
415 UserFile->first_time = 1;
419 if (ListBox2->moved || new_listboxes)
421 if (ListBox2->current_item >= 0 )
423 if (strrchr( directory_list[ListBox2->current_item], ':' ))
424 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
426 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->current_item], Filespec );
427 UserFile->position = strlen(UserFile->text);
428 UserFile->oldposition = UserFile->position;
430 UserFile->first_time = 1;
435 if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
439 if (ListBox2->selected_item > -1 ) {
440 if (strrchr( directory_list[ListBox2->selected_item], ':' ))
441 sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
443 sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->selected_item], Filespec );
446 error_mode = 1; // Critical error handler automatically fails.
448 TempFile = fopen( UserFile->text, "r" );
451 // Looks like a valid filename that already exists!
456 // File doesn't exist, but can we create it?
457 TempFile = fopen( UserFile->text, "w" );
460 // Looks like a valid filename!
462 remove( UserFile->text );
466 _splitpath( UserFile->text, drive, dir, fname, ext );
467 sprintf( fullfname, "%s%s", fname, ext );
469 //mprintf( 0, "----------------------------\n" );
470 //mprintf( 0, "Full text: '%s'\n", UserFile->text );
471 //mprintf( 0, "Drive: '%s'\n", drive );
472 //mprintf( 0, "Dir: '%s'\n", dir );
473 //mprintf( 0, "Filename: '%s'\n", fname );
474 //mprintf( 0, "Extension: '%s'\n", ext );
475 //mprintf( 0, "Full dir: '%s'\n", fulldir );
476 //mprintf( 0, "Full fname: '%s'\n", fname );
478 if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )
480 sprintf( fulldir, "%s%s.", drive, dir );
482 sprintf( fullfname, "%s", Filespec );
483 sprintf( fulldir, "%s", UserFile->text );
486 //mprintf( 0, "----------------------------\n" );
487 //mprintf( 0, "Full dir: '%s'\n", fulldir );
488 //mprintf( 0, "Full fname: '%s'\n", fullfname );
490 if (file_chdir( fulldir )==0)
492 NumFiles = file_getfilelist( 300, filename_list, fullfname );
494 strcpy(UserFile->text, fullfname );
495 UserFile->position = strlen(UserFile->text);
496 UserFile->oldposition = UserFile->position;
498 UserFile->first_time = 1;
500 NumDirs = file_getdirlist( 100, directory_list );
502 ui_listbox_change( wnd, ListBox1, NumFiles, filename_list, 13 );
503 ui_listbox_change( wnd, ListBox2, NumDirs, directory_list, 13 );
506 getcwd( CurDir, 35 );
507 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
508 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
511 while ( TICKER < i+2 );
514 sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
515 MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
516 UserFile->first_time = 1;
529 _splitpath( UserFile->text, drive, dir, fname, ext );
530 sprintf( fulldir, "%s%s.", drive, dir );
531 sprintf( fullfname, "%s%s", fname, ext );
533 if ( strlen(fulldir) > 1 )
534 file_chdir( fulldir );
536 getcwd( CurDir, 35 );
538 if ( strlen(CurDir) > 0 )
540 if ( CurDir[strlen(CurDir)-1] == '\\' )
541 CurDir[strlen(CurDir)-1] = 0;
544 sprintf( filename, "%s\\%s", CurDir, fullfname );
545 //MessageBox( -2, -2, 1, filename, "Ok" );
547 file_chdir( OrgDir );
549 ui_close_window(wnd);
556 int ui_get_file( char * filename, char * Filespec )
561 NumFiles = file_getfilelist( 200, filename_list, Filespec );
563 for (i=0; i< NumFiles; i++ )
565 MALLOC( text[i], char, 15 );
566 strcpy(text[i], filename_list[i] );
569 x = MenuX( -1, -1, NumFiles, text );
572 strcpy(filename, filename_list[x-1] );
574 for (i=0; i< NumFiles; i++ )