FILE.C 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/ui/rcs/file.c $
  15. * $Revision: 1.6 $
  16. * $Author: john $
  17. * $Date: 1994/06/09 12:18:29 $
  18. *
  19. * File dialog box stuff.
  20. *
  21. * $Log: file.c $
  22. * Revision 1.6 1994/06/09 12:18:29 john
  23. * Took out keyboard flushes.
  24. *
  25. * Revision 1.5 1994/04/27 18:30:49 john
  26. * Fixed bug with enter a directory without a slash on
  27. * the end not working.
  28. * .
  29. *
  30. * Revision 1.4 1994/04/22 11:09:47 john
  31. * Speed up directory loading by only searching for *. instead of *.*
  32. *
  33. * Revision 1.3 1993/12/07 12:30:18 john
  34. * new version.
  35. *
  36. * Revision 1.2 1993/10/26 13:46:22 john
  37. * *** empty log message ***
  38. *
  39. * Revision 1.1 1993/10/06 11:10:23 john
  40. * Initial revision
  41. *
  42. *
  43. */
  44. #pragma off (unreferenced)
  45. static char rcsid[] = "$Id: file.c 1.6 1994/06/09 12:18:29 john Exp $";
  46. #pragma on (unreferenced)
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <direct.h>
  51. #include <dos.h>
  52. #include <ctype.h>
  53. #include <conio.h>
  54. #include <fcntl.h>
  55. #include <io.h>
  56. #include <sys\types.h>
  57. #include <sys\stat.h>
  58. #include "fix.h"
  59. #include "types.h"
  60. #include "gr.h"
  61. #include "key.h"
  62. #include "ui.h"
  63. #include "mono.h"
  64. #include "mem.h"
  65. #define TICKER (*(volatile int *)0x46C)
  66. char filename_list[300][13];
  67. char directory_list[100][13];
  68. static char *Message[] = {
  69. "Disk is write protected",
  70. "Unknown unit",
  71. "Drive not ready",
  72. "Unknown command",
  73. "CRC error in data",
  74. "Bad drive-request stuct length",
  75. "Seek error",
  76. "Unknown media type",
  77. "Sector not found",
  78. "Printer out of paper",
  79. "Write fault",
  80. "Read fault",
  81. "General Failure" };
  82. static int error_mode = 0;
  83. int __far critical_error_handler( unsigned deverr, unsigned errcode, unsigned far * devhdr )
  84. {
  85. int x;
  86. devhdr = devhdr; deverr = deverr;
  87. if (error_mode==1) return _HARDERR_FAIL;
  88. x = MessageBox( -2, -2, 2, Message[errcode], "Retry", "Fail" );
  89. switch (x)
  90. {
  91. case 1: return _HARDERR_RETRY;
  92. case 2: return _HARDERR_FAIL;
  93. default: return _HARDERR_FAIL;
  94. }
  95. }
  96. void InstallErrorHandler()
  97. {
  98. _harderr((void *) critical_error_handler );
  99. //Above line modified by KRB, added (void *) cast
  100. //for the compiler.
  101. }
  102. void file_sort( int n, char list[][13] )
  103. {
  104. int i, j, incr;
  105. char t[14];
  106. incr = n / 2;
  107. while( incr > 0 )
  108. {
  109. for (i=incr; i<n; i++ )
  110. {
  111. j = i - incr;
  112. while (j>=0 )
  113. {
  114. if (strncmp(list[j], list[j+incr], 12) > 0)
  115. {
  116. memcpy( t, list[j], 13 );
  117. memcpy( list[j], list[j+incr], 13 );
  118. memcpy( list[j+incr], t, 13 );
  119. j -= incr;
  120. }
  121. else
  122. break;
  123. }
  124. }
  125. incr = incr / 2;
  126. }
  127. }
  128. int SingleDrive()
  129. {
  130. int FloppyPresent, FloppyNumber;
  131. unsigned char b;
  132. b = *((unsigned char *)0x410);
  133. FloppyPresent = b & 1;
  134. FloppyNumber = ((b&0xC0)>>6)+1;
  135. if (FloppyPresent && (FloppyNumber==1) )
  136. return 1;
  137. else
  138. return 0;
  139. }
  140. void SetFloppy(int d)
  141. {
  142. if (SingleDrive())
  143. {
  144. if (d==1)
  145. *((unsigned char *)0x504) = 0;
  146. else if (d==2)
  147. *((unsigned char *)0x504) = 1;
  148. }
  149. }
  150. void file_capitalize( char * s )
  151. {
  152. while( *s++ = toupper(*s) );
  153. }
  154. // Changes to a drive if valid.. 1=A, 2=B, etc
  155. // If flag, then changes to it.
  156. // Returns 0 if not-valid, 1 if valid.
  157. int file_chdrive( int DriveNum, int flag )
  158. {
  159. unsigned NumDrives, n, org;
  160. int Valid = 0;
  161. if (!flag)
  162. _dos_getdrive( &org );
  163. _dos_setdrive( DriveNum, &NumDrives );
  164. _dos_getdrive( &n );
  165. if (n == DriveNum )
  166. Valid = 1;
  167. if ( (!flag) && (n != org) )
  168. _dos_setdrive( org, &NumDrives );
  169. return Valid;
  170. }
  171. // Changes to directory in dir. Even drive is changed.
  172. // Returns 1 if failed.
  173. // 0 = Changed ok.
  174. // 1 = Invalid disk drive.
  175. // 2 = Invalid directory.
  176. int file_chdir( char * dir )
  177. {
  178. int e;
  179. char OriginalDirectory[100];
  180. char * Drive, * Path;
  181. char NoDir[] = "\.";
  182. getcwd( OriginalDirectory, 100 );
  183. file_capitalize( dir );
  184. Drive = strchr(dir, ':');
  185. if (Drive)
  186. {
  187. if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
  188. return 1;
  189. Path = Drive+1;
  190. SetFloppy(*(Drive - 1) - 'A' + 1);
  191. }
  192. else
  193. {
  194. Path = dir;
  195. }
  196. if (!(*Path))
  197. {
  198. Path = NoDir;
  199. }
  200. // This chdir might get a critical error!
  201. e = chdir( Path );
  202. if (e)
  203. {
  204. file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
  205. return 2;
  206. }
  207. return 0;
  208. }
  209. int file_getdirlist( int MaxNum, char list[][13] )
  210. {
  211. struct find_t find;
  212. int NumDirs = 0, i, CurDrive;
  213. char cwd[129];
  214. MaxNum = MaxNum;
  215. getcwd(cwd, 128 );
  216. if (strlen(cwd) >= 4)
  217. {
  218. sprintf( list[NumDirs++], ".." );
  219. }
  220. CurDrive = cwd[0] - 'A' + 1;
  221. if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
  222. {
  223. if ( find.attrib & _A_SUBDIR ) {
  224. if (strcmp( "..", find.name) && strcmp( ".", find.name))
  225. strncpy(list[NumDirs++], find.name, 13 );
  226. }
  227. while( !_dos_findnext( &find ) )
  228. {
  229. if ( find.attrib & _A_SUBDIR ) {
  230. if (strcmp( "..", find.name) && strcmp( ".", find.name))
  231. {
  232. if (NumDirs==74)
  233. {
  234. MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
  235. break;
  236. } else {
  237. strncpy(list[NumDirs++], find.name, 13 );
  238. }
  239. }
  240. }
  241. }
  242. }
  243. file_sort( NumDirs, list );
  244. for (i=1; i<=26; i++ )
  245. {
  246. if (file_chdrive(i,0) && (i!=CurDrive))
  247. {
  248. if (!((i==2) && SingleDrive()))
  249. sprintf( list[NumDirs++], "%c:", i+'A'-1 );
  250. }
  251. }
  252. return NumDirs;
  253. }
  254. int file_getfilelist( int MaxNum, char list[][13], char * filespec )
  255. {
  256. struct find_t find;
  257. int NumFiles = 0;
  258. MaxNum = MaxNum;
  259. if( !_dos_findfirst( filespec, 0, &find ) )
  260. {
  261. //if ( !(find.attrib & _A_SUBDIR) )
  262. strncpy(list[NumFiles++], find.name, 13 );
  263. while( !_dos_findnext( &find ) )
  264. {
  265. //if ( !(find.attrib & _A_SUBDIR) )
  266. //{
  267. if (NumFiles==300)
  268. {
  269. MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
  270. break;
  271. } else {
  272. strncpy(list[NumFiles++], find.name, 13 );
  273. }
  274. //}
  275. }
  276. }
  277. file_sort( NumFiles, list );
  278. return NumFiles;
  279. }
  280. static int FirstTime = 1;
  281. static char CurDir[128];
  282. int ui_get_filename( char * filename, char * Filespec, char * message )
  283. {
  284. FILE * TempFile;
  285. int NumFiles, NumDirs,i;
  286. char InputText[100];
  287. char Spaces[35];
  288. char ErrorMessage[100];
  289. UI_WINDOW * wnd;
  290. UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
  291. UI_GADGET_LISTBOX * ListBox1;
  292. UI_GADGET_LISTBOX * ListBox2;
  293. UI_GADGET_INPUTBOX * UserFile;
  294. int new_listboxes;
  295. char drive[ _MAX_DRIVE ];
  296. char dir[ _MAX_DIR ];
  297. char fname[ _MAX_FNAME ];
  298. char ext[ _MAX_EXT ];
  299. char fulldir[ _MAX_DIR + _MAX_DRIVE ];
  300. char fullfname[ _MAX_FNAME + _MAX_EXT ];
  301. char OrgDir[128];
  302. getcwd( OrgDir, 128 );
  303. if (FirstTime)
  304. getcwd( CurDir, 128 );
  305. FirstTime=0;
  306. file_chdir( CurDir );
  307. //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
  308. for (i=0; i<35; i++)
  309. Spaces[i] = ' ';
  310. Spaces[34] = 0;
  311. NumFiles = file_getfilelist( 300, filename_list, Filespec );
  312. NumDirs = file_getdirlist( 100, directory_list );
  313. wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
  314. ui_wprintf_at( wnd, 10, 5, message );
  315. _splitpath( filename, drive, dir, fname, ext );
  316. sprintf( InputText, "%s%s", fname, ext );
  317. ui_wprintf_at( wnd, 20, 32,"N&ame" );
  318. UserFile = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
  319. ui_wprintf_at( wnd, 20, 86,"&Files" );
  320. ui_wprintf_at( wnd, 210, 86,"&Dirs" );
  321. ListBox1 = ui_add_gadget_listbox( wnd, 20, 110, 125, 200, NumFiles, filename_list, 13 );
  322. ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list, 13 );
  323. Button1 = ui_add_gadget_button( wnd, 20, 330, 60, 25, "Ok", NULL );
  324. Button2 = ui_add_gadget_button( wnd, 100, 330, 60, 25, "Cancel", NULL );
  325. HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
  326. wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
  327. Button1->hotkey = KEY_CTRLED + KEY_ENTER;
  328. Button2->hotkey = KEY_ESC;
  329. HelpButton->hotkey = KEY_F1;
  330. ListBox1->hotkey = KEY_ALTED + KEY_F;
  331. ListBox2->hotkey = KEY_ALTED + KEY_D;
  332. UserFile->hotkey = KEY_ALTED + KEY_A;
  333. ui_gadget_calc_keys(wnd);
  334. ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
  335. ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
  336. new_listboxes = 0;
  337. while( 1 )
  338. {
  339. ui_mega_process();
  340. ui_window_do_gadgets(wnd);
  341. if ( Button2->pressed )
  342. {
  343. file_chdir( OrgDir );
  344. ui_close_window(wnd);
  345. return 0;
  346. }
  347. if ( HelpButton->pressed )
  348. MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
  349. if (ListBox1->moved || new_listboxes)
  350. {
  351. if (ListBox1->current_item >= 0 )
  352. {
  353. strcpy(UserFile->text, filename_list[ListBox1->current_item] );
  354. UserFile->position = strlen(UserFile->text);
  355. UserFile->oldposition = UserFile->position;
  356. UserFile->status=1;
  357. UserFile->first_time = 1;
  358. }
  359. }
  360. if (ListBox2->moved || new_listboxes)
  361. {
  362. if (ListBox2->current_item >= 0 )
  363. {
  364. if (strrchr( directory_list[ListBox2->current_item], ':' ))
  365. sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
  366. else
  367. sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->current_item], Filespec );
  368. UserFile->position = strlen(UserFile->text);
  369. UserFile->oldposition = UserFile->position;
  370. UserFile->status=1;
  371. UserFile->first_time = 1;
  372. }
  373. }
  374. new_listboxes = 0;
  375. if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
  376. {
  377. ui_mouse_hide();
  378. if (ListBox2->selected_item > -1 ) {
  379. if (strrchr( directory_list[ListBox2->selected_item], ':' ))
  380. sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
  381. else
  382. sprintf( UserFile->text, "%s\\%s", directory_list[ListBox2->selected_item], Filespec );
  383. }
  384. error_mode = 1; // Critical error handler automatically fails.
  385. TempFile = fopen( UserFile->text, "r" );
  386. if (TempFile)
  387. {
  388. // Looks like a valid filename that already exists!
  389. fclose( TempFile );
  390. break;
  391. }
  392. // File doesn't exist, but can we create it?
  393. TempFile = fopen( UserFile->text, "w" );
  394. if (TempFile)
  395. {
  396. // Looks like a valid filename!
  397. fclose( TempFile );
  398. remove( UserFile->text );
  399. break;
  400. }
  401. _splitpath( UserFile->text, drive, dir, fname, ext );
  402. 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 );
  411. if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )
  412. {
  413. sprintf( fulldir, "%s%s.", drive, dir );
  414. } else {
  415. sprintf( fullfname, "%s", Filespec );
  416. sprintf( fulldir, "%s", UserFile->text );
  417. }
  418. //mprintf( 0, "----------------------------\n" );
  419. //mprintf( 0, "Full dir: '%s'\n", fulldir );
  420. //mprintf( 0, "Full fname: '%s'\n", fullfname );
  421. if (file_chdir( fulldir )==0)
  422. {
  423. NumFiles = file_getfilelist( 300, filename_list, fullfname );
  424. strcpy(UserFile->text, fullfname );
  425. UserFile->position = strlen(UserFile->text);
  426. UserFile->oldposition = UserFile->position;
  427. UserFile->status=1;
  428. UserFile->first_time = 1;
  429. NumDirs = file_getdirlist( 100, directory_list );
  430. ui_listbox_change( wnd, ListBox1, NumFiles, filename_list, 13 );
  431. ui_listbox_change( wnd, ListBox2, NumDirs, directory_list, 13 );
  432. new_listboxes = 0;
  433. getcwd( CurDir, 35 );
  434. ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
  435. ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
  436. i = TICKER;
  437. while ( TICKER < i+2 );
  438. }else {
  439. sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
  440. MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
  441. UserFile->first_time = 1;
  442. }
  443. error_mode = 0;
  444. ui_mouse_show();
  445. }
  446. }
  447. //key_flush();
  448. _splitpath( UserFile->text, drive, dir, fname, ext );
  449. sprintf( fulldir, "%s%s.", drive, dir );
  450. sprintf( fullfname, "%s%s", fname, ext );
  451. if ( strlen(fulldir) > 1 )
  452. file_chdir( fulldir );
  453. getcwd( CurDir, 35 );
  454. if ( strlen(CurDir) > 0 )
  455. {
  456. if ( CurDir[strlen(CurDir)-1] == '\\' )
  457. CurDir[strlen(CurDir)-1] = 0;
  458. }
  459. sprintf( filename, "%s\\%s", CurDir, fullfname );
  460. //MessageBox( -2, -2, 1, filename, "Ok" );
  461. file_chdir( OrgDir );
  462. ui_close_window(wnd);
  463. return 1;
  464. }
  465. int ui_get_file( char * filename, char * Filespec )
  466. {
  467. int x, i, NumFiles;
  468. char * text[200];
  469. NumFiles = file_getfilelist( 200, filename_list, Filespec );
  470. for (i=0; i< NumFiles; i++ )
  471. {
  472. //MALLOC( text[i], char, 15 );//Another compile hack -KRB
  473. text[i]=(char *)malloc(15*sizeof(char));
  474. strcpy(text[i], filename_list[i] );
  475. }
  476. x = MenuX( -1, -1, NumFiles, text );
  477. if ( x > 0 )
  478. strcpy(filename, filename_list[x-1] );
  479. for (i=0; i< NumFiles; i++ )
  480. {
  481. free( text[i] );
  482. }
  483. if ( x>0 )
  484. return 1;
  485. else
  486. return 0;
  487. }
  488.