home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w023 / 2.ddi / ALL.001 / NUMPIC.POC < prev    next >
Encoding:
Text File  |  1991-03-20  |  8.3 KB  |  331 lines

  1. /** 
  2.  **
  3.  **  NUMPIC.POC  -  Ways to convert a numbered sequence of picture files 
  4.  **  to a flic and back.  Also a way to delete the numbered sequence of
  5.  **  pictures when you're done.
  6.  **
  7.  **/
  8.  
  9.         /* Success means no error */
  10. #define Success 0
  11.  
  12.         /* Defines for Boolean values */
  13. #define TRUE 1
  14. #define FALSE 0
  15.  
  16.         /* This macro returns the number of elements in an array */
  17. #define ArrayEls(arr) (sizeof(arr)/sizeof((arr)[0]))
  18.  
  19. char *start_of_final_digits(char *pt)
  20. /***********************************************************************
  21.  *  returns pointer to the start of the ending number in the string.
  22.  ***********************************************************************/
  23. {
  24. char c;
  25. int len;
  26. int i;
  27.  
  28. i = len = strlen(pt);
  29. pt += len;
  30. while (--i >= 0)
  31.     {
  32.     c = *(--pt);
  33.     if (!(c >= '0' && c <= '9'))
  34.         return(pt+1);
  35.     }
  36. return(pt);
  37. }
  38.  
  39. int patoi(char *pt)
  40. /***********************************************************************
  41.  *  returns value of positive decimal number contained in string
  42.  ***********************************************************************/
  43. {
  44. char c;
  45. int acc = 0;
  46.  
  47. for (;;)
  48.     {
  49.     c = *pt++;
  50.     if (c >= '0' && c <= '9')
  51.         acc = acc*10+(c-'0');
  52.     else
  53.         return(acc);
  54.     }
  55. }
  56.  
  57. int get_num_name(char *name)
  58. /***********************************************************************
  59.  * Return the number embedded in the "file" portion of a file-name
  60.  * (as opposed to the directory or the suffix.)  Return -1 if none.
  61.  ***********************************************************************/
  62. {
  63. char *pt;
  64. char dev[4],dir[70],file[10],suff[5];
  65.  
  66. fnsplit(name,dev,dir,file,suff);
  67. if ((pt = start_of_final_digits(file)) == file)
  68.     return(-1);
  69. return(patoi(pt));
  70. }
  71.  
  72. int count_digits(int num)
  73. /***********************************************************************
  74.  * Return number of digits (base 10) in number.
  75.  ***********************************************************************/
  76. {
  77. int digits = 1;
  78.  
  79. while (num > 10)
  80.     {
  81.     ++digits;
  82.     num /= 10;
  83.     }
  84. return(digits);
  85. }
  86.  
  87. void make_num_name(char *buf, char *name, int num, int digits)
  88. /***********************************************************************
  89.  * Given a full file name (device, path, file, suffix), a number,
  90.  * and a minimum count of digits,  make a name with the number
  91.  * embedded in the last parts of the 'file' portion of the name.
  92.  * Parameters:
  93.  *        char *buf;        where to put the numbered name
  94.  *        char *name;        the original name.
  95.  *        int  num;        the number.
  96.  *        int  digits;    minimum amount of digits to use.
  97.  * Example:
  98.  *        make_num_name(buf, "c:\pics\sample.gif", 16, 3)
  99.  * leaves "c:\pics\sampl016.gif" in buf.
  100.  ***********************************************************************/
  101. {
  102. char dev[4],dir[70],file[10],suff[5];
  103. char nfile[10];
  104. int diglen;
  105. char *pt;
  106.  
  107. fnsplit(name,dev,dir,file,suff);    /* split file into component parts */
  108. if (count_digits(num)>digits)        /* if number too big override digits */
  109.     digits = count_digits(num);
  110. pt = start_of_final_digits(file);
  111. diglen = strlen(pt);        /* find out how many digits already in file name */
  112. if (diglen < digits)        /* if less than needed truncate file name */
  113.     {
  114.     file[8-digits] = 0;
  115.     diglen = digits;
  116.     }
  117. else                     /* Otherwise truncate at first digit*/                
  118.     {
  119.     *pt = 0;
  120.     }
  121. sprintf(nfile,"%s%0*d", file, diglen, num);
  122. fnmerge(buf,dev,dir,nfile,suff);
  123. }
  124.  
  125. save_pics(char *name)
  126. /***********************************************************************
  127.  * Save one picture for each frame in current flic.
  128.  ***********************************************************************/
  129. {
  130. int i;
  131. int count = GetFrameCount();
  132. char nbuf[80];
  133. ErrCode err;
  134. int min, max;
  135.  
  136. if ((min = get_num_name(name)) < 0)
  137.     min = 1;
  138. max = min + count - 1;
  139. for (i=min; i<=max; ++i)
  140.     {
  141.     make_num_name(nbuf,name,i,count_digits(max));    /* make up file name */
  142.     printf("Saving %s", nbuf);    /* Put up status line. */
  143.     if ((err = SavePic(nbuf)) < Success)
  144.         {
  145.         Qerror(err, "Couldn't save %s", nbuf);
  146.         return;
  147.         }
  148.     NextFrame();
  149.     }
  150. unprintf();    /* Get rid of status line */
  151. }
  152.  
  153. Boolean fexists(char *name)
  154. /***********************************************************************
  155.  * See whether a file exists.
  156.  ***********************************************************************/
  157. {
  158. FILE *f;
  159.  
  160. if ((f = fopen(name, "rb")) != NULL)
  161.     {
  162.     fclose(f);
  163.     return(TRUE);
  164.     }
  165. else
  166.     return(FALSE);
  167. }
  168.  
  169. int count_files(char *base_name)
  170. /***********************************************************************
  171.  * Count up the number of files starting at base_name and
  172.  * numbered sequentially.
  173.  ***********************************************************************/
  174. {
  175. char nbuf[80];
  176. int count = 0;
  177. int base = get_num_name(base_name);
  178.  
  179. for (;;)
  180.     {
  181.     make_num_name(nbuf, base_name,base+count,0);
  182.     if (!fexists(nbuf))
  183.         break;
  184.     ++count;    
  185.     }
  186. return(count);
  187. }
  188.  
  189. void load_pics(char *base_name)
  190. /***********************************************************************
  191.  * Load up a bunch of consecutively numbered files into the flic.
  192.  ***********************************************************************/
  193. {
  194. int i;
  195. int count;
  196. char nbuf[80];
  197. int base;
  198. ErrCode err;
  199.  
  200. if (!fexists(base_name))
  201.     {
  202.     Qerror(-1, "Couldn't find %s", base_name);
  203.     return;
  204.     }
  205. base = get_num_name(base_name);
  206. count = count_files(base_name);
  207. if (!Qquestion("Load %d pictures into flic?  (Will overwrite current flic!)", 
  208.     count))
  209.     return;
  210. make_num_name(nbuf,base_name,base,0);    /* Create name of first frame pic */
  211. if (Qquestion("Change flic size to fit pics?"))
  212.     {
  213.     err = LoadFlic(nbuf);    /* Loading pic as a flic will get the right dimensions */
  214.     }
  215. else
  216.     {
  217.     SetFrame(0);           /* Go to first frame */
  218.     err = LoadPic(nbuf);     /* And load it the normal way. */
  219.     }
  220. if (err < Success)
  221.     {
  222.     Qerror(err, "Can't load %s", nbuf);
  223.     return;
  224.     }
  225. SetFrameCount(count);        /* Make Flic frame count match # of pictures */
  226. for (i=1;i<count;++i)        /* Load the rest of the frames */
  227.     {
  228.     NextFrame();
  229.     make_num_name(nbuf,base_name,base+i,0);
  230.     printf("Loading %s", nbuf);    /* put up status line with our progress */
  231.     if ((err = LoadPic(nbuf)) < Success)
  232.         {
  233.         Qerror(err, "Can't load %s", nbuf);
  234.         return;
  235.         }
  236.     }
  237. unprintf();            /* get rid of top status info line */
  238. }
  239.  
  240. void delete_files(char *base_name)
  241. /***********************************************************************
  242.  * Delete a bunch of consecutively numbered files.
  243.  ***********************************************************************/
  244. {
  245. char nbuf[80];
  246. int count;
  247. int i;
  248. int base =  get_num_name(base_name);
  249.  
  250. count = count_files(base_name);
  251. if (!Qquestion("Delete numbered files starting with %s ???", base_name))
  252.     return;
  253. if (!Qquestion("Are you sure you want to delete all %d"
  254.                " files starting with:\n      %s?", count, base_name))
  255.     return;
  256. for (i=0;i<count;++i)
  257.     {
  258.     make_num_name(nbuf,base_name,base+i,0);
  259.     DosDelete(nbuf);
  260.     }
  261. }
  262.  
  263. char *menu_options[] = 
  264.     {
  265.     "Save Flic as Pics",
  266.     "Load Pics as Flic",
  267.     "Delete Numbered files",
  268.     "Exit",
  269.     };
  270.  
  271. Boolean do_one_set()
  272. /***********************************************************************
  273.  * Put up main program menu and respond to choices.  Return FALSE if
  274.  * user selects Cancel,  TRUE after other commands.
  275.  ***********************************************************************/
  276. {
  277. static char base_name[80];
  278. int choice;
  279. char *what;
  280.  
  281. choice = Qmenu(menu_options, ArrayEls(menu_options), 
  282.               "Convert from Flic to Pictures");
  283.  
  284.                         /* Figure out what to put on the file selector
  285.                          * action button */
  286. switch (choice)    
  287.     {
  288.     case 0:
  289.         return FALSE;    /* If use has chosen Cancel return all done */
  290.     case 1:
  291.         what = "Save";
  292.         break;
  293.     case 2:
  294.         what = "Load";
  295.         break;
  296.     case 3:
  297.         what = "Delete";
  298.         break;
  299.     }
  300.  
  301.                         /* Ask user for the base file name. */
  302. if (!Qfile(".*",what,base_name,base_name,FALSE,
  303.     "Base name of numbered pictures?"))
  304.     return TRUE;        /* On File menu cancel,
  305.                          * abort this command, but not the program */
  306.  
  307. switch (choice)
  308.     {
  309.     case 1:
  310.         save_pics(base_name);
  311.         break;
  312.     case 2:
  313.         load_pics(base_name);
  314.         break;
  315.     case 3:
  316.         delete_files(base_name);
  317.         break;
  318.     }
  319. return TRUE;
  320. }
  321.  
  322.  
  323. main()
  324. /***********************************************************************
  325.  * Call menu routine above until Cancel.
  326.  ***********************************************************************/
  327. {
  328. while (do_one_set())
  329.     ;
  330. }
  331.