Changeset 4c8f5e7 in mainline for uspace/lib/posix/fnmatch.c


Ignore:
Timestamp:
2011-08-17T17:42:36Z (14 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4419c34
Parents:
903bd436
Message:

Documentation and minor changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/fnmatch.c

    r903bd436 r4c8f5e7  
    3333 */
    3434
    35 // TODO: clean this up a bit
     35/* This file contains an implementation of the fnmatch() pattern matching
     36 * function. There is more code than necessary to account for the possibility
     37 * of adding POSIX-like locale support to the system in the future. Functions
     38 * that are only necessary for locale support currently simply use single
     39 * characters for "collation elements".
     40 * When (or if) locales are properly implemented, extending this implementation
     41 * will be fairly straightforward.
     42 */
    3643
    3744#include "stdbool.h"
     
    4653#include "fnmatch.h"
    4754
    48 // TODO: documentation
    49 
     55/* Returned by _match... functions. */
    5056#define INVALID_PATTERN -1
    5157
     
    5359 * but may be extended for better locale support.
    5460 */
    55 typedef int _coll_elm_t;
    56 
     61typedef int coll_elm_t;
     62
     63/** Return value indicating that the element in question
     64 * is not valid in the current locale. (That is, if locales are supported.)
     65 */
    5766#define COLL_ELM_INVALID -1
    5867
     
    6069 *
    6170 * @param str
    62  * @return
    63  */
    64 static _coll_elm_t _coll_elm_get(const char* str)
     71 * @return Matching collating element or COLL_ELM_INVALID.
     72 */
     73static coll_elm_t _coll_elm_get(const char* str)
    6574{
    6675        if (str[0] == '\0' || str[1] != '\0') {
     
    7584 * @return
    7685 */
    77 static _coll_elm_t _coll_elm_char(int c)
     86static coll_elm_t _coll_elm_char(int c)
    7887{
    7988        return c;
     
    8695 * @return 0 if the element doesn't match, or the number of characters matched.
    8796 */
    88 static int _coll_elm_match(_coll_elm_t elm, const char *str)
     97static int _coll_elm_match(coll_elm_t elm, const char *str)
    8998{
    9099        return elm == *str;
    91100}
    92101
    93 static int _coll_elm_between(_coll_elm_t first, _coll_elm_t second,
     102/** Checks whether a string begins with a collating element in the given range.
     103 *  Ordering depends on the locale (if locales are supported).
     104 *
     105 * @param first First element of the range.
     106 * @param second Last element of the range.
     107 * @param str String to match.
     108 * @return 0 if there is no match, or the number of characters matched.
     109 */
     110static int _coll_elm_between(coll_elm_t first, coll_elm_t second,
    94111    const char *str)
    95112{
     
    105122 * @param buf_sz Read buffer's size. If the buffer is not large enough for
    106123 *    the entire string, the string is cut with no error indication.
    107  * @return
     124 * @param flags Flags modifying the behavior.
     125 * @return True on success, false if the pattern is invalid.
    108126 */
    109127static bool _get_delimited(
     
    171189};
    172190
    173 /**
     191/** Compare function for binary search in the _char_classes array.
    174192 *
    175193 * @param key
     
    183201}
    184202
    185 /**
     203/** Returns whether the given character belongs to the specified character class.
    186204 *
    187  * @param cname
    188  * @param c
    189  * @return
     205 * @param cname Name of the character class.
     206 * @param c Character.
     207 * @return True if the character belongs to the class, false otherwise.
    190208 */
    191209static bool _is_in_class (const char *cname, int c)
     
    205223}
    206224
    207 /**
     225/** Tries to parse an initial part of the pattern as a character class pattern,
     226 *  and if successful, matches the beginning of the given string against the class.
    208227 *
    209  * @param pattern
    210  * @param str
    211  * @param flags
    212  * @return
     228 * @param pattern Pointer to the pattern to match. Must begin with a class
     229 *    specifier and is repositioned to the first character after the specifier
     230 *    if successful.
     231 * @param str String to match.
     232 * @param flags Flags modifying the behavior (see fnmatch()).
     233 * @return INVALID_PATTERN if the pattern doesn't start with a valid class
     234 *    specifier, 0 if the beginning of the matched string doesn't belong
     235 *    to the class, or positive number of characters matched.
    213236 */
    214237static int _match_char_class(const char **pattern, const char *str, int flags)
     
    225248/************** END CHARACTER CLASSES ****************/
    226249
    227 /**
     250/** Reads the next collating element in the pattern, taking into account
     251 *  locale (if supported) and flags (see fnmatch()).
    228252 *
    229  * @param pattern
    230  * @param flags
    231  * @return
    232  */
    233 static _coll_elm_t _next_coll_elm(const char **pattern, int flags)
    234 {
     253 * @param pattern Pattern.
     254 * @param flags Flags given to fnmatch().
     255 * @return Collating element on success,
     256 *     or COLL_ELM_INVALID if the pattern is invalid.
     257 */
     258static coll_elm_t _next_coll_elm(const char **pattern, int flags)
     259{
     260        assert(pattern != NULL);
     261        assert(*pattern != NULL);
     262        assert(**pattern != '\0');
     263
    235264        const char *p = *pattern;
    236265        const bool noescape = (flags & FNM_NOESCAPE) != 0;
     
    257286        if (!noescape && *p == '\\') {
    258287                p++;
     288                if (*p == '\0') {
     289                        *pattern = p;
     290                        return COLL_ELM_INVALID;
     291                }
    259292        }
    260293        if (pathname && *p == '/') {
    261294                return COLL_ELM_INVALID;
    262295        }
    263 
     296       
    264297        *pattern = p + 1;
    265298        return _coll_elm_char(*p);
    266299}
    267300
    268 /**
     301/** Matches the beginning of the given string against a bracket expression
     302 *  the pattern begins with.
    269303 *
    270  * @param pattern
    271  * @param str
    272  * @param flags
    273  * @return
     304 * @param pattern Pointer to the beginning of a bracket expression in a pattern.
     305 *     On success, the pointer is moved to the first character after the
     306 *     bracket expression.
     307 * @param str Unmatched part of the string.
     308 * @param flags Flags given to fnmatch().
     309 * @return INVALID_PATTERN if the pattern is invalid, 0 if there is no match
     310 *     or the number of matched characters on success.
    274311 */
    275312static int _match_bracket_expr(const char **pattern, const char *str, int flags)
     
    315352        }
    316353       
    317         _coll_elm_t current_elm = COLL_ELM_INVALID;
     354        coll_elm_t current_elm = COLL_ELM_INVALID;
    318355       
    319356        while (*p != ']') {
     
    322359                        /* Range expression. */
    323360                        p++;
    324                         _coll_elm_t end_elm = _next_coll_elm(&p, flags);
     361                        coll_elm_t end_elm = _next_coll_elm(&p, flags);
    325362                        if (end_elm == COLL_ELM_INVALID) {
    326363                                return INVALID_PATTERN;
     
    357394}
    358395
    359 /**
     396/** Matches a portion of the pattern containing no asterisks (*) against
     397 *  the given string.
    360398 *
    361  * @param pattern
    362  * @param string
    363  * @param flags
    364  * @return
     399 * @param pattern Pointer to the unmatched portion of the pattern.
     400 *     On success, the pointer is moved to the first asterisk, or to the
     401 *     terminating nul character, whichever occurs first.
     402 * @param string Pointer to the input string. On success, the pointer is moved
     403 *     to the first character that wasn't explicitly matched.
     404 * @param flags Flags given to fnmatch().
     405 * @return True if the entire subpattern matched. False otherwise.
    365406 */
    366407static bool _partial_match(const char **pattern, const char **string, int flags)
     
    456497}
    457498
    458 /**
     499/** Match string against a pattern.
    459500 *
    460  * @param pattern
    461  * @param string
    462  * @param flags
    463  * @return
     501 * @param pattern Pattern.
     502 * @param string String to match.
     503 * @param flags Flags given to fnmatch().
     504 * @return True if the string matched the pattern, false otherwise.
    464505 */
    465506static bool _full_match(const char *pattern, const char *string, int flags)
     
    518559}
    519560
    520 /**
     561/** Transform the entire string to lowercase.
    521562 *
    522  * @param s
    523  * @return
     563 * @param s Input string.
     564 * @return Newly allocated copy of the input string with all uppercase
     565 *     characters folded to their lowercase variants.
    524566 */
    525567static char *_casefold(const char *s)
Note: See TracChangeset for help on using the changeset viewer.