Whamcloud - gitweb
1f8771d89cb7fe4c14dad310e187706b2a3217ca
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_string.h
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, Whamcloud, Inc.
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 /* Convert a text string to a bitmask */
48 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
49                  int *oldmask, int minmask, int allmask);
50
51 /* Allocate space for and copy an existing string.
52  * Must free with cfs_free().
53  */
54 char *cfs_strdup(const char *str, u_int32_t flags);
55
56 /* safe vsnprintf */
57 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
58
59 /* safe snprintf */
60 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...);
61
62 /* trim leading and trailing space characters */
63 char *cfs_firststr(char *str, size_t size);
64
65 /**
66  * Structure to represent NULL-less strings.
67  */
68 struct cfs_lstr {
69         char            *ls_str;
70         int             ls_len;
71 };
72
73 /*
74  * Structure to represent \<range_expr\> token of the syntax.
75  */
76 struct cfs_range_expr {
77         /*
78          * Link to cfs_expr_list::el_exprs.
79          */
80         cfs_list_t      re_link;
81         __u32           re_lo;
82         __u32           re_hi;
83         __u32           re_stride;
84 };
85
86 struct cfs_expr_list {
87         cfs_list_t      el_link;
88         cfs_list_t      el_exprs;
89 };
90
91 static inline int
92 cfs_iswhite(char c)
93 {
94         switch (c) {
95         case ' ':
96         case '\t':
97         case '\n':
98         case '\r':
99                 return 1;
100         default:
101                 break;
102         }
103         return 0;
104 }
105
106 char *cfs_trimwhite(char *str);
107 int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
108 int cfs_str2num_check(char *str, int nob, unsigned *num,
109                       unsigned min, unsigned max);
110 int cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
111                          int single_tok, struct cfs_range_expr **expr);
112 int cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list);
113 int cfs_expr_list_values(struct cfs_expr_list *expr_list,
114                          int max, __u32 **values);
115 void cfs_expr_list_free(struct cfs_expr_list *expr_list);
116 void cfs_expr_list_print(struct cfs_expr_list *expr_list);
117 int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
118                         struct cfs_expr_list **elpp);
119 void cfs_expr_list_free_list(cfs_list_t *list);
120 int cfs_ip_addr_parse(char *str, int len, cfs_list_t *list);
121 int cfs_ip_addr_match(__u32 addr, cfs_list_t *list);
122 void cfs_ip_addr_free(cfs_list_t *list);
123
124 #ifdef __KERNEL__
125 #define strtoul(str, endp, base)        simple_strtoul(str, endp, base)
126 #endif
127
128 #endif