Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 4c8f5e7 in mainline


Ignore:
Timestamp:
2011-08-17T17:42:36Z (10 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master
Children:
4419c34
Parents:
903bd436
Message:

Documentation and minor changes

Location:
uspace/lib/posix
Files:
7 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)
  • uspace/lib/posix/locale.c

    r903bd436 r4c8f5e7  
    4242#include "string.h"
    4343
    44 // TODO: documentation
     44/* Just a very basic dummy implementation.
     45 * This should allow code using locales to work properly, but doesn't provide
     46 * any localization functionality.
     47 * Should be extended/rewritten when or if HelenOS supports locales natively.
     48 */
    4549
    4650struct __posix_locale {
     
    7579};
    7680
    77 /**
     81/** Set program locale.
    7882 *
    79  * @param category
    80  * @param locale
    81  * @return
     83 * @param category What category to set.
     84 * @param locale Locale name.
     85 * @return Original locale name on success, NULL on failure.
    8286 */
    8387char *posix_setlocale(int category, const char *locale)
     
    9195}
    9296
    93 /**
     97/** Return locale-specific information.
    9498 *
    95  * @return
     99 * @return Information about the current locale.
    96100 */
    97101struct posix_lconv *posix_localeconv(void)
     
    101105}
    102106
    103 /**
     107/** Duplicate locale object.
    104108 *
    105  * @param locobj
    106  * @return
     109 * @param locobj Object to duplicate.
     110 * @return Duplicated object.
    107111 */
    108112posix_locale_t posix_duplocale(posix_locale_t locobj)
     
    121125}
    122126
    123 /**
     127/** Free locale object.
    124128 *
    125  * @param locobj
     129 * @param locobj Object to free.
    126130 */
    127131void posix_freelocale(posix_locale_t locobj)
     
    132136}
    133137
    134 /**
     138/** Create or modify a locale object.
    135139 *
    136  * @param category_mask
    137  * @param locale
    138  * @param base
    139  * @return
     140 * @param category_mask Mask of categories to be set or modified.
     141 * @param locale Locale to be used.
     142 * @param base Object to modify. 0 if new object is to be created.
     143 * @return The new/modified locale object.
    140144 */
    141145posix_locale_t posix_newlocale(int category_mask, const char *locale,
     
    159163}
    160164
    161 /**
     165/** Set locale for the current thread.
    162166 *
    163  * @param newloc
    164  * @return
     167 * @param newloc Locale to use.
     168 * @return The previously set locale or LC_GLOBAL_LOCALE
    165169 */
    166170posix_locale_t posix_uselocale(posix_locale_t newloc)
    167171{
    168172        // TODO
    169         return NULL;
     173        return LC_GLOBAL_LOCALE;
    170174}
    171175
  • uspace/lib/posix/locale.h

    r903bd436 r4c8f5e7  
    3535#ifndef POSIX_LOCALE_H_
    3636#define POSIX_LOCALE_H_
    37 
    38 // TODO: documentation
    3937
    4038#ifndef NULL
  • uspace/lib/posix/stdio.c

    r903bd436 r4c8f5e7  
    320320
    321321/**
    322  *
    323  * @param buf
    324  * @param size
    325  * @param mode
    326  * @return
    327  */
    328 FILE *posix_fmemopen(void *restrict buf, size_t size,
    329     const char *restrict mode)
    330 {
    331         // TODO
    332         not_implemented();
    333 }
    334 
    335 /**
    336  *
    337  * @param bufp
    338  * @param sizep
    339  * @return
    340  */
    341 FILE *posix_open_memstream(char **bufp, size_t *sizep)
    342 {
    343         // TODO
    344         not_implemented();
    345 }
    346 
    347 /**
    348322 * Write error messages to standard error.
    349323 *
  • uspace/lib/posix/stdio.h

    r903bd436 r4c8f5e7  
    6565extern FILE *posix_freopen(const char *restrict filename,
    6666    const char *restrict mode, FILE *restrict stream);
    67 extern FILE *posix_fmemopen(void *restrict buf, size_t size,
    68     const char *restrict mode);
    69 extern FILE *posix_open_memstream(char **bufp, size_t *sizep);
    7067
    7168/* Error Messages */
  • uspace/lib/posix/stdlib/strtold.c

    r903bd436 r4c8f5e7  
    4848#include "../limits.h"
    4949
    50 // FIXME: #include <float.h>
     50#include "../float.h"
    5151
    5252#ifndef HUGE_VALL
  • uspace/lib/posix/unistd.c

    r903bd436 r4c8f5e7  
    8888int posix_isatty(int fd)
    8989{
     90        // TODO
    9091        /* Always returns false, because there is no easy way to find
    9192         * out under HelenOS. */
Note: See TracChangeset for help on using the changeset viewer.