1 | /* |
2 | * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright notice, |
9 | * this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * * Neither the name of Redis nor the names of its contributors may be used |
14 | * to endorse or promote products derived from this software without |
15 | * specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #ifndef __REDIS_UTIL_H |
31 | #define __REDIS_UTIL_H |
32 | |
33 | #include <stdint.h> |
34 | #include "sds.h" |
35 | |
36 | /* The maximum number of characters needed to represent a long double |
37 | * as a string (long double has a huge range of some 4952 chars, see LDBL_MAX). |
38 | * This should be the size of the buffer given to ld2string */ |
39 | #define MAX_LONG_DOUBLE_CHARS 5*1024 |
40 | |
41 | /* The maximum number of characters needed to represent a double |
42 | * as a string (double has a huge range of some 328 chars, see DBL_MAX). |
43 | * This should be the size of the buffer for sprintf with %f */ |
44 | #define MAX_DOUBLE_CHARS 400 |
45 | |
46 | /* The maximum number of characters needed to for d2string call. |
47 | * Since it uses %g and not %f, some 40 chars should be enough. */ |
48 | #define MAX_D2STRING_CHARS 128 |
49 | |
50 | /* Bytes needed for long -> str + '\0' */ |
51 | #define LONG_STR_SIZE 21 |
52 | |
53 | /* long double to string conversion options */ |
54 | typedef enum { |
55 | LD_STR_AUTO, /* %.17Lg */ |
56 | LD_STR_HUMAN, /* %.17Lf + Trimming of trailing zeros */ |
57 | LD_STR_HEX /* %La */ |
58 | } ld2string_mode; |
59 | |
60 | int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase); |
61 | int stringmatch(const char *p, const char *s, int nocase); |
62 | int stringmatchlen_fuzz_test(void); |
63 | unsigned long long memtoull(const char *p, int *err); |
64 | const char *mempbrk(const char *s, size_t len, const char *chars, size_t charslen); |
65 | char *memmapchars(char *s, size_t len, const char *from, const char *to, size_t setlen); |
66 | uint32_t digits10(uint64_t v); |
67 | uint32_t sdigits10(int64_t v); |
68 | int ll2string(char *s, size_t len, long long value); |
69 | int ull2string(char *s, size_t len, unsigned long long value); |
70 | int string2ll(const char *s, size_t slen, long long *value); |
71 | int string2ull(const char *s, unsigned long long *value); |
72 | int string2l(const char *s, size_t slen, long *value); |
73 | int string2ld(const char *s, size_t slen, long double *dp); |
74 | int string2d(const char *s, size_t slen, double *dp); |
75 | int trimDoubleString(char *buf, size_t len); |
76 | int d2string(char *buf, size_t len, double value); |
77 | int ld2string(char *buf, size_t len, long double value, ld2string_mode mode); |
78 | int double2ll(double d, long long *out); |
79 | int yesnotoi(char *s); |
80 | sds getAbsolutePath(char *filename); |
81 | long getTimeZone(void); |
82 | int pathIsBaseName(char *path); |
83 | int dirCreateIfMissing(char *dname); |
84 | int dirExists(char *dname); |
85 | int dirRemove(char *dname); |
86 | int fileExist(char *filename); |
87 | sds makePath(char *path, char *filename); |
88 | int fsyncFileDir(const char *filename); |
89 | |
90 | #ifdef REDIS_TEST |
91 | int utilTest(int argc, char **argv, int flags); |
92 | #endif |
93 | |
94 | #endif |
95 | |