File size: 3,744 Bytes
8be366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | /*---------------------------------------------
* modification time: 2016.12.18 01:15
* mender: Muse
-*---------------------------------------------*/
/*---------------------------------------------
* creation time: 2016.12.05 14:15
* author: Muse
-*---------------------------------------------*/
/*---------------------------------------------
* Source file content Seven part
*
* Part Zero: Include
* Part One: Define
* Part Two: Local data
* Part Three: Local function
*
* Part Four: i/o
* Part Five: string
* Part Six: random
*
-*---------------------------------------------*/
/*---------------------------------------------
* Part Zero: Include
-*---------------------------------------------*/
#include "base.h"
/*---------------------------------------------
* Part One: Define
-*---------------------------------------------*/
#define CHECK_STR(cond, ret) \
if (!haystack || offset < 1 || strlen(haystack) < offset || cond) { \
errno = EINVAL; \
return ret; \
}
#define TRUN_STR() { \
ch = haystack[offset]; \
haystack[offset] = 0; \
}
#define JOIN_STR() haystack[offset] = ch;
/*---------------------------------------------
* Part Four: i/o
*
* 1. readnonblock
* 2. writen
* 3. strnstr
* 4. strnchr
* 5. atoin
*
-*---------------------------------------------*/
/*-----readnonblock-----*/
int32_t readnonblock(int32_t fd, void *buf, size_t count)
{
int32_t res, offset = 0;
while (true) {
if ((res = read(fd, buf + offset, count)) == count) {
return offset + res;
} else if (res == CERR && errno == EAGAIN) {
return offset;
} else {
if (res != CERR) {
offset += res;
count -= res;
}
}
}
}
/*-----writen-----*/
int32_t writen(int32_t fd, void *buf, size_t count)
{
int32_t res, offset = 0;
while (true) {
if ((res = write(fd, buf + offset, count)) == count) {
return offset + res;
} else if (res >= 0) {
offset += res;
count -= res;
} else {
return offset;
}
}
}
/*---------------------------------------------
* Part Five: string
*
* 1. strnstr
* 2. strnchr
* 3. atoin
*
-*---------------------------------------------*/
/*-----strnstr-----*/
char *strnstr(char *haystack, const char *needle, int32_t offset)
{
char *str, ch;
CHECK_STR(!needle, NULL);
TRUN_STR();
str = strstr(haystack, needle);
JOIN_STR();
return str;
}
/*-----strnchr-----*/
char *strnchr(char *haystack, char c, int32_t offset, bool is_first)
{
char *str, ch;
CHECK_STR(false, NULL);
if (haystack[offset] == c)
return &haystack[offset];
TRUN_STR();
str = (is_first) ? strchr(haystack, c) : strrchr(haystack, c);
JOIN_STR();
return str;
}
/*-----atoin-----*/
int32_t atoin(char *haystack, int32_t offset)
{
char ch;
int32_t result;
CHECK_STR(false, CERR);
TRUN_STR();
result = atoi(haystack);
JOIN_STR();
return result;
}
/*---------------------------------------------
* Part Six: random
*
* 1. trandom
* 2. clockrandom
*
-*---------------------------------------------*/
/*-----trandom-----*/
int64_t trandom(int32_t mod)
{
srand48(clock());
return (lrand48() % mod);
}
/*-----clockrandom-----*/
int64_t clockrandom(int32_t mod)
{
return clock() % mod;
}
|