home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / l / lzwbackup / !LZWbackup / c / LZWbackup
Encoding:
Text File  |  1994-11-20  |  26.0 KB  |  947 lines

  1. /**************************/
  2. /* ANSI standard includes */
  3. /**************************/
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <kernel.h>
  9. #include <swis.h>
  10.  
  11. /***********************/
  12. /* risc_oslib includes */
  13. /***********************/                     
  14.  
  15. #include "risc_oslib:wimp.h" 
  16. #include "risc_oslib:menu.h"
  17. #include "risc_oslib:wimpt.h"
  18. #include "risc_oslib:baricon.h"
  19. #include "risc_oslib:werr.h"
  20. #include "RISC_OSlib:res.h"
  21. #include "RISC_OSlib:resspr.h"
  22. #include "RISC_OSlib:event.h"
  23. #include "RISC_OSlib:win.h" 
  24. #include "RISC_OSlib:template.h"
  25. #include "RISC_OSlib:dbox.h"
  26. #include "RISC_OSlib:os.h"
  27. #include "RISC_OSlib:bbc.h"
  28.  
  29. /********************/
  30. /* desklib includes */
  31. /********************/
  32.  
  33. #include "Desklib:mem.h"
  34.  
  35. /********************/
  36. /* worzlib includes */
  37. /********************/
  38.  
  39. #include "Worzlib:hglass.h" 
  40.  
  41. /******************/
  42. /* local includes */
  43. /******************/
  44.  
  45. #include "LZWmodule.h"
  46. #include "LZWstuff.h"
  47. /* #include "hglass.h" */
  48.  
  49. /*****************/
  50. /* local defines */
  51. /*****************/
  52.  
  53. /* none */
  54.  
  55. /********************/
  56. /* global variables */
  57. /********************/
  58.  
  59. dbox main_window;    /* the window number of the main window */
  60. int  state=STATE_no_backup;    /* the state of the program i.e. no_backup, load file etc. */
  61. FILE *include_handle=0;    /* file handle of the include file */
  62. void *exclude_file_pointer=0;    /* pointer to exclude file in memory */
  63. int  exclude_file_length=0;    /* length of exclude file in memory */
  64. void *compressed_data=0;    /* pointer to the compressed file in memory */
  65. void *original_data=0;    /* pointer to the source file in memory */
  66. void *floppy_data=0;    /* pointer to the data in memory that will be written to the floppy */
  67. int  compressed_data_length=0;    /* length of compressed data in memory */
  68. int  original_data_length=0;    /* length of source file in memory */
  69. int  floppy_data_length=0;    /* length of the data to be written to the floppy */
  70. char header_block[FILE_HEADER_LENGTH];    /* for the header info to be created in */
  71. int  floppy_count;    /* number of the floppy disc in the series */
  72. char floppy_file_name[255];    /* partial file name of the floppy file */
  73. int  event_count=0;    /* for keeping count of events processed in the main loop */
  74. char version[]="1.0 (Nov 1994)"; /* version string in info box */
  75.  
  76. /***********************/
  77. /* function prototypes */
  78. /***********************/
  79.  
  80. static void ibar_select(wimp_i icon_number); /**/
  81. static void main_window_handler(dbox dbox_number, void *handle); /**/
  82. static void ibar_load(wimp_eventstr *data_block, void *handle); /**/
  83. static void load_next_file(void); /**/
  84. static void compress_next_file(void); /**/
  85. static void write_next_file(void); /**/
  86. static void abort_button_clicked(void); /**/
  87. static void go_button_clicked(void); /**/
  88. static void continue_button_clicked(void); /**/
  89. static void open_main_window(void); /**/
  90. static void show_info_box(void); /**/
  91. static void list_files(char *dir_name, FILE *list_handle); /**/
  92. static void end_backup(void); /**/
  93. static int  in_exclude_list(char *filename); /**/
  94. static void write_next_directory(void); /**/
  95. static int  prompt_for_floppy(void); /**/
  96. static void ibar_menu(void *h, char *hit); /**/
  97. static int  floppy_free_space(void); /**/
  98. static int  erase_floppy(void); /**/
  99. static void create_empty_file(char *file_name); /**/
  100.  
  101. /****************/
  102. /* program code */
  103. /****************/
  104.  
  105. /* start of main */
  106. int main(void)
  107. {
  108. /* create variables */
  109. menu icon_menu;    /* the menu number of the menu attached to the icon bar icon */
  110.  
  111. /* do the initialisations */
  112. wimpt_init("LZW Backup");
  113. res_init("LZWbackup");
  114. resspr_init();
  115. template_init();          
  116. dbox_init();
  117. Mem_Initialise();
  118.  
  119. /* show the icon on the icon bar */
  120. baricon("!lzwbackup", 1, ibar_select);
  121.  
  122. /* create the menu for the icon bar icon */
  123. icon_menu=menu_new("LZW Backup",">Info,Show Include List,Show Exclude List,Clear Include List,Clear Exclude List,Show Main Window,Quit");
  124.  
  125. /* attach the menu to the icon bar icon, quit with a message if the attach fails */
  126. if(!event_attachmenu(win_ICONBAR, icon_menu, ibar_menu, 0))
  127.     werr(1,"Unable to attach icon bar menu");
  128.  
  129. /* create main window, quit with an error message if it fails */
  130. main_window=dbox_new("main");
  131. if(!main_window)
  132.     werr(1,"Error creating dbox for main");
  133.  
  134. /* set the fields in the main window to default values */
  135. dbox_setfield(main_window, MAIN_WINDOW_osize, "\0");
  136. dbox_setfield(main_window, MAIN_WINDOW_csize, "\0");
  137. dbox_setfield(main_window, MAIN_WINDOW_fspace, "\0");
  138. dbox_setfield(main_window, MAIN_WINDOW_comment, "Hello World");
  139. dbox_setfield(main_window, MAIN_WINDOW_source_fname, "\0");
  140. dbox_setfield(main_window, MAIN_WINDOW_floppy_fname, "Backup");
  141.  
  142. /* activate/deactivate the required buttons */
  143. dbox_unfadefield(main_window, MAIN_WINDOW_go);
  144. dbox_unfadefield(main_window, MAIN_WINDOW_inc_disc_name);
  145. dbox_unfadefield(main_window, MAIN_WINDOW_inc_dirs);
  146. dbox_fadefield(main_window, MAIN_WINDOW_continue);
  147. dbox_fadefield(main_window, MAIN_WINDOW_abort);
  148.  
  149. /* register main_window_handler() as the function to decode mouse clicks on the main window */
  150. dbox_eventhandler(main_window, main_window_handler, 0);
  151.  
  152. /* register ibar_load() as the function to handle a file icon being dropped on the icon bar icon */
  153. win_register_event_handler(win_ICONBARLOAD, ibar_load, 0);
  154.  
  155. /* main Wimp_Poll loop */
  156. while(TRUE)
  157.     {
  158.     event_process();
  159.     if(!event_count--)
  160.         {
  161.         event_count=4; /* only does one of our jobs every n events */
  162.         switch(state)
  163.             {
  164.             case STATE_no_backup:    /* do nothing */
  165.                 break;
  166.             case STATE_load:    /* load a file into memory */
  167.                 load_next_file();
  168.                 /* new state gets set by load_next_file */
  169.                 break;
  170.             case STATE_compress:    /* compress the file in memory */
  171.                 compress_next_file();
  172.                 state=STATE_write_file;
  173.                 break;
  174.             case STATE_write_file:    /* write file to floppy */
  175.                 write_next_file();
  176.                 state=STATE_load;
  177.                 break;
  178.             case STATE_write_directory:    /* write directory to floppy */
  179.                 write_next_directory();
  180.                 state=STATE_load;
  181.                 break;
  182.             default:    /* what sort of state is this ?? */
  183.                 abort_button_clicked();
  184.             }
  185.         }
  186.     }
  187. }
  188. /* end of main() */
  189.  
  190. /* start of ibar_select */
  191. static void ibar_select(wimp_i icon_number)
  192. {
  193. open_main_window();
  194. }
  195. /* end of ibar_select */
  196.  
  197. /* start of main_window_handler */
  198. /* called when the mouse is clicked on the main window */
  199. static void main_window_handler(dbox dbox_number, void *handle)
  200. {
  201. /* create variables */
  202.  
  203. switch (dbox_get(dbox_number))
  204.     {
  205.     case MAIN_WINDOW_go:    /* the go button */
  206.         go_button_clicked();
  207.         break;
  208.     case MAIN_WINDOW_abort:    /* the abort button */
  209.         abort_button_clicked();
  210.         break;
  211.     case MAIN_WINDOW_continue:    /* the continue button */
  212.         continue_button_clicked();
  213.         break;
  214.     case -1: /* the go away button */
  215.         dbox_hide(dbox_number);
  216.         break;
  217.     /* default: */
  218.         /* do nothing */
  219.     }
  220. }
  221. /* end of main_window_handler */
  222.  
  223. /* start of go_button_clicked */
  224. static void go_button_clicked(void)
  225. {
  226. /* create variables */
  227. _kernel_swi_regs regs;
  228. _kernel_oserror *error;
  229. char floppy_full_name[255];
  230.  
  231. /* activate/deactivate the required buttons */
  232. dbox_unfadefield(main_window, MAIN_WINDOW_abort);
  233. dbox_fadefield(main_window, MAIN_WINDOW_go);
  234. dbox_fadefield(main_window, MAIN_WINDOW_continue);
  235. dbox_fadefield(main_window, MAIN_WINDOW_floppy_fname);
  236. dbox_fadefield(main_window, MAIN_WINDOW_inc_dirs);
  237. dbox_fadefield(main_window, MAIN_WINDOW_inc_disc_name);
  238.  
  239. /* get the filename to use on the floppy */
  240. dbox_getfield(main_window, MAIN_WINDOW_floppy_fname, floppy_file_name,7);
  241.  
  242. /* open the include file */
  243. include_handle=fopen("<LZWbackup$include>","r");
  244. if(!include_handle)
  245.     {
  246.     open_main_window();
  247.     dbox_setfield(main_window, MAIN_WINDOW_comment,"No Include list file available");
  248.     bbc_vdu(7);
  249.     end_backup();
  250.     return;
  251.     }
  252.  
  253. /* load the exclude file */
  254. /* get info on the exclude file */
  255. regs.r[0]=5;
  256. regs.r[1]=(int)"<LZWbackup$exclude>";
  257. error=_kernel_swi(OS_File,®s,®s);
  258. if(error)
  259.     werr(1,"Error %d in OS_File 5 in main_window_handler, %s.",error->errnum,error->errmess);
  260. exclude_file_length=regs.r[4];
  261.  
  262. switch(regs.r[0])
  263.     {
  264.     case 0: /* exclude file not found */
  265.         exclude_file_pointer=0;
  266.         exclude_file_length=0;
  267.         break;
  268.     case 1: /* exclude file found ok */
  269.         if(exclude_file_length>0)
  270.             {
  271.             Mem_Alloc(&exclude_file_pointer, exclude_file_length+1);
  272.             if(!exclude_file_pointer)
  273.                 werr(1,"Unable to malloc %d bytes for the exclude file.",exclude_file_length);
  274.             regs.r[0]=0xff; /* load file */
  275.             regs.r[1]=(int)"<LZWbackup$exclude>";
  276.             regs.r[2]=(int)exclude_file_pointer;
  277.             regs.r[3]=0;
  278.             error=_kernel_swi(OS_File,®s,®s);
  279.             if(error)
  280.                 werr(1,"Error %d in OS_File 0xff in main_window_handler, %s.",error->errnum,error->errmess);
  281.             }
  282.         else
  283.             {
  284.         exclude_file_pointer=0;
  285.         exclude_file_length=0;
  286.             }
  287.         break;
  288.     default: /* exclude file of an unrecognised type */
  289.         werr(0,"Exclude file is of an unrecognised type");
  290.         exclude_file_pointer=0;
  291.         exclude_file_length=0;
  292.     }
  293.  
  294. /* allow null events */
  295. event_setmask(0);
  296.  
  297. /* erase the floppy in the drive */
  298. do    {
  299.     if(prompt_for_floppy()==MAIN_WINDOW_abort)
  300.         {
  301.         abort_button_clicked();
  302.         return;
  303.         }
  304.     }    while(!erase_floppy());
  305.  
  306. /* reset the floppy disc counter */
  307. floppy_count=0;
  308.  
  309. /* create the empty file */
  310. sprintf(floppy_full_name,"ADFS::0.$.%s_%0.2d",floppy_file_name,floppy_count);
  311. create_empty_file(floppy_full_name);
  312.  
  313. /* set the state variable to load next file */
  314. state=STATE_load;
  315. }
  316. /* end of go_button_clicked() */
  317.  
  318. /* start of abort_button_clicked() */
  319. static void abort_button_clicked(void)
  320. {
  321. dbox_setfield(main_window, MAIN_WINDOW_comment, "Backup Aborted");
  322. end_backup();
  323. }
  324. /* end of abort_button_clicked() */
  325.  
  326. /* start of ibar_load() */
  327. /* this takes the filename given and appends it to the include file */
  328. /* if it is a directory, it appends the contents to the include file */
  329. static void ibar_load(wimp_eventstr *data_block, void *handle)
  330. {
  331. /* create variables */
  332. FILE *out_handle;    /* file handle for include file */
  333. os_filestr os_file_block;    /* data block for os_file */
  334. os_error *error;    /* pointer to data block for any error returned from os_file */
  335.  
  336. os_file_block.action = 5; /* read catalog info */
  337. os_file_block.name = data_block->data.msg.data.dataload.name; /* set pointer to file name */
  338. error = os_file(&os_file_block);
  339. if(error)
  340.     werr(0,"Error %d in os_file in ibar_load. %s", error->errnum, error->errmess);
  341.  
  342. out_handle=fopen("<LZWbackup$include>","a");
  343. if(!out_handle)
  344.     werr(1,"Error opening include file for append in extend_include_file");
  345.  
  346. hglass_on();
  347. switch (os_file_block.action)
  348.     {
  349.     case 1: /* it's a file */
  350.         fprintf(out_handle,"%s\n",os_file_block.name);
  351.         break;
  352.     case 2: /* it's a directory */
  353.         fprintf(out_handle,"%s\n",os_file_block.name);
  354.         list_files(os_file_block.name, out_handle);
  355.         break;
  356.     default: /* what the #$!* is that ??? */
  357.         break;
  358.     }
  359. hglass_off();
  360.  
  361. fclose(out_handle);
  362. }
  363.  
  364. /* start of show_info_box */
  365. void show_info_box(void)
  366. {
  367. dbox d=dbox_new("info");
  368. if(d!=NULL)
  369.     {
  370.     dbox_show(d);
  371.     dbox_setfield(d, 0, version);
  372.     dbox_fillin(d);
  373.     dbox_dispose(&d);
  374.     }
  375. }
  376. /* end of show_info_box() */
  377.  
  378. /* start of list_files */
  379. void list_files(char *dir_name, FILE *list_handle)
  380. {
  381. /* create variables */
  382. char dir_space[33*77], file_name[12], full_file_name[200];
  383. char *dir_space_pointer;
  384. int number_of_files, file_count;
  385. _kernel_oserror *error;
  386. _kernel_swi_regs regs;
  387.  
  388. regs.r[0]=10; /* read directory info */
  389. regs.r[1]=(int)dir_name;
  390. regs.r[2]=(int)dir_space;
  391. regs.r[3]=77; /* number of entries in a directory */
  392. regs.r[4]=0;
  393. regs.r[5]=77*32;
  394. regs.r[6]=(int)"*";
  395. error=_kernel_swi(OS_GBPB, ®s, ®s);
  396. if(error)
  397.     {
  398.     werr(1,"Error %d in OS_GPBP in list_files() %s",error->errnum,error->errmess);
  399.     hglass_off();
  400.     }
  401.  
  402. number_of_files=regs.r[3];
  403. dir_space_pointer=dir_space;
  404. for(file_count=1; file_count<=number_of_files; file_count++)
  405.     {
  406.     strcpy(file_name,(dir_space_pointer+0x14));
  407.     sprintf(full_file_name,"%s.%s",dir_name,file_name);
  408.     printf("%s\n",full_file_name);
  409.     fprintf(list_handle,"%s\n",full_file_name);
  410.     if(*(dir_space_pointer+0x10)==2)
  411.         list_files(full_file_name, list_handle);
  412.     dir_space_pointer+=0x14;
  413.     while(*(dir_space_pointer++)!=0);
  414.     while((int)dir_space_pointer%4 !=0)    dir_space_pointer++;
  415.     }
  416. }
  417. /* end of list_files() */
  418.  
  419. /* start of open_main_window */
  420. static void open_main_window(void)
  421. {
  422. dbox_showstatic(main_window);
  423. }
  424. /* end of open_main_window */
  425.  
  426. /* start of end_backup */
  427. static void end_backup(void)
  428. {
  429. /* activate/deactivate the required buttons */
  430. dbox_unfadefield(main_window, MAIN_WINDOW_go);
  431. dbox_unfadefield(main_window, MAIN_WINDOW_floppy_fname);
  432. dbox_unfadefield(main_window, MAIN_WINDOW_inc_dirs);
  433. dbox_unfadefield(main_window, MAIN_WINDOW_inc_disc_name);
  434. dbox_fadefield(main_window, MAIN_WINDOW_continue);
  435. dbox_fadefield(main_window, MAIN_WINDOW_abort);
  436.  
  437. /* close the include file */
  438. if(include_handle)
  439.     {
  440.     fclose(include_handle);
  441.     include_handle=0;
  442.     }
  443.  
  444. /* free the space used by the exclude file */
  445. if(exclude_file_pointer)
  446.     {
  447.     Mem_Free(&exclude_file_pointer);
  448.     exclude_file_pointer=0;
  449.     exclude_file_length=0;
  450.     }
  451.  
  452. /* dissallow null events */
  453. event_setmask(1);
  454.  
  455. /* set the state variable go dormant */
  456. state=STATE_no_backup;
  457. }
  458. /* end of end_backup */
  459.  
  460. /* start of load_next_file */
  461. static void load_next_file(void)
  462. {
  463. /* create variables */
  464. char next_file_name[255];    /* next filename read from the include list */
  465. _kernel_swi_regs regs,file_info;
  466. _kernel_oserror *error;
  467. char comment[100];    /* string for comments */
  468. int gp_count;    /* GP counter */
  469. char *filename_to_use;    /* the file name written to the floppy */
  470.  
  471. /* get next filename from include list */
  472. fgets(next_file_name, 254, include_handle);
  473.  
  474. /* remove any non-alpha characters from next_file_name */
  475. for(gp_count=0; next_file_name[gp_count]>32; gp_count++);
  476. next_file_name[gp_count]=0;
  477.  
  478. if(feof(include_handle))
  479.     {
  480.     dbox_setfield(main_window,MAIN_WINDOW_comment,"Backup completed");
  481.     end_backup();
  482.     return;
  483.     }
  484.  
  485. dbox_setfield(main_window, MAIN_WINDOW_source_fname, next_file_name);
  486. if(exclude_file_pointer)
  487.     if(in_exclude_list(next_file_name))
  488.         {
  489.         dbox_setfield(main_window, MAIN_WINDOW_comment, "File Excluded");
  490.         return;
  491.         }
  492.  
  493. /* get info for source file */
  494. regs.r[0]=5; /* read catalog info */
  495. regs.r[1]=(int)next_file_name;
  496. error = _kernel_swi(OS_File, ®s, &file_info);
  497. if(error)
  498.     werr(1,"Error %d in OS_File in load_next_file(). %s",error->errnum,error->errmess);
  499.  
  500. switch(file_info.r[0])
  501.     {
  502.     case 0:    /* file not found */
  503.         dbox_setfield(main_window, MAIN_WINDOW_comment, "Not Found");
  504.         return;
  505.         break;
  506.     case 1:    /* file found ok */
  507.         dbox_setfield(main_window, MAIN_WINDOW_comment, "\0");
  508.         original_data_length=file_info.r[4];
  509.         dbox_setnumeric(main_window, MAIN_WINDOW_osize, original_data_length);
  510.  
  511.         /* allocate memory for the source file */
  512.         Mem_Alloc(&original_data, original_data_length + EXTRA_WORK_SPACE);
  513.         if(!original_data)
  514.             werr(1,"Unable to allocate %d bytes for original data",original_data_length);
  515.  
  516.         /* load file into memory */
  517.         regs.r[0]=0xff; /* load file */
  518.         regs.r[1]=(int)next_file_name;
  519.         regs.r[2]=(int)original_data;
  520.         regs.r[3]=0;
  521.         error=_kernel_swi(OS_File,®s,®s);
  522.         if(error)
  523.             werr(1,"Error %d in OS_File 0xff in load_next_file, %s.",error->errnum,error->errmess);
  524.  
  525.         /* fill header block */
  526.         /*
  527.         0:    compressed length including header (gets written later)
  528.         4:    load address r2
  529.         8:    execute address r3
  530.         12:    uncompressed length r4
  531.         16:    file attributes r5
  532.         20:    object type r0 (probably not needed, but it's here anyway)
  533.         24:    full file name (0 terminated)
  534.         */
  535.  
  536.         *(int *)(header_block+4)=file_info.r[2];
  537.         *(int *)(header_block+8)=file_info.r[3];
  538.         *(int *)(header_block+12)=file_info.r[4];
  539.         *(int *)(header_block+16)=file_info.r[5];
  540.         *(int *)(header_block+20)=file_info.r[0];
  541.  
  542.         /* chop the disc name off the full file name if required */
  543.         filename_to_use=next_file_name;
  544.         if(!dbox_getnumeric(main_window,MAIN_WINDOW_inc_disc_name) )
  545.             while(*filename_to_use != '$')
  546.                 filename_to_use++;
  547.         strncpy((header_block+24),filename_to_use,(FILE_HEADER_LENGTH-25));
  548.  
  549.         state=STATE_compress;
  550.         break;
  551.     case 2:    /* found a directory */
  552.         if(!dbox_getnumeric(main_window, MAIN_WINDOW_inc_dirs) )
  553.             {
  554.             state=STATE_load;
  555.             return;
  556.             }
  557.         dbox_setfield(main_window, MAIN_WINDOW_comment, "Directory");
  558.  
  559.         /* fill header block */
  560.         /*
  561.         0:    compressed length including header (0 for a directory)
  562.         4:    load address r2
  563.         8:    execute address r3
  564.         12:    uncompressed length r4 (0 for a directory)
  565.         16:    file attributes r5
  566.         20:    object type r0 (probably not needed, but it's here anyway)
  567.         24:    full file name (0 terminated)
  568.         */
  569.  
  570.         *(int *)(header_block+0)=0;
  571.         *(int *)(header_block+4)=file_info.r[2];
  572.         *(int *)(header_block+8)=file_info.r[3];
  573.         *(int *)(header_block+12)=0;
  574.         *(int *)(header_block+16)=file_info.r[5];
  575.         *(int *)(header_block+20)=file_info.r[0];
  576.  
  577.         /* chop the disc name off the full file name if required */
  578.         filename_to_use=next_file_name;
  579.         if(!dbox_getnumeric(main_window,MAIN_WINDOW_inc_disc_name) )
  580.             while(*filename_to_use != '$')
  581.                 filename_to_use++;
  582.         strncpy((header_block+24),filename_to_use,(FILE_HEADER_LENGTH-25));
  583.  
  584.         state=STATE_write_directory;
  585.         break;
  586.     default:
  587.         sprintf(comment,"%s is of unknown type %d\n",next_file_name,file_info.r[0]);
  588.         dbox_setfield(main_window, MAIN_WINDOW_comment, comment);
  589.         break;
  590.     }
  591. }
  592. /* end of load_next_file */
  593.  
  594. /* start of compress_next_file */
  595. static void compress_next_file(void)
  596. {
  597. /* create variables */
  598. _kernel_swi_regs regs;
  599. _kernel_oserror *error;
  600.  
  601. /* allocate memory to compress file into */
  602. Mem_Alloc(&compressed_data, original_data_length * 3/2 + EXTRA_WORK_SPACE);
  603. if(!compressed_data)
  604.     werr(1,"Unable to allocate %d bytes for compressed data",original_data_length * 3/2 + EXTRA_WORK_SPACE);
  605.  
  606. /* compress the file into memory */
  607. regs.r[0]=3; /* source and destination in memory */
  608. regs.r[1]=(int)original_data;
  609. regs.r[2]=(int)compressed_data;
  610. regs.r[3]=original_data_length;
  611. regs.r[4]=original_data_length * 3/2 + EXTRA_WORK_SPACE;
  612.  
  613. hglass_on();
  614. hglass_leds(HGLASS_LEDS_top);
  615. error=_kernel_swi(LZW_Compress, ®s, ®s);
  616. hglass_off();
  617. if(error)
  618.     werr(1,"Error %d in LZW_Compress in compress_next_file. %s",error->errnum, error->errmess);
  619.  
  620. compressed_data_length=regs.r[5];
  621. dbox_setnumeric(main_window, MAIN_WINDOW_csize, compressed_data_length);
  622. if(compressed_data_length >= original_data_length)
  623.     {
  624.     /* announce the fact */
  625.     dbox_setfield(main_window, MAIN_WINDOW_comment, "The compressed version is longer than the original");
  626.  
  627.     /* set floppy pointer and length to the original data */
  628.     floppy_data=original_data;
  629.     floppy_data_length=original_data_length;
  630.     }
  631. else
  632.     {
  633.     /* set floppy_pointer and length to the compressed data */
  634.     floppy_data=compressed_data;
  635.     floppy_data_length=compressed_data_length;
  636.     }
  637.  
  638. *(int *)header_block=floppy_data_length;
  639. }
  640. /* end of compress_next_file */
  641.  
  642. /* start of prompt_for_floppy */
  643. static int prompt_for_floppy(void)
  644. {
  645. /* create variables */
  646. int quit,button;
  647.  
  648. /* enable/disable the required buttons */
  649. dbox_unfadefield(main_window, MAIN_WINDOW_abort);
  650. dbox_unfadefield(main_window, MAIN_WINDOW_continue);
  651. dbox_fadefield(main_window, MAIN_WINDOW_go);
  652. dbox_fadefield(main_window, MAIN_WINDOW_inc_dirs);
  653. dbox_fadefield(main_window, MAIN_WINDOW_inc_disc_name);
  654.  
  655. open_main_window();
  656. dbox_setfield(main_window, MAIN_WINDOW_comment, "Insert new floppy disc in drive 0");
  657. bbc_vdu(7);
  658.  
  659. dbox_eventhandler(main_window,0,0);
  660. quit=FALSE;
  661. while(!quit)
  662.     {
  663.     button=(int)dbox_fillin(main_window);
  664.     switch(button)
  665.         {
  666.         case MAIN_WINDOW_continue:
  667.         case MAIN_WINDOW_abort:
  668.             quit=TRUE;
  669.             break;
  670.         /* default: */
  671.             /* not interested in any other buttons */
  672.         }
  673.     }
  674. dbox_eventhandler(main_window,main_window_handler,0);
  675.  
  676. /* disable continue button */
  677. dbox_fadefield(main_window, MAIN_WINDOW_continue);
  678. return(button);
  679. }
  680. /* end of prompt_for_floppy */
  681.  
  682. /* start of write_next_file */
  683. static void write_next_file(void)
  684. {
  685. /* create variables */
  686. FILE *floppy_handle;    /* handle of the file on the floppy */
  687. int space_on_floppy;    /* space left on the floppy */
  688. char floppy_full_name[255];    /* complete name of the floppy file */
  689. int count;    /* GP counter for writing stuff, byte at a time */
  690. char *gp_count;    /* GP char pointer for writing the compressed file */
  691.  
  692. space_on_floppy=floppy_free_space();
  693.  
  694. /* wait for a floppy with enough free space */
  695. while(space_on_floppy < (floppy_data_length+24+strlen(header_block+24)))
  696.     {
  697.     do    {
  698.         if(prompt_for_floppy()==MAIN_WINDOW_abort)
  699.             {
  700.             abort_button_clicked();
  701.             return;
  702.             }
  703.         }    while(!erase_floppy());
  704.     floppy_count++;
  705.  
  706.     /* create the empty file */
  707.     sprintf(floppy_full_name,"ADFS::0.$.%s_%0.2d",floppy_file_name,floppy_count);
  708.     create_empty_file(floppy_full_name);
  709.  
  710.     space_on_floppy=floppy_free_space();
  711.     }
  712.  
  713. sprintf(floppy_full_name,"ADFS::0.$.%s_%0.2d",floppy_file_name,floppy_count);
  714.  
  715. dbox_setnumeric(main_window, MAIN_WINDOW_fspace, space_on_floppy);
  716.  
  717. /* open floppy file for appending */
  718. floppy_handle=fopen(floppy_full_name,"a");
  719. if(!floppy_handle)
  720.     werr(1,"Unable to open %s for appending",floppy_full_name);
  721.  
  722. /* append header to floppy */
  723. for(count=0; count<24; fputc(header_block[count++], floppy_handle));    /* file info */
  724. fputs(header_block+24, floppy_handle);    /* file name */
  725. fputc(0, floppy_handle);    /* terminating null */
  726.  
  727. /* append file to floppy */
  728. hglass_on();
  729. hglass_leds(HGLASS_LEDS_bottom);
  730. gp_count=floppy_data;
  731. for(count=0; count<floppy_data_length; count++, gp_count++)
  732.     fputc(*gp_count,floppy_handle);
  733.  
  734. /* close floppy file */
  735. fclose(floppy_handle);
  736. hglass_off();
  737.  
  738. /* free memory */
  739. Mem_Free(&original_data);
  740. Mem_Free(&compressed_data);
  741. compressed_data=0;
  742. original_data=0;
  743. compressed_data_length=0;
  744. original_data_length=0;
  745. }
  746. /* end of write_next_file */
  747.  
  748. /* start of in_exclude_list */
  749. int in_exclude_list(char *f_name)
  750. {
  751. /* create variables */
  752. char *exclude_file_place=exclude_file_pointer;    /* pointer to current place in exclude file in memory */
  753. char name_in_list[255];    /* file name extracted from list */
  754. int  count;    /* GP counter */
  755. char file_name[255];    /* local copy of the file name */
  756.  
  757. /* if there is no list, return FALSE */
  758. if(!exclude_file_pointer)
  759.     return(FALSE);
  760.  
  761. strcpy(file_name,f_name);
  762.  
  763. while((int)exclude_file_place<(int)exclude_file_pointer+exclude_file_length-1)
  764.     {
  765.     /* advance to next alpha-numeric character */
  766.     for(; *exclude_file_place<33 || *exclude_file_place>126; exclude_file_place++);
  767.  
  768.     /* get name from list into name_in_list */
  769.     for(count=0; *exclude_file_place>31; name_in_list[count++]=*exclude_file_place++);
  770.     name_in_list[count]=0;
  771.  
  772.     /* strip trailing * from name_in_list */
  773.     for(count=0; name_in_list[count] && name_in_list[count]!='*'; count++);
  774.     name_in_list[count]=0;
  775.  
  776. /*
  777.     werr(0,"name_in_list= %s",name_in_list);
  778.     werr(0,"file_name= %s",file_name);
  779. */
  780.  
  781.     /* do the comparison */
  782. /*     if(!strncmp(name_in_list, file_name, strlen(name_in_list) ) )  */
  783.     file_name[strlen(name_in_list)]=0;
  784.     if(!strcmp(name_in_list, file_name) )
  785.         {
  786. /*        werr(0,"returning TRUE");  */
  787.         return(TRUE);
  788.         }
  789.  
  790.     }
  791. /* werr(0,"returning FALSE");  */
  792. return(FALSE);
  793. }
  794. /* end of in_exclude_list */
  795.  
  796. /* start of write_next_directory */
  797. static void write_next_directory(void)
  798. {
  799. /* create variables */
  800. FILE *floppy_handle;    /* handle of the file on the floppy */
  801. int space_on_floppy;    /* space left on the floppy */
  802. char floppy_full_name[255];    /* complete name of the floppy file */
  803. int count;    /* GP counter for writing stuff, byte at a time */
  804.  
  805. space_on_floppy=floppy_free_space();
  806.  
  807. /* wait for a floppy with enough free space */
  808. while(space_on_floppy < (24+strlen(header_block+24)))
  809.     {
  810.     do    {
  811.         if(prompt_for_floppy()==MAIN_WINDOW_abort)
  812.             {
  813.             abort_button_clicked();
  814.             return;
  815.             }
  816.         }    while(!erase_floppy());
  817.     floppy_count++;
  818.  
  819.     /* create the empty file */
  820.     sprintf(floppy_full_name,"ADFS::0.$.%s_%0.2d",floppy_file_name,floppy_count);
  821.     create_empty_file(floppy_full_name);
  822.  
  823.     space_on_floppy=floppy_free_space();
  824.     }
  825.  
  826. sprintf(floppy_full_name,"ADFS::0.$.%s_%0.2d",floppy_file_name,floppy_count);
  827.  
  828. dbox_setnumeric(main_window, MAIN_WINDOW_fspace, space_on_floppy);
  829.  
  830. /* open floppy file for appending */
  831. floppy_handle=fopen(floppy_full_name,"a");
  832. if(!floppy_handle)
  833.     werr(1,"Unable to open %s for appending",floppy_full_name);
  834.  
  835. /* append header to floppy */
  836. for(count=0; count<24; fputc(header_block[count++], floppy_handle));    /* file info */
  837. fputs(header_block+24, floppy_handle);    /* file name */
  838. fputc(0, floppy_handle);    /* terminating null */
  839.  
  840. /* close floppy file */
  841. fclose(floppy_handle);
  842.  
  843. }
  844. /* end of write_next_directory */
  845.  
  846. /* start of ibar_menu */
  847. static void ibar_menu(void *handle, char *hit)
  848. {
  849. switch(hit[0])
  850.     {
  851.     case 1:
  852.         show_info_box();
  853.         break;
  854.     case 2:
  855.         wimp_starttask("taskwindow \"type <LZWbackup$include>\" -name \"LZWbackup include\" -wimpslot 32K -quit");
  856.         break;
  857.     case 3:
  858.         wimp_starttask("taskwindow \"type <LZWbackup$exclude>\" -name \"LZWbackup exclude\" -wimpslot 32K -quit");
  859.         break;
  860.     case 4:
  861.         _kernel_oscli("wipe <LZWbackup$include> ~cf~r~v");
  862.         break;
  863.     case 5:
  864.         _kernel_oscli("wipe <LZWbackup$exclude> ~cf~r~v");
  865.         break;
  866.     case 6:
  867.         open_main_window();
  868.         break;
  869.     case 7:
  870.         exit(0);
  871.         break;
  872.     }
  873. }
  874. /* end of ibar_menu */
  875.  
  876. /* start of floppy_free_space */
  877. static int floppy_free_space(void)
  878. {
  879. /* create variables */
  880. _kernel_swi_regs regs;
  881. _kernel_oserror *error;
  882.  
  883. regs.r[0]=(int)"0";
  884. error=_kernel_swi(ADFS_FreeSpace, ®s, ®s);
  885. if(error)
  886.     werr(1,"Error %d in ADFS_FreeSpace in floppy_free_space(). %s",error->errnum, error->errmess);
  887.  
  888. return(regs.r[0]);
  889. }
  890. /* end of floppy_free_space */
  891.  
  892. /* start of erase_floppy */
  893. static int erase_floppy(void)
  894. {
  895. /* create variables */
  896. _kernel_swi_regs regs;
  897. _kernel_oserror *error;
  898.  
  899. dbox_setfield(main_window,9,"Erasing Drive 0.");
  900. regs.r[0]=(int)"wipe ADFS::0.$.* ~cfr~v \n";
  901. error = _kernel_swi(OS_CLI, ®s, ®s);
  902. if(error)
  903.     {
  904.     switch(error->errnum)
  905.         {
  906.         case 1046: /* nothing to delete */                 
  907.         
  908.             /* so just exit */
  909.             return(TRUE);
  910.             break;
  911.         default:
  912.             werr(0,"Error %d in OS_CLI in erase_floppy(). %s",error->errnum, error->errmess);
  913.             return(FALSE);
  914.             break;
  915.         }
  916.     }
  917. return(TRUE);
  918. }
  919. /* end of erase_floppy */
  920.  
  921. /* start of create_empty_file */
  922. static void create_empty_file(char *file_name)
  923. {
  924. /* create variables */
  925. _kernel_swi_regs regs;
  926. _kernel_oserror *error;
  927.  
  928. regs.r[0]=0x0b; /* create empty file */
  929. regs.r[1]=(int)file_name;
  930. regs.r[2]=BACKUP_FILE_TYPE;
  931. regs.r[4]=0;
  932. regs.r[5]=0;
  933. error = _kernel_swi(OS_File, ®s, ®s);
  934. if(error)
  935.     werr(1,"Error %d in OS_File in create_empty_file(). %s",error->errnum, error->errmess);
  936. }
  937. /* end of create_empty_file */
  938.  
  939. /* start of continue_button_clicked */
  940. static void continue_button_clicked(void)
  941. {
  942. if(state & STATE_wait_for_continue)
  943.     state&=(!STATE_wait_for_continue);
  944. }
  945. /* end of continue_button_clicked */
  946.  
  947.