Advertisement
Guest User

Untitled

a guest
May 25th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. #include "strings.h"
  2. #include <string.h>
  3. #include <malloc.h>
  4. #pragma warning(disable : 4996)
  5.  
  6. #define END 126
  7. #define false 0
  8. #define true 1
  9.  
  10. //@brief This function converts our string to \0 terminated string
  11. //@param
  12. //char *str - pointer to string
  13. //@return pointer to converted string
  14. char *convertStr(char * str)
  15. {
  16. if (str != NULL)
  17. {
  18. const char * begin = str;
  19. str = str + my_strlen(str);
  20. *str = '\0';
  21. return (char*)begin;
  22. }
  23. return NULL;
  24. }
  25.  
  26. //@brief This function prints a string
  27. //@param
  28. //char *str - pointer to string
  29. //@return void
  30. void print(const char *str)
  31. {
  32. if (str != NULL)
  33. {
  34. printf("%s", str);
  35. }
  36. else
  37. printf("(null)");
  38. }
  39.  
  40. //@brief This function prints string with '\n' in the end
  41. //@param
  42. //char *str - pointer to string
  43. //@return void
  44. void printn(const char *str)
  45. {
  46.  
  47. if (str != NULL)
  48. {
  49. printf("%s\n", str);
  50. }
  51. else
  52. printf("(null)\n");
  53. }
  54.  
  55. //@brief This function reads string from keyboard and set last character as END
  56. //@return void
  57. char *getstring()
  58. {
  59. fflush(stdin);
  60.  
  61. char* buf = (char*)malloc(127);
  62.  
  63. fgets(buf, 127, stdin); //reading string from keyboard
  64.  
  65. size_t i = 0;
  66. while (*(buf + i) != '\0') //looking for '\0' to replace it by END
  67. ++i;
  68.  
  69. *(buf + i - 1) = END; //end of string character
  70. return buf;
  71. }
  72.  
  73. //@brief This function cheaks if array "accept" contains character "chr"
  74. //@param char *chr - some character
  75. //@param const char *accept - array of characters
  76. //@return 1 if character was found in "accept", or 0 otherwise
  77. short contain(char chr, const char *accept)
  78. {
  79. for (size_t i = 0; accept[i] != END; ++i)
  80. if (accept[i] == chr)
  81. {
  82. return 1;
  83. }
  84. return 0;
  85. }
  86.  
  87. //@brief This function copies one memory region to the another region
  88. //@param pointer to destination
  89. //@param pointer to source
  90. //@param number of bytes to copy
  91. //@return void
  92. void *my_memcpy(char *dest, const char *src, size_t bytes)
  93. {
  94. if (dest == src)
  95. return NULL;
  96.  
  97. while (bytes--)
  98. {
  99. *dest = *src;
  100. ++dest;
  101. ++src;
  102. }
  103. }
  104.  
  105. //@brief Fills a buffer with a character
  106. //@param
  107. //char *buf - buffer to fill
  108. //int chr - chr to fill the buffer
  109. //size_t bytes - number of bytes to fill
  110. //@return void
  111. void *my_memset(char *buf, int chr, size_t bytes)
  112. {
  113. for (size_t i = 0; i < bytes; ++i)
  114. *(buf + i) = chr; //filling buffer with chr
  115. return buf;
  116. }
  117.  
  118. //@brief This function finds the first occurrence of a character in a buffer
  119. //@param
  120. //const char *str - pointer to buffer
  121. //int - number of character in ASCII table we are looking for
  122. //@return pointer to character position or NULL
  123. void *my_memchr(const char *buf, char chr, size_t bytes)
  124. {
  125. for (size_t i = 0; i < bytes; ++i)
  126. if (*(buf + i) == chr)
  127. return (void*)(buf + i);
  128. return NULL;
  129. }
  130.  
  131. //@brief This function compares no more then n bytes of two buffers of memory
  132. //@param
  133. //const char *buf1 - pointer to first region of memory
  134. //const char *buf2 - pointer to second region of memory
  135. //@return buf1==buf2 => 0, buf1>buf2 => 1, buf2<buf1 => -1
  136. int my_memcmp(const char *buf1, const char *buf2, size_t bytes)
  137. {
  138. for (size_t i = 0; i < bytes, (*buf1 == *buf2); ++i)
  139. {
  140. ++buf1; ++buf2;
  141. }
  142. if (*buf1 == *buf2)
  143. return 0;
  144. else
  145. return (*buf1 < *buf2) ? -1 : 1;
  146. }
  147.  
  148. //@brief This function appends one string to another
  149. //@param
  150. //char *dest - destination string
  151. //char *src - source string
  152. //@return pointer to destination string
  153. char *my_strcat(char *dest, const char *src)
  154. {
  155. //memcopy will not copy data from pointer to the same pointer, so we need temp variable
  156. //if dest==src nothing will happen
  157. //so we need temp variable
  158. char *tDest = dest;
  159.  
  160. //begin copying from the end of tDest
  161. my_memcpy(tDest + my_strlen(tDest), src, my_strlen(src)+1);
  162. return tDest;
  163. }
  164.  
  165. //@brief This function appends no more then n bytes from one string to another
  166. //@param
  167. //char *dest - destination string
  168. //char *src - source string
  169. //size_t bytes - number of bytes to append
  170. //@return pointer to destination string
  171. char *my_strncat(char *dest, const char *src, size_t bytes)
  172. {
  173. //we will call strlen once insted of 3 times
  174. const size_t srcLength = my_strlen(src);
  175. const size_t destLength = my_strlen(dest);
  176.  
  177. //there is no sense to copy something after "end of the line symbol"
  178. if (bytes > srcLength)
  179. bytes = srcLength + 1;
  180.  
  181. //memcopy will not copy data from pointer to the same pointer, so we need temp variable
  182. //if dest==src nothing will happen
  183. //so we need temp variable
  184. char *tDest = dest;
  185. my_memcpy(tDest + my_strlen(tDest), src, bytes);
  186.  
  187. //we should set last character as END
  188. if (bytes < srcLength + 1)
  189. *(tDest + destLength + bytes) = END;
  190. return tDest;
  191. }
  192.  
  193. //@brief This function copies one string to another
  194. //@param
  195. //char *dest - destination string
  196. //char *src - source string
  197. //@return pointer to destination string
  198. char *my_strcpy(char *dest, const char *src)
  199. {
  200. //there is no sense to copy data like this
  201. if (dest == src)
  202. return dest;
  203.  
  204. my_memcpy(dest, src, my_strlen(src) + 1);
  205. return dest;
  206. }
  207.  
  208. //@brief This function copies no more then n bytes from one string to another
  209. //@param
  210. //char *dest - destination string
  211. //char *src - source string
  212. //size_t bytes - number of bytes to copy
  213. //@return pointer to destination string
  214. char *my_strncpy(char *dest, const char *src, size_t bytes)
  215. {
  216. //there is no sense to copy data like this
  217. if (dest == src)
  218. return dest;
  219.  
  220. //we will call strlen once insted of 3 times
  221. const size_t srcLength = my_strlen(src);
  222.  
  223. //there is no sense to copy something after "end of the line symbol"
  224. if (bytes > srcLength)
  225. bytes = srcLength + 1;
  226.  
  227. my_memcpy(dest, src, bytes);
  228.  
  229. //setting last character
  230. if (bytes <= srcLength + 1)
  231. *(dest + bytes) = END;
  232. return dest;
  233. }
  234.  
  235. //@brief This function returns length of the string
  236. //@param
  237. //const char *str - pointer to string
  238. //@return integer number of symbols
  239. size_t my_strlen(const char *str)
  240. {
  241. size_t symbCounter = 0;
  242. while (*(str + symbCounter) != END)
  243. {
  244. ++symbCounter;
  245. }
  246. return symbCounter;
  247. }
  248.  
  249. //@brief lexicographical comparison of two strings
  250. //@param
  251. //const char *str1 - pointer to first string
  252. //const char *str2 - pointer to second string
  253. //@return str1==str2 => 0, str1>str2 => 1, str2>str1 => -1
  254. int my_strcmp(const char *str1, const char *str2)
  255. {
  256. //stop comparing if here is the end of str 1 or some symbols doesn't match
  257. while (*str1 != END && (*str1 == *str2))
  258. {
  259. ++str1; ++str2;
  260. }
  261.  
  262. if (*str1 == *str2)
  263. return 0;
  264. else
  265. return (*str1 < *str2) ? -1 : 1;
  266. }
  267.  
  268. //@brief lexicographical comparison no more then n bytes from two strings
  269. //@param
  270. //const char *str1 - pointer to first string
  271. //const char *str2 - pointer to second string
  272. //@return str1==str2 => 0, str1>str2 => 1, str2<str1 => -1
  273. int my_strncmp(const char *str1, const char *str2, size_t bytes)
  274. {
  275. //we will call strlen once insted of 3 times
  276. const size_t str1Length = my_strlen(str1);
  277. const size_t str2Length = my_strlen(str2);
  278. //we will not compare more then "minimal string length" bytes
  279. if (bytes > str1Length || bytes > str2Length)
  280. {
  281. if (str1Length < str2Length)
  282. bytes = str1Length;
  283. else
  284. bytes = str2Length;
  285. }
  286.  
  287. size_t bytesCompared = 0;
  288. //stop comparing if symbols does not match, or we campared enough bytes
  289. while ((*str1 == *str2) && (bytesCompared < bytes))
  290. {
  291. ++str1; ++str2; ++bytesCompared;
  292. }
  293. if (*str1 == *str2)
  294. return 0;
  295. else
  296. return (*str1 < *str2) ? -1 : 1;
  297. }
  298.  
  299. //@brief This function looking for FIRST char which is equal to second parameter
  300. //@param
  301. //const char *str - pointer to string
  302. //int chr - number of character in ASCII table we are looking for
  303. //@return pointer to character position or NULL
  304. char *my_strchr(const char *str, int chr)
  305. {
  306. //looking for character while not end of string or character is found
  307. while (*str != chr && *str != END)
  308. ++str;
  309.  
  310. //if end of line was reached and symbol is not found
  311. if (*str == END)
  312. return NULL;
  313. else
  314. return (char*)str;
  315. }
  316.  
  317. //@brief This function looking for LAST char which is equal to second parameter
  318. //@param
  319. //const char *str - pointer to string
  320. //int chr - number of character in ASCII table we are looking for
  321. //@return pointer to character position or NULL
  322. char *my_strrchr(const char *str, int chr)
  323. {
  324. //we will start from the end of string
  325. const char *begin = str;
  326. str = str + my_strlen(str);
  327. for (; *str != chr && str != begin; --str);
  328.  
  329. //first symbol means end of the cycle but it can be equal to "chr"
  330. if (str == begin && *begin != chr)
  331. return NULL;
  332. else
  333. if (str == begin && *begin == chr)
  334. return (char*)str;
  335. else
  336. return (char*)str;
  337. }
  338.  
  339. //@brief This function counting length of string which only consists of symbols from "accept"
  340. //@param
  341. //const char *str - pointer to string
  342. //const char *accept - array of characters
  343. //@return length of string which only consists of symbols from "accept"
  344. size_t my_strspn(const char *str, const char *accept)
  345. {
  346. size_t curStrChr, curAcceptChr;
  347. for (curStrChr = 0; str[curStrChr] != END; ++curStrChr)
  348. {
  349. for (curAcceptChr = 0; accept[curAcceptChr] != END; ++curAcceptChr)
  350. {
  351. if (accept[curAcceptChr] == str[curStrChr])
  352. break;
  353. }
  354.  
  355. //if the end of "accept" is reached -> there is no any symbol from "accept" in the string
  356. if (accept [curAcceptChr] == END)
  357. break;
  358. }
  359. return curStrChr;
  360. }
  361.  
  362. //@brief This function counting length of string which does not consist any symbol from "reject"
  363. //@param
  364. //const char *str - pointer to string
  365. //const char *accept - array of characters
  366. //@return length of string which does not consist any symbol from "reject"
  367. size_t my_strcspn(const char *str, const char *reject)
  368. {
  369. size_t curStrChr, curRejectChr;
  370.  
  371. for (curStrChr = 0; str[curStrChr] != END; ++curStrChr)
  372. {
  373. for (curRejectChr = 0; reject[curRejectChr] != END; ++curRejectChr)
  374. {
  375. //if character is found in array "reject" => return length
  376. if (reject[curRejectChr] == str[curStrChr])
  377. return curStrChr;
  378. }
  379. }
  380. return curStrChr;
  381. }
  382.  
  383. //@brief This function finds in a string the first occurrence of a character in a string
  384. //@param
  385. //const char *str - pointer to string
  386. //const char *accept - array of characters
  387. //@return pointer to first occurrence of a character in a string
  388. char *my_strpbrk(const char *str, const char *accept)
  389. {
  390. size_t curStrChr, curAcceptChr;
  391. for (curStrChr = 0; str[curStrChr] != END; ++curStrChr)
  392. {
  393. for (curAcceptChr = 0; accept[curAcceptChr] != END; ++curAcceptChr)
  394. {
  395. //if characters matches
  396. if (accept[curAcceptChr] == str[curStrChr])
  397. return (char*)(str + curStrChr);
  398. }
  399. }
  400. return NULL;
  401. }
  402.  
  403. //@brief This function looking for occurrence of substring in a string
  404. //@param
  405. //const char *str - pointer to string
  406. //const char *substr - pointer to substring
  407. //@return pointer to string
  408. char *my_strstr(const char *str, const char *substr)
  409. {
  410. //substr pointer will be modified so we should save the beginning of substr
  411. const char *subStrBegin = substr;
  412. size_t matchesCounter;
  413. const size_t substrSize = my_strlen(subStrBegin);
  414. for (; *str != END; ++str)
  415. {
  416. //each time we start comparing from the beginning of substring
  417. substr = subStrBegin;
  418. for (matchesCounter = 0; *substr != END; ++matchesCounter)
  419. {
  420. if (*substr != *(str + matchesCounter))
  421. break;
  422. ++substr;
  423. }
  424.  
  425. //if all symbols of str part and substring are equal => return pointer to this part
  426. if (matchesCounter == substrSize)
  427. return (char*)str;
  428. }
  429. return NULL;
  430. }
  431.  
  432. //@brief This function splits string into tokens. Each function call returns one token.
  433. // but each next call for the same ftring requires NULL as first parameter.
  434. //@param
  435. //char *str - pointer to string
  436. //const char *delims - pointer to delimiters
  437. //@return pointer to token
  438. char *my_strtok(char *str, const char *delims)
  439. {
  440. static char *strToSplit;
  441. size_t tokenSize = 0;
  442. size_t delimitersToPass = 0;
  443. char *token = NULL;
  444. short lastToken = false;
  445.  
  446. //new str to split
  447. if (str != NULL)
  448. {
  449. strToSplit = str;
  450. delimitersToPass = my_strspn(strToSplit, delims);
  451. strToSplit = strToSplit + delimitersToPass;
  452. }
  453.  
  454. //strcspn will return length of string before next delimiter
  455. tokenSize = my_strcspn(strToSplit, delims);
  456.  
  457. //if there is no more characters to split a token
  458. if (tokenSize == 0)
  459. return NULL;
  460.  
  461. //checking if token is last
  462. if (*(strToSplit + tokenSize) == END)
  463. lastToken = true;
  464. //set end of line symbol to return a token instead of whole string
  465. *(strToSplit + tokenSize) = END;
  466. //strToSplit will be modified for next function call, so we should save pointer to the token
  467. token = strToSplit;
  468. //Now strToSplit will point to next character after the token and end of line symbol
  469. strToSplit = strToSplit + tokenSize;
  470. //if token is not last pointer will point to the next symbol after END
  471. if (!lastToken)
  472. ++strToSplit;
  473.  
  474. //counting delimiters to pass if we there are few delimiters in sequence
  475. delimitersToPass = my_strspn(strToSplit, delims);
  476. //strToSplit now points to the next token or end of line
  477. strToSplit = strToSplit + delimitersToPass;
  478.  
  479. return token;
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement