| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

file_cycle

#include "sflfile.h"
Bool
file_cycle (
    const char *filename,
    int how)

Synopsis

Cycles the file: if the file already exists, renames the existing file. This function tries to rename the file using the date of creation of the file; if this fails because an existing file had the same name, generates a guaranteed unique file name. Returns TRUE if the cycle operation succeeded, or FALSE if it failed (e.g. due to a protection problem). The how argument must be one of:
CYCLE ALWAYS Cycle file unconditionally
CYCLE HOURLY Cycle file if hour has changed
CYCLE DAILY Cycle file if day has changed
CYCLE WEEKLY Cycle file if week has changed
CYCLE MONTHLY Cycle file if month has changed
CYCLE NEVER Don't cycle the file

Source Code - (sflfile.c)

{
    long
        file_date;                      /*  Datestamp of file                */
    char
        *point,
        *insert_at;                     /*  Where we start messing name      */
    int
        unique_nbr;                     /*  To generate a unique name        */

    ASSERT (filename);

    /*  If no cycling needed, do nothing                                     */
    if (!file cycle needed (filename, how))
        return (TRUE);                  /*  No errors, nothing in fact       */

    file_date = timer to date (get file time (filename));
    strcpy (full_name, filename);
    point = strrchr (full_name, '.');
    if (point)
      {
        strcpy (work_name, point);      /*  Save extension, if any           */
        *point = '\0';                  /*    and truncate original name     */
      }
    else
        strclr (work_name);

    /*  We leave up to 2 original letters of the filename, then stick-in     */
    /*  the 6-digit timestamp.                                               */
    insert_at = strrchr (full_name, '/');
#if (defined (MSDOS_FILESYSTEM))
    if (!insert_at)
        insert_at = strrchr (full_name, '\\');
#endif
    if (insert_at)
        insert_at++;                    /*  Bump past slash, if found        */
    else
        insert_at = full_name;

    if (*insert_at)                     /*  Bump insert_at twice, to leave   */
        insert_at++;                    /*    up to 2 letters before we      */
    if (*insert_at)                     /*    stick-in the date stamp        */
        insert_at++;

    /*  Format new name for file and make sure it does not already exist     */
    sprintf (insert_at, "%06d", (int) (file_date % 1000000L));
    strcat  (insert_at, work_name);
    if (file exists (full_name))
      {
        point = strrchr (full_name, '.') + 1;
        for (unique_nbr = 0; unique_nbr < 1000; unique_nbr++)
          {
            sprintf (point, "%03d", unique_nbr);
            if (!file exists (full_name))
                break;
          }
      }
    if (file exists (full_name))
        return (FALSE);                 /*  We give up!                      */

    if (file rename (filename, full_name))
        return (FALSE);                 /*  No permission                    */
    else
        return (TRUE);                  /*  Okay, it worked                  */
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation