/hal/microCOS/device/rexy_clib.c

https://github.com/nemustech/rexy-embedded · C · 427 lines · 132 code · 52 blank · 243 comment · 1 complexity · d66ee3db8f0b34fc7d45341b4bb9dec6 MD5 · raw file

  1. /*
  2. * $Id:
  3. * Copyright(c) 2000-5, Nemustech Inc, All Rights Reserved.
  4. *
  5. * This file is a part of Rexy
  6. *
  7. */
  8. #include "rexy.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. int RalSPrintf(char *buf, const char *format , ...);
  14. ///*
  15. // * @defgroup stdlib C Standard Libraries
  16. // * C Standard Libraries Set
  17. // * @ingroup clib
  18. // * @{
  19. // */
  20. /** @addtogroup util
  21. * @{
  22. */
  23. /** Allocate memory blocks
  24. * Wrapper function for malloc in the standard C library. Returns NULL if it fails to allocate the required memory.
  25. *
  26. * @param size_t memory byte size
  27. *
  28. * @return void * allocated memory pointer
  29. */
  30. void* RalMemAlloc(unsigned int size)
  31. {
  32. void *ptr;
  33. ptr = malloc(size);
  34. return ptr;
  35. }
  36. /** Free memory blocks
  37. * Wrapper function for free in the standard C library
  38. *
  39. * @param void *ptr memory pointer to free
  40. *
  41. * @return void
  42. */
  43. void RalMemFree(void* ptr)
  44. {
  45. if (!ptr) return;
  46. free( ptr );
  47. }
  48. /** Allocate memory blocs and initialize their contents with zero.
  49. * Wrapper function for calloc in the standard C library. Returns NULL if it fails to allocate the required memory.
  50. *
  51. * @param num number of memory count
  52. *
  53. * @param size byte size
  54. *
  55. * @return void * allocated memory pointer
  56. */
  57. void* RalCalloc(unsigned int num, unsigned int size)
  58. {
  59. void *ptr;
  60. ptr = calloc(num, size);
  61. return ptr;
  62. }
  63. /** Reallocation of memory function
  64. * Wrapper function for realloc in the standard C library. Returns NULL if it fails to allocate the required memory.
  65. *
  66. * @param ptr address of already assigned memory block
  67. *
  68. * @param size byte size
  69. *
  70. * @return void * re-allocated memory pointer
  71. */
  72. void* RalRealloc(void* ptr, size_t size)
  73. {
  74. void* newptr;
  75. newptr = realloc(ptr, size);
  76. return newptr;
  77. }
  78. /** Initialize the buffer with zero.
  79. * Equivalent to memset(dest, 0, sizeof(dest))
  80. *
  81. * @param dest pointer to the buffer to clear its contents
  82. * @param size byte length of the buffer
  83. *
  84. * @return none
  85. */
  86. void RalZeroMemory(void* dest, unsigned int size)
  87. {
  88. memset( dest, 0, size );
  89. }
  90. /** Calculates the length of the string s, not including the terminating `\0' character.
  91. * Wrapper function for strlen in the standard C library.
  92. *
  93. * @param s source string pointer
  94. *
  95. * @return the byte length of String s
  96. */
  97. int RalStrLen(const char *s)
  98. {
  99. return strlen(s);
  100. }
  101. /** Copy a string.
  102. * Wrapper function for the strcpy in the standard C library.
  103. *
  104. * @param dest Destination string
  105. * @param src Null-terminated source string
  106. *
  107. * @return returns the value of dest
  108. */
  109. char* RalStrCpy(char* dest, const char* src)
  110. {
  111. return strcpy( dest, src );
  112. }
  113. /** Copy N characters of the source string to the destination string.
  114. * Wrapper function for the strncpy in the standard C library
  115. *
  116. * @param dest Destination string
  117. * @param src Source string
  118. * @param count Number of characters to be copied
  119. *
  120. * @return returns the value of dest
  121. */
  122. char* RalStrNCpy(char* dest, const char* src,unsigned int count)
  123. {
  124. return strncpy( dest, src, count );
  125. }
  126. /** Compare strings.
  127. * Wrapper function for the strcmp in the standard C library.
  128. *
  129. * @param string1 Null-terminated strings to compare
  130. * @param string2 Null-terminated strings to compare
  131. *
  132. * @return The return value indicates the lexicographic relation of string1 to string2.
  133. * returns a value less than zero if string1 is less than string2. returns zero
  134. * if two strings are equivalent. returns a value greater than zero if string1 is
  135. * greater than string2
  136. *
  137. */
  138. int RalStrCmp(const char *string1, const char *string2)
  139. {
  140. return strcmp(string1,string2);
  141. }
  142. /** Duplicate string.
  143. * Wrapper function for strdup in the standard C library. Allocate a memory block, copy the source string and returns the
  144. * pointer to the new string.
  145. *
  146. * @param ptr string pointer
  147. *
  148. * @return void * allocated memory pointer
  149. */
  150. char* RalStrDup(const char* ptr)
  151. {
  152. char* ptmp;
  153. //return ( CheckFile( szFileName, FS_TEST_FILE ) );
  154. return 0;
  155. }
  156. /** Copy memory blocks
  157. * Wrapper function for memcpy in the standard C library.
  158. *
  159. * @param dest destination memory pointer
  160. * @param src source memory pointer
  161. * @param size size
  162. *
  163. * @return returns the value of dest
  164. */
  165. void* RalMemCpy(void* dest, const void* src, unsigned int size)
  166. {
  167. return memcpy( dest, src, size );
  168. }
  169. /** Shrink the file.
  170. *
  171. * @param fd file descriptor
  172. *
  173. * @param new_size the desired byte length of the file.
  174. *
  175. * @return On success, return TRUE. On failure, return false
  176. *
  177. */
  178. bool RalShrinkFile(int fd, int new_size)
  179. {
  180. // return ((ftruncate ( fd, new_size ) == FS_OKAY_S) ? TRUE : FALSE);
  181. return 0;
  182. }
  183. /** Shrink the file. (will be deprecated in 23rd release)
  184. *
  185. * @param fd file descriptor
  186. *
  187. * @param new_size the desired byte length of the file.
  188. *
  189. * @return On success, return TRUE. On failure, return false
  190. *
  191. */
  192. bool RalShrink(int fd, int new_size)
  193. {
  194. return RalShrink(fd, new_size);
  195. }
  196. /** Output debugging messages.
  197. *
  198. * @param msg debugging message
  199. *
  200. * @return none
  201. */
  202. void RalDebugMessage( const char* msg )
  203. {
  204. printf(msg);
  205. printf("\n");
  206. }
  207. /** Halt an execution.
  208. * function stops the execution of the application which called this function.
  209. */
  210. void RalDebugBreak(void)
  211. {
  212. }
  213. void* RalMemSet(void* dst, int value, unsigned int size)
  214. {
  215. memset(dst, value, size);
  216. return dst;
  217. }
  218. void* RalMemCopy(void* dst, const void* src, unsigned int size)
  219. {
  220. memcpy(dst, src, size);
  221. return dst;
  222. }
  223. int RalOpenFile(const char * file)
  224. {
  225. // return open( file, O_RDWR );
  226. return 0;
  227. }
  228. /** Read data from the file indicated by the file handle.
  229. *
  230. * @param fd the file handle
  231. * @param buffer the buffer where data to be stored.
  232. * @param length length to be read.
  233. * @return the number of bytes read.
  234. */
  235. int RalReadFile(int fd, char* buffer, int length)
  236. {
  237. //return read( fd, buffer, length );
  238. return 0;
  239. }
  240. /** Write data to the file of the file handle.
  241. * @param fd the file handle
  242. * @param buffer the buffer which has data to be stored
  243. * @param length length to be written.
  244. * @return the number of bytes written.
  245. */
  246. int RalWriteFile(int fd, const char * buffer, int length)
  247. {
  248. // return write( fd, buffer, length );
  249. return 0;
  250. }
  251. /** Move a file pointer to the specified location.
  252. * Repositions the offset of the open file associated with the file descriptor fildes to the argument offset according to the directive whence as follows.
  253. * - SEEK_SET The offset is set to offset bytes.
  254. * - SEEK_CUR The offset is set to its current location plus offset bytes.
  255. * - SEEK_END The offset is set to the size of the file plus offset bytes.
  256. *
  257. * @param fd the file handle
  258. * @param offset the offset of the open file
  259. * @param whence directive hence
  260. *
  261. * @return same as lseek function in POSIX
  262. */
  263. int RalLseekFile(int fd, int offset, int whence)
  264. {
  265. // return lseek( fd, offset, whence );
  266. return 0;
  267. }
  268. /** Close the file indicated by the file handle.
  269. * @param fd the file handle.
  270. * @return Return 0 if the file was successfully closed.
  271. * A return value of -1 indicates an error.
  272. */
  273. int RalCloseFile(int fd)
  274. {
  275. // return close(fd);
  276. return 0;
  277. }
  278. /** Check if the file exists
  279. *
  280. * @param szFileName the fie name
  281. *
  282. * @return TRUE if file exist, FALSE if the file does not exist
  283. */
  284. bool RalFileExists(const char* szFileName)
  285. {
  286. // return ( CheckFile( szFileName, FS_TEST_FILE ) );
  287. return FALSE;
  288. }
  289. /** Create directory
  290. *
  291. * @param szDirName the directory name
  292. *
  293. * @return File handle. On error, return -1
  294. */
  295. int RalMakeDir(const char* szDirName)
  296. {
  297. // return mkdir(szDirName, 0);
  298. return 0;
  299. }
  300. /** Create a file with read and write permission
  301. *
  302. * @param szFileName the file name
  303. *
  304. * @return File handle. On error, return -1
  305. */
  306. int RalCreateFile(const char* szFileName)
  307. {
  308. // return create( szFileName, 0 );
  309. return 0;
  310. }
  311. /** Delete a file.
  312. *
  313. * @param szFileName the file name
  314. *
  315. * @return On success, return TRUE. On failure, return FALSE
  316. */
  317. /*
  318. bool RalDeleteFile(const char* szFileName)
  319. {
  320. // return ((remove ( szFileName ) == FS_OKAY_S) ? TRUE : FALSE);
  321. // return 0;
  322. return 0;
  323. }
  324. */
  325. /** Compare N characters of two strings.
  326. * Wrapper function for the strncmp in the standard C library.
  327. *
  328. * @param string1 Null-terminated strings to compare
  329. * @param string2 Null-terminated strings to compare
  330. *
  331. * @return The return value is same with that of RalStrCmp. The only difference of this function is
  332. * that RalStrNCmp compares only the first "count" characters of the given argument strings.
  333. */
  334. int RalStrNCmp(const char *string1, const char *string2,unsigned int count)
  335. {
  336. return strncmp(string1,string2,count);
  337. }
  338. /** Append a string.
  339. * Wrapper function for strcat in the standard C library.
  340. *
  341. * @param dest Null-terminated destination string
  342. * @param src Null-terminated source string
  343. *
  344. * @return the destination string. No return value is reserved to indicate an error.
  345. */
  346. char* RalStrCat(char* dest, const char* src)
  347. {
  348. return strcat( dest, src );
  349. }
  350. /** Append a string.
  351. * Wrapper function for strncat in the standard C library.
  352. *
  353. * @param dest Null-terminated destination string
  354. * @param src Null-terminated source string
  355. * @param count the maximum number of the characters to copy from src
  356. *
  357. * @return the destination string. No return value is reserved to indicate an error.
  358. */
  359. char * RalStrNCat(char * dest, const char * src, size_t count)
  360. {
  361. return strncat(dest, src, count);
  362. }
  363. /** Find a character in a string.
  364. * Wrapper function for strchr in the standard C library.
  365. *
  366. * @param pszSource Null-terminated source string
  367. * @param nChar Character code to be found
  368. *
  369. * @return a pointer to the first occurrence of c in string, or NULL if c is not found.
  370. */
  371. char* RalStrChr(const char* pszSource, int nChar )
  372. {
  373. return strchr(pszSource, nChar);
  374. }
  375. /** @} */