⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sfllang.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 2 页
字号:

/*  ---------------------------------------------------------------------[<]-
    Function: get_accents

    Synopsis: Returns TRUE if accents are enabled, FALSE if not.
    ---------------------------------------------------------------------[>]-*/

Bool
get_accents (void)
{
    return (use_accents);
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_units_name

    Synopsis: Returns the name for the specified units, which is a value from
    zero to 19.  Accented characters are formatted according to the current
    accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_units_name (int units)
{
    ASSERT (units >= 0 && units <= 19);
    return (handle_accents (units_table [units]));
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_tens_name

    Synopsis: Returns the name for the specified tens, which is a value
    from 10 to 90; it is rounded as required.  Accented characters are
    formatted according to the current accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_tens_name (int tens)
{
    ASSERT (tens >= 10 && tens < 100);
    return (handle_accents (tens_table [tens / 10 - 1]));
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_day_name

    Synopsis: Returns the name for the specified day, which must be a
    value from 0 (Sunday) to 6 (Saturday).  Accented characters are
    formatted according to the current accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_day_name (int day)
{
    ASSERT (day >= 0 && day <= 6);
    return (handle_accents (day_table [day]));
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_day_abbrev

    Synopsis: Returns the abbreviation for the specified day, which must be
    a value from 0 (Sunday) to 6 (Saturday).  The abbreviation (3 letters)
    is converted into uppercase if the 'upper' argument is true.  Accented
    characters are formatted according to the current accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_day_abbrev (int day, Bool upper)
{
    char
        abbrev [4];

    ASSERT (day >= 0 && day <= 6);

    strncpy (abbrev, day_table [day], 3);
    abbrev [3] = '\0';
    if (upper)
        strupc (abbrev);
    return (handle_accents (abbrev));
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_month_name

    Synopsis: Returns the name for the specified month, which must be a
    value from 1 to 12.  Accented characters are handled as per the current
    accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_month_name (int month)
{
    ASSERT (month >= 1 && month <= 12);
    return (handle_accents (month_table [month - 1]));
}


/*  ---------------------------------------------------------------------[<]-
    Function: get_month_abbrev

    Synopsis: Returns the abbreviation for the specified month, which must
    be a value from 1 to 12.  The abbreviation (3 letters) is converted into
    uppercase if the 'upper' argument is true.  Accented characters are
    formatted according to the current accents setting.
    ---------------------------------------------------------------------[>]-*/

char *
get_month_abbrev (int month, Bool upper)
{
    char
        abbrev [4];

    ASSERT (month >= 1 && month <= 12);

    strncpy (abbrev, month_table [month - 1], 3);
    abbrev [3] = '\0';
    if (upper)
        strupc (abbrev);

    return (handle_accents (abbrev));
}


/*  handle_accents -- internal
 *
 *  Accepts string containing escaped accented characters and converts into
 *  local values.  Currently handles DOS and Unix character sets and this
 *  (reduced) set of accented characters:
 *
 *      /Uu     u umlaut
 *      /UU     U umlaut
 *      /Uo     o umlaut
 *      /UO     O umlaut
 *      /Ra     a ring
 *      /RA     A ring
 *      /So     slash o
 *      /SO     slash O
 *      /Bs     scharfes S
 *      /BS     scharfes S
 *      /Cu     circumflex u
 *      /CU     circumflex U
 */

static char *
handle_accents (char *string)
{
#if (defined (__UNIX__))
#   define CHAR_a_ring    '\345'
#   define CHAR_A_ring    '\305'
#   define CHAR_o_uml     '\366'
#   define CHAR_O_uml     '\326'
#   define CHAR_u_circ    '\373'
#   define CHAR_u_uml     '\374'
#   define CHAR_U_uml     '\334'
#elif (defined (__MSDOS__))
#   define CHAR_a_ring    '\206'
#   define CHAR_A_ring    '\217'
#   define CHAR_o_uml     '\224'
#   define CHAR_O_uml     '\231'
#   define CHAR_u_circ    '\226'
#   define CHAR_u_uml     '\201'
#   define CHAR_U_uml     '\232'
#else
#   define CHAR_a_ring    'a'
#   define CHAR_A_ring    'A'
#   define CHAR_o_uml     'o'
#   define CHAR_O_uml     'O'
#   define CHAR_u_circ    'u'
#   define CHAR_u_uml     'u'
#   define CHAR_U_uml     'U'
#endif
    return (string);                    /*  To be implemented                */
}


/*  ---------------------------------------------------------------------[<]-
    Function: timestamp_string

    Synopsis: Formats a timestamp according to a user-supplied pattern.  The
    result is returned in a buffer supplied by the caller; if this argument
    is NULL, allocates a buffer and returns that (the caller must then free
    the buffer using mem_free()).  The pattern consists of arbitrary text
    mixed with insertion symbols indicated by '%':
    <TABLE>
        %y        day of year, 001-366
        %yy       year 2 digits, 00-99
        %yyyy     year 4 digits, 0100-9999
        %mm       month, 01-12
        %mmm      month, Jan
        %mmmm     month, January
        %MMM      month, JAN
        %MMMM     month, JANUARY
        %dd       day, 01-31
        %ddd      day of week, Sun
        %dddd     day of week, Sunday
        %DDD      day of week, SUN
        %DDDD     day of week, SUNDAY
        %w        day of week, 1-7 (1=Sunday)
        %ww       week of year, 01-53
        %q        year quarter, 1-4
        %h        hour, 00-23
        %m        minute, 00-59
        %s        second, 00-59
        %c        centisecond, 00-99
        %%        literal character %
    </TABLE>
    ---------------------------------------------------------------------[>]-*/

char *
timestamp_string (char *buffer, const char *pattern)
{
    long
        date,                           /*  Current date                     */
        time;                           /*    and time                       */
    int
        century,                        /*  Century component of date        */
        year,                           /*  Year component of date           */
        month,                          /*  Month component of date          */
        day,                            /*  Day component of date            */
        hour,                           /*  Hour component of time           */
        minute,                         /*  Minute component of time         */
        second,                         /*  Second component of time         */
        centi,                          /*  1/100 sec component of time      */
        cursize;                        /*  Size of current component        */
    char
       *dest,                           /*  Store formatted data here        */
        ch;                             /*  Next character in picture        */

    date = date_now ();
    time = time_now ();

    century = GET_CENTURY (date);
    year    = GET_YEAR    (date);
    month   = GET_MONTH   (date);
    day     = GET_DAY     (date);
    hour    = GET_HOUR    (time);
    minute  = GET_MINUTE  (time);
    second  = GET_SECOND  (time);
    centi   = GET_CENTI   (time);

    if (buffer == NULL)
        buffer = mem_alloc (strlen (pattern) * 2);

    /*  Scan through picture, converting each component                      */
    dest = buffer;
    *dest = 0;                          /*  String is empty                  */
    while (*pattern)
      {
        ch = *pattern++;
        if (ch == '%' && *pattern)
          {
            ch = *pattern++;            /*  Count size of pattern after %    */
            for (cursize = 1; *pattern == ch; cursize++)
                pattern++;
          }
        else
          {
            *dest++ = ch;               /*  Something else - store and next  */
            *dest = 0;                  /*  Terminate the string nicely      */
            continue;
          }

        /*  Now process pattern itself                                       */
        switch (ch)
          {
            case 'y':
                if (cursize == 1)       /*  y     day of year, 001-366       */
                    sprintf (dest, "%03d", julian_date (date));
                else
                if (cursize == 2)       /*  yy    year 2 digits, 00-99       */
                    sprintf (dest, "%02d", year);
                else
                if (cursize == 4)       /*  yyyy  year 4 digits, 0100-9999   */
                    sprintf (dest, "%02d%02d", century, year);
                break;

            case 'm':
                if (cursize == 1)       /*  m     minute, 00-59              */
                    sprintf (dest, "%02d", minute);
                else
                if (cursize == 2)       /*  mm    month, 01-12               */
                    sprintf (dest, "%02d", month);
                else
                if (cursize == 3)       /*  mmm   month, 3 letters           */
                    strcpy (dest, get_month_abbrev (month, FALSE));
                else
                if (cursize == 4)       /*  mmmm  month, full name           */
                    strcpy (dest, get_month_name (month));
                break;

            case 'M':
                if (cursize == 3)       /*  MMM   month, 3-letters, ucase    */
                    strcpy (dest, get_month_abbrev (month, TRUE));
                else
                if (cursize == 4)       /*  MMMM  month, full name, ucase    */
                  {
                    strcpy (dest, get_month_name (month));
                    strupc (dest);
                  }
                break;

            case 'd':
                if (cursize == 2)       /*  dd    day, 01-31                 */
                    sprintf (dest, "%02d", day);
                else
                if (cursize == 3)       /*  ddd   day of week, Sun           */
                    strcpy (dest, get_day_abbrev (day_of_week (date), FALSE));
                else
                if (cursize == 4)       /*  dddd  day of week, Sunday        */
                    strcpy (dest, get_day_name (day_of_week (date)));
                break;

            case 'D':
                if (cursize == 3)       /*  DDD   day of week, SUN           */
                    strcpy (dest, get_day_abbrev (day_of_week (date), TRUE));
                else
                if (cursize == 4)       /*  DDDD  day of week, SUNDAY        */
                  {
                    strcpy (dest, get_day_name (day_of_week (date)));
                    strupc (dest);
                  }
                break;

            case 'w':
                if (cursize == 1)       /*  w     day of week, 1-7 (1=Sun)   */
                    sprintf (dest, "%d", day_of_week (date) + 1);
                else
                if (cursize == 2)       /*  ww    week of year, 01-53        */
                    sprintf (dest, "%d", week_of_year (date));
                break;

            case 'q':
                if (cursize == 1)       /*  q     year quarter, 1-4          */
                    sprintf (dest, "%d", year_quarter (date));
                break;

            case 'h':
                if (cursize == 1)       /*  h     hour, 00-23                */
                    sprintf (dest, "%02d", hour);
                break;

            case 's':
                if (cursize == 1)       /*  s     second, 00-59              */
                    sprintf (dest, "%02d", second);
                break;

            case 'c':
                if (cursize == 1)       /*  c     centisecond, 00-99         */
                    sprintf (dest, "%02d", centi);
                break;

            case '%':
                if (cursize == 1)       /*  %     literal '%'                */
                    strcpy (dest, "%");
                break;
        }
        if (*dest)                      /*  If something was output,         */
            while (*dest)               /*    skip to end of string          */
                dest++;
        else
          {
            while (cursize--)           /*  Else output ch once or more      */
                *dest++ = ch;           /*    and bump dest pointer          */
            *dest = 0;                  /*  Terminate the string nicely      */
          }
    }
    return (buffer);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -