home *** CD-ROM | disk | FTP | other *** search
- /**
- **
- ** NUMPIC.POC - Ways to convert a numbered sequence of picture files
- ** to a flic and back. Also a way to delete the numbered sequence of
- ** pictures when you're done.
- **
- **/
-
- /* Success means no error */
- #define Success 0
-
- /* Defines for Boolean values */
- #define TRUE 1
- #define FALSE 0
-
- /* This macro returns the number of elements in an array */
- #define ArrayEls(arr) (sizeof(arr)/sizeof((arr)[0]))
-
- char *start_of_final_digits(char *pt)
- /***********************************************************************
- * returns pointer to the start of the ending number in the string.
- ***********************************************************************/
- {
- char c;
- int len;
- int i;
-
- i = len = strlen(pt);
- pt += len;
- while (--i >= 0)
- {
- c = *(--pt);
- if (!(c >= '0' && c <= '9'))
- return(pt+1);
- }
- return(pt);
- }
-
- int patoi(char *pt)
- /***********************************************************************
- * returns value of positive decimal number contained in string
- ***********************************************************************/
- {
- char c;
- int acc = 0;
-
- for (;;)
- {
- c = *pt++;
- if (c >= '0' && c <= '9')
- acc = acc*10+(c-'0');
- else
- return(acc);
- }
- }
-
- int get_num_name(char *name)
- /***********************************************************************
- * Return the number embedded in the "file" portion of a file-name
- * (as opposed to the directory or the suffix.) Return -1 if none.
- ***********************************************************************/
- {
- char *pt;
- char dev[4],dir[70],file[10],suff[5];
-
- fnsplit(name,dev,dir,file,suff);
- if ((pt = start_of_final_digits(file)) == file)
- return(-1);
- return(patoi(pt));
- }
-
- int count_digits(int num)
- /***********************************************************************
- * Return number of digits (base 10) in number.
- ***********************************************************************/
- {
- int digits = 1;
-
- while (num > 10)
- {
- ++digits;
- num /= 10;
- }
- return(digits);
- }
-
- void make_num_name(char *buf, char *name, int num, int digits)
- /***********************************************************************
- * Given a full file name (device, path, file, suffix), a number,
- * and a minimum count of digits, make a name with the number
- * embedded in the last parts of the 'file' portion of the name.
- * Parameters:
- * char *buf; where to put the numbered name
- * char *name; the original name.
- * int num; the number.
- * int digits; minimum amount of digits to use.
- * Example:
- * make_num_name(buf, "c:\pics\sample.gif", 16, 3)
- * leaves "c:\pics\sampl016.gif" in buf.
- ***********************************************************************/
- {
- char dev[4],dir[70],file[10],suff[5];
- char nfile[10];
- int diglen;
- char *pt;
-
- fnsplit(name,dev,dir,file,suff); /* split file into component parts */
- if (count_digits(num)>digits) /* if number too big override digits */
- digits = count_digits(num);
- pt = start_of_final_digits(file);
- diglen = strlen(pt); /* find out how many digits already in file name */
- if (diglen < digits) /* if less than needed truncate file name */
- {
- file[8-digits] = 0;
- diglen = digits;
- }
- else /* Otherwise truncate at first digit*/
- {
- *pt = 0;
- }
- sprintf(nfile,"%s%0*d", file, diglen, num);
- fnmerge(buf,dev,dir,nfile,suff);
- }
-
- save_pics(char *name)
- /***********************************************************************
- * Save one picture for each frame in current flic.
- ***********************************************************************/
- {
- int i;
- int count = GetFrameCount();
- char nbuf[80];
- ErrCode err;
- int min, max;
-
- if ((min = get_num_name(name)) < 0)
- min = 1;
- max = min + count - 1;
- for (i=min; i<=max; ++i)
- {
- make_num_name(nbuf,name,i,count_digits(max)); /* make up file name */
- printf("Saving %s", nbuf); /* Put up status line. */
- if ((err = SavePic(nbuf)) < Success)
- {
- Qerror(err, "Couldn't save %s", nbuf);
- return;
- }
- NextFrame();
- }
- unprintf(); /* Get rid of status line */
- }
-
- Boolean fexists(char *name)
- /***********************************************************************
- * See whether a file exists.
- ***********************************************************************/
- {
- FILE *f;
-
- if ((f = fopen(name, "rb")) != NULL)
- {
- fclose(f);
- return(TRUE);
- }
- else
- return(FALSE);
- }
-
- int count_files(char *base_name)
- /***********************************************************************
- * Count up the number of files starting at base_name and
- * numbered sequentially.
- ***********************************************************************/
- {
- char nbuf[80];
- int count = 0;
- int base = get_num_name(base_name);
-
- for (;;)
- {
- make_num_name(nbuf, base_name,base+count,0);
- if (!fexists(nbuf))
- break;
- ++count;
- }
- return(count);
- }
-
- void load_pics(char *base_name)
- /***********************************************************************
- * Load up a bunch of consecutively numbered files into the flic.
- ***********************************************************************/
- {
- int i;
- int count;
- char nbuf[80];
- int base;
- ErrCode err;
-
- if (!fexists(base_name))
- {
- Qerror(-1, "Couldn't find %s", base_name);
- return;
- }
- base = get_num_name(base_name);
- count = count_files(base_name);
- if (!Qquestion("Load %d pictures into flic? (Will overwrite current flic!)",
- count))
- return;
- make_num_name(nbuf,base_name,base,0); /* Create name of first frame pic */
- if (Qquestion("Change flic size to fit pics?"))
- {
- err = LoadFlic(nbuf); /* Loading pic as a flic will get the right dimensions */
- }
- else
- {
- SetFrame(0); /* Go to first frame */
- err = LoadPic(nbuf); /* And load it the normal way. */
- }
- if (err < Success)
- {
- Qerror(err, "Can't load %s", nbuf);
- return;
- }
- SetFrameCount(count); /* Make Flic frame count match # of pictures */
- for (i=1;i<count;++i) /* Load the rest of the frames */
- {
- NextFrame();
- make_num_name(nbuf,base_name,base+i,0);
- printf("Loading %s", nbuf); /* put up status line with our progress */
- if ((err = LoadPic(nbuf)) < Success)
- {
- Qerror(err, "Can't load %s", nbuf);
- return;
- }
- }
- unprintf(); /* get rid of top status info line */
- }
-
- void delete_files(char *base_name)
- /***********************************************************************
- * Delete a bunch of consecutively numbered files.
- ***********************************************************************/
- {
- char nbuf[80];
- int count;
- int i;
- int base = get_num_name(base_name);
-
- count = count_files(base_name);
- if (!Qquestion("Delete numbered files starting with %s ???", base_name))
- return;
- if (!Qquestion("Are you sure you want to delete all %d"
- " files starting with:\n %s?", count, base_name))
- return;
- for (i=0;i<count;++i)
- {
- make_num_name(nbuf,base_name,base+i,0);
- DosDelete(nbuf);
- }
- }
-
- char *menu_options[] =
- {
- "Save Flic as Pics",
- "Load Pics as Flic",
- "Delete Numbered files",
- "Exit",
- };
-
- Boolean do_one_set()
- /***********************************************************************
- * Put up main program menu and respond to choices. Return FALSE if
- * user selects Cancel, TRUE after other commands.
- ***********************************************************************/
- {
- static char base_name[80];
- int choice;
- char *what;
-
- choice = Qmenu(menu_options, ArrayEls(menu_options),
- "Convert from Flic to Pictures");
-
- /* Figure out what to put on the file selector
- * action button */
- switch (choice)
- {
- case 0:
- return FALSE; /* If use has chosen Cancel return all done */
- case 1:
- what = "Save";
- break;
- case 2:
- what = "Load";
- break;
- case 3:
- what = "Delete";
- break;
- }
-
- /* Ask user for the base file name. */
- if (!Qfile(".*",what,base_name,base_name,FALSE,
- "Base name of numbered pictures?"))
- return TRUE; /* On File menu cancel,
- * abort this command, but not the program */
-
- switch (choice)
- {
- case 1:
- save_pics(base_name);
- break;
- case 2:
- load_pics(base_name);
- break;
- case 3:
- delete_files(base_name);
- break;
- }
- return TRUE;
- }
-
-
- main()
- /***********************************************************************
- * Call menu routine above until Cancel.
- ***********************************************************************/
- {
- while (do_one_set())
- ;
- }
-