linux/drivers/staging/lustre/include/linux/libcfs/libcfs_string.h
<<
>>
Prefs
   1/*
   2 * GPL HEADER START
   3 *
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 only,
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License version 2 for more details (a copy is included
  14 * in the LICENSE file that accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * version 2 along with this program; If not, see
  18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  19 *
  20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21 * CA 95054 USA or visit www.sun.com if you need additional information or
  22 * have any questions.
  23 *
  24 * GPL HEADER END
  25 */
  26/*
  27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2012, Intel Corporation.
  31 */
  32/*
  33 * This file is part of Lustre, http://www.lustre.org/
  34 * Lustre is a trademark of Sun Microsystems, Inc.
  35 *
  36 * libcfs/include/libcfs/libcfs_string.h
  37 *
  38 * Generic string manipulation functions.
  39 *
  40 * Author: Nathan Rutman <nathan.rutman@sun.com>
  41 */
  42
  43#ifndef __LIBCFS_STRING_H__
  44#define __LIBCFS_STRING_H__
  45
  46/* libcfs_string.c */
  47/* string comparison ignoring case */
  48int cfs_strncasecmp(const char *s1, const char *s2, size_t n);
  49/* Convert a text string to a bitmask */
  50int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
  51                 int *oldmask, int minmask, int allmask);
  52
  53/* Allocate space for and copy an existing string.
  54 * Must free with kfree().
  55 */
  56char *cfs_strdup(const char *str, u_int32_t flags);
  57
  58/* safe vsnprintf */
  59int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  60
  61/* safe snprintf */
  62int cfs_snprintf(char *buf, size_t size, const char *fmt, ...);
  63
  64/* trim leading and trailing space characters */
  65char *cfs_firststr(char *str, size_t size);
  66
  67/**
  68 * Structure to represent NULL-less strings.
  69 */
  70struct cfs_lstr {
  71        char            *ls_str;
  72        int             ls_len;
  73};
  74
  75/*
  76 * Structure to represent \<range_expr\> token of the syntax.
  77 */
  78struct cfs_range_expr {
  79        /*
  80         * Link to cfs_expr_list::el_exprs.
  81         */
  82        struct list_head        re_link;
  83        __u32           re_lo;
  84        __u32           re_hi;
  85        __u32           re_stride;
  86};
  87
  88struct cfs_expr_list {
  89        struct list_head        el_link;
  90        struct list_head        el_exprs;
  91};
  92
  93static inline int
  94cfs_iswhite(char c)
  95{
  96        switch (c) {
  97        case ' ':
  98        case '\t':
  99        case '\n':
 100        case '\r':
 101                return 1;
 102        default:
 103                break;
 104        }
 105        return 0;
 106}
 107
 108char *cfs_trimwhite(char *str);
 109int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
 110int cfs_str2num_check(char *str, int nob, unsigned *num,
 111                      unsigned min, unsigned max);
 112int cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
 113                         int single_tok, struct cfs_range_expr **expr);
 114int cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list);
 115int cfs_expr_list_values(struct cfs_expr_list *expr_list,
 116                         int max, __u32 **values);
 117static inline void
 118cfs_expr_list_values_free(__u32 *values, int num)
 119{
 120        /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
 121         * by OBD_FREE() if it's called by module other than libcfs & LNet,
 122         * otherwise we will see fake memory leak */
 123        LIBCFS_FREE(values, num * sizeof(values[0]));
 124}
 125
 126void cfs_expr_list_free(struct cfs_expr_list *expr_list);
 127void cfs_expr_list_print(struct cfs_expr_list *expr_list);
 128int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
 129                        struct cfs_expr_list **elpp);
 130void cfs_expr_list_free_list(struct list_head *list);
 131int cfs_ip_addr_parse(char *str, int len, struct list_head *list);
 132int cfs_ip_addr_match(__u32 addr, struct list_head *list);
 133void cfs_ip_addr_free(struct list_head *list);
 134
 135#define strtoul(str, endp, base)        simple_strtoul(str, endp, base)
 136
 137#endif
 138