/*--------------------------------------------- * 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; }