system.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
  2. /* If you are missing that file, acquire a complete release at teeworlds.com. */
  3. /*
  4. Title: OS Abstraction
  5. */
  6. #ifndef BASE_SYSTEM_H
  7. #define BASE_SYSTEM_H
  8. #include "detect.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /* Group: Debug */
  13. /*
  14. Function: dbg_assert
  15. Breaks into the debugger based on a test.
  16. Parameters:
  17. test - Result of the test.
  18. msg - Message that should be printed if the test fails.
  19. Remarks:
  20. Does nothing in release version of the library.
  21. See Also:
  22. <dbg_break>
  23. */
  24. void dbg_assert(int test, const char *msg);
  25. #define dbg_assert(test,msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
  26. void dbg_assert_imp(const char *filename, int line, int test, const char *msg);
  27. #ifdef __clang_analyzer__
  28. #include <assert.h>
  29. #undef dbg_assert
  30. #define dbg_assert(test,msg) assert(test)
  31. #endif
  32. /*
  33. Function: dbg_break
  34. Breaks into the debugger.
  35. Remarks:
  36. Does nothing in release version of the library.
  37. See Also:
  38. <dbg_assert>
  39. */
  40. void dbg_break();
  41. /*
  42. Function: dbg_msg
  43. Prints a debug message.
  44. Parameters:
  45. sys - A string that describes what system the message belongs to
  46. fmt - A printf styled format string.
  47. Remarks:
  48. Does nothing in release version of the library.
  49. See Also:
  50. <dbg_assert>
  51. */
  52. void dbg_msg(const char *sys, const char *fmt, ...);
  53. /* Group: Memory */
  54. /*
  55. Function: mem_alloc
  56. Allocates memory.
  57. Parameters:
  58. size - Size of the needed block.
  59. alignment - Alignment for the block.
  60. Returns:
  61. Returns a pointer to the newly allocated block. Returns a
  62. null pointer if the memory couldn't be allocated.
  63. Remarks:
  64. - Passing 0 to size will allocated the smallest amount possible
  65. and return a unique pointer.
  66. See Also:
  67. <mem_free>
  68. */
  69. void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment);
  70. #define mem_alloc(s,a) mem_alloc_debug(__FILE__, __LINE__, (s), (a))
  71. /*
  72. Function: mem_free
  73. Frees a block allocated through <mem_alloc>.
  74. Remarks:
  75. - In the debug version of the library the function will assert if
  76. a non-valid block is passed, like a null pointer or a block that
  77. isn't allocated.
  78. See Also:
  79. <mem_alloc>
  80. */
  81. void mem_free(void *block);
  82. /*
  83. Function: mem_copy
  84. Copies a a memory block.
  85. Parameters:
  86. dest - Destination.
  87. source - Source to copy.
  88. size - Size of the block to copy.
  89. Remarks:
  90. - This functions DOES NOT handles cases where source and
  91. destination is overlapping.
  92. See Also:
  93. <mem_move>
  94. */
  95. void mem_copy(void *dest, const void *source, unsigned size);
  96. /*
  97. Function: mem_move
  98. Copies a a memory block
  99. Parameters:
  100. dest - Destination
  101. source - Source to copy
  102. size - Size of the block to copy
  103. Remarks:
  104. - This functions handles cases where source and destination
  105. is overlapping
  106. See Also:
  107. <mem_copy>
  108. */
  109. void mem_move(void *dest, const void *source, unsigned size);
  110. /*
  111. Function: mem_zero
  112. Sets a complete memory block to 0
  113. Parameters:
  114. block - Pointer to the block to zero out
  115. size - Size of the block
  116. */
  117. void mem_zero(void *block, unsigned size);
  118. /*
  119. Function: mem_comp
  120. Compares two blocks of memory
  121. Parameters:
  122. a - First block of data
  123. b - Second block of data
  124. size - Size of the data to compare
  125. Returns:
  126. <0 - Block a is lesser then block b
  127. 0 - Block a is equal to block b
  128. >0 - Block a is greater then block b
  129. */
  130. int mem_comp(const void *a, const void *b, int size);
  131. /*
  132. Function: mem_check
  133. Validates the heap
  134. Will trigger a assert if memory has failed.
  135. */
  136. int mem_check_imp();
  137. #define mem_check() dbg_assert_imp(__FILE__, __LINE__, mem_check_imp(), "Memory check failed")
  138. /* Group: File IO */
  139. enum {
  140. IOFLAG_READ = 1,
  141. IOFLAG_WRITE = 2,
  142. IOFLAG_RANDOM = 4,
  143. IOSEEK_START = 0,
  144. IOSEEK_CUR = 1,
  145. IOSEEK_END = 2
  146. };
  147. typedef struct IOINTERNAL *IOHANDLE;
  148. /*
  149. Function: io_open
  150. Opens a file.
  151. Parameters:
  152. filename - File to open.
  153. flags - A set of flags. IOFLAG_READ, IOFLAG_WRITE, IOFLAG_RANDOM.
  154. Returns:
  155. Returns a handle to the file on success and 0 on failure.
  156. */
  157. IOHANDLE io_open(const char *filename, int flags);
  158. /*
  159. Function: io_read
  160. Reads data into a buffer from a file.
  161. Parameters:
  162. io - Handle to the file to read data from.
  163. buffer - Pointer to the buffer that will recive the data.
  164. size - Number of bytes to read from the file.
  165. Returns:
  166. Number of bytes read.
  167. */
  168. unsigned io_read(IOHANDLE io, void *buffer, unsigned size);
  169. /*
  170. Function: io_skip
  171. Skips data in a file.
  172. Parameters:
  173. io - Handle to the file.
  174. size - Number of bytes to skip.
  175. Returns:
  176. Number of bytes skipped.
  177. */
  178. unsigned io_skip(IOHANDLE io, int size);
  179. /*
  180. Function: io_write
  181. Writes data from a buffer to file.
  182. Parameters:
  183. io - Handle to the file.
  184. buffer - Pointer to the data that should be written.
  185. size - Number of bytes to write.
  186. Returns:
  187. Number of bytes written.
  188. */
  189. unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);
  190. /*
  191. Function: io_write_newline
  192. Writes newline to file.
  193. Parameters:
  194. io - Handle to the file.
  195. Returns:
  196. Number of bytes written.
  197. */
  198. unsigned io_write_newline(IOHANDLE io);
  199. /*
  200. Function: io_seek
  201. Seeks to a specified offset in the file.
  202. Parameters:
  203. io - Handle to the file.
  204. offset - Offset from pos to stop.
  205. origin - Position to start searching from.
  206. Returns:
  207. Returns 0 on success.
  208. */
  209. int io_seek(IOHANDLE io, int offset, int origin);
  210. /*
  211. Function: io_tell
  212. Gets the current position in the file.
  213. Parameters:
  214. io - Handle to the file.
  215. Returns:
  216. Returns the current position. -1L if an error occured.
  217. */
  218. long int io_tell(IOHANDLE io);
  219. /*
  220. Function: io_length
  221. Gets the total length of the file. Resetting cursor to the beginning
  222. Parameters:
  223. io - Handle to the file.
  224. Returns:
  225. Returns the total size. -1L if an error occured.
  226. */
  227. long int io_length(IOHANDLE io);
  228. /*
  229. Function: io_close
  230. Closes a file.
  231. Parameters:
  232. io - Handle to the file.
  233. Returns:
  234. Returns 0 on success.
  235. */
  236. int io_close(IOHANDLE io);
  237. /*
  238. Function: io_flush
  239. Empties all buffers and writes all pending data.
  240. Parameters:
  241. io - Handle to the file.
  242. Returns:
  243. Returns 0 on success.
  244. */
  245. int io_flush(IOHANDLE io);
  246. /*
  247. Function: io_stdin
  248. Returns an <IOHANDLE> to the standard input.
  249. */
  250. IOHANDLE io_stdin();
  251. /*
  252. Function: io_stdout
  253. Returns an <IOHANDLE> to the standard output.
  254. */
  255. IOHANDLE io_stdout();
  256. /*
  257. Function: io_stderr
  258. Returns an <IOHANDLE> to the standard error.
  259. */
  260. IOHANDLE io_stderr();
  261. /* Group: Threads */
  262. /*
  263. Function: thread_sleep
  264. Suspends the current thread for a given period.
  265. Parameters:
  266. milliseconds - Number of milliseconds to sleep.
  267. */
  268. void thread_sleep(int milliseconds);
  269. /*
  270. Function: thread_create
  271. Creates a new thread.
  272. Parameters:
  273. threadfunc - Entry point for the new thread.
  274. user - Pointer to pass to the thread.
  275. */
  276. void *thread_create(void (*threadfunc)(void *), void *user);
  277. /*
  278. Function: thread_wait
  279. Waits for a thread to be done or destroyed.
  280. Parameters:
  281. thread - Thread to wait for.
  282. */
  283. void thread_wait(void *thread);
  284. /*
  285. Function: thread_destroy
  286. Destroys a thread.
  287. Parameters:
  288. thread - Thread to destroy.
  289. */
  290. void thread_destroy(void *thread);
  291. /*
  292. Function: thread_yeild
  293. Yeild the current threads execution slice.
  294. */
  295. void thread_yield();
  296. /*
  297. Function: thread_detach
  298. Puts the thread in the detached thread, guaranteeing that
  299. resources of the thread will be freed immediately when the
  300. thread terminates.
  301. Parameters:
  302. thread - Thread to detach
  303. */
  304. void thread_detach(void *thread);
  305. /* Group: Locks */
  306. typedef void* LOCK;
  307. LOCK lock_create();
  308. void lock_destroy(LOCK lock);
  309. int lock_try(LOCK lock);
  310. void lock_wait(LOCK lock);
  311. void lock_release(LOCK lock);
  312. /* Group: Semaphores */
  313. #if !defined(CONF_PLATFORM_MACOSX)
  314. #if defined(CONF_FAMILY_UNIX)
  315. #include <semaphore.h>
  316. typedef sem_t SEMAPHORE;
  317. #elif defined(CONF_FAMILY_WINDOWS)
  318. typedef void* SEMAPHORE;
  319. #else
  320. #error missing sempahore implementation
  321. #endif
  322. void semaphore_init(SEMAPHORE *sem);
  323. void semaphore_wait(SEMAPHORE *sem);
  324. void semaphore_signal(SEMAPHORE *sem);
  325. void semaphore_destroy(SEMAPHORE *sem);
  326. #endif
  327. /* Group: Timer */
  328. #ifdef __GNUC__
  329. /* if compiled with -pedantic-errors it will complain about long
  330. not being a C90 thing.
  331. */
  332. __extension__ typedef long long int64;
  333. #else
  334. typedef long long int64;
  335. #endif
  336. /*
  337. Function: time_get
  338. Fetches a sample from a high resolution timer.
  339. Returns:
  340. Current value of the timer.
  341. Remarks:
  342. To know how fast the timer is ticking, see <time_freq>.
  343. */
  344. int64 time_get();
  345. /*
  346. Function: time_freq
  347. Returns the frequency of the high resolution timer.
  348. Returns:
  349. Returns the frequency of the high resolution timer.
  350. */
  351. int64 time_freq();
  352. /*
  353. Function: time_timestamp
  354. Retrives the current time as a UNIX timestamp
  355. Returns:
  356. The time as a UNIX timestamp
  357. */
  358. int time_timestamp();
  359. /* Group: Network General */
  360. typedef struct
  361. {
  362. int type;
  363. int ipv4sock;
  364. int ipv6sock;
  365. } NETSOCKET;
  366. enum
  367. {
  368. NETADDR_MAXSTRSIZE = 1+(8*4+7)+1+1+5+1, // [XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX]:XXXXX
  369. NETTYPE_INVALID = 0,
  370. NETTYPE_IPV4 = 1,
  371. NETTYPE_IPV6 = 2,
  372. NETTYPE_LINK_BROADCAST = 4,
  373. NETTYPE_ALL = NETTYPE_IPV4|NETTYPE_IPV6
  374. };
  375. typedef struct
  376. {
  377. unsigned int type;
  378. unsigned char ip[16];
  379. unsigned short port;
  380. } NETADDR;
  381. /*
  382. Function: net_init
  383. Initiates network functionallity.
  384. Returns:
  385. Returns 0 on success,
  386. Remarks:
  387. You must call this function before using any other network
  388. functions.
  389. */
  390. int net_init();
  391. /*
  392. Function: net_host_lookup
  393. Does a hostname lookup by name and fills out the passed
  394. NETADDR struct with the recieved details.
  395. Returns:
  396. 0 on success.
  397. */
  398. int net_host_lookup(const char *hostname, NETADDR *addr, int types);
  399. /*
  400. Function: net_addr_comp
  401. Compares two network addresses.
  402. Parameters:
  403. a - Address to compare
  404. b - Address to compare to.
  405. Returns:
  406. <0 - Address a is lesser then address b
  407. 0 - Address a is equal to address b
  408. >0 - Address a is greater then address b
  409. */
  410. int net_addr_comp(const NETADDR *a, const NETADDR *b);
  411. /*
  412. Function: net_addr_str
  413. Turns a network address into a representive string.
  414. Parameters:
  415. addr - Address to turn into a string.
  416. string - Buffer to fill with the string.
  417. max_length - Maximum size of the string.
  418. add_port - add port to string or not
  419. Remarks:
  420. - The string will always be zero terminated
  421. */
  422. void net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port);
  423. /*
  424. Function: net_addr_from_str
  425. Turns string into a network address.
  426. Returns:
  427. 0 on success
  428. Parameters:
  429. addr - Address to fill in.
  430. string - String to parse.
  431. */
  432. int net_addr_from_str(NETADDR *addr, const char *string);
  433. /* Group: Network UDP */
  434. /*
  435. Function: net_udp_create
  436. Creates a UDP socket and binds it to a port.
  437. Parameters:
  438. bindaddr - Address to bind the socket to.
  439. Returns:
  440. On success it returns an handle to the socket. On failure it
  441. returns NETSOCKET_INVALID.
  442. */
  443. NETSOCKET net_udp_create(NETADDR bindaddr);
  444. /*
  445. Function: net_udp_send
  446. Sends a packet over an UDP socket.
  447. Parameters:
  448. sock - Socket to use.
  449. addr - Where to send the packet.
  450. data - Pointer to the packet data to send.
  451. size - Size of the packet.
  452. Returns:
  453. On success it returns the number of bytes sent. Returns -1
  454. on error.
  455. */
  456. int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size);
  457. /*
  458. Function: net_udp_recv
  459. Recives a packet over an UDP socket.
  460. Parameters:
  461. sock - Socket to use.
  462. addr - Pointer to an NETADDR that will recive the address.
  463. data - Pointer to a buffer that will recive the data.
  464. maxsize - Maximum size to recive.
  465. Returns:
  466. On success it returns the number of bytes recived. Returns -1
  467. on error.
  468. */
  469. int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize);
  470. /*
  471. Function: net_udp_close
  472. Closes an UDP socket.
  473. Parameters:
  474. sock - Socket to close.
  475. Returns:
  476. Returns 0 on success. -1 on error.
  477. */
  478. int net_udp_close(NETSOCKET sock);
  479. /* Group: Network TCP */
  480. /*
  481. Function: net_tcp_create
  482. Creates a TCP socket.
  483. Parameters:
  484. bindaddr - Address to bind the socket to.
  485. Returns:
  486. On success it returns an handle to the socket. On failure it returns NETSOCKET_INVALID.
  487. */
  488. NETSOCKET net_tcp_create(NETADDR bindaddr);
  489. /*
  490. Function: net_tcp_listen
  491. Makes the socket start listening for new connections.
  492. Parameters:
  493. sock - Socket to start listen to.
  494. backlog - Size of the queue of incomming connections to keep.
  495. Returns:
  496. Returns 0 on success.
  497. */
  498. int net_tcp_listen(NETSOCKET sock, int backlog);
  499. /*
  500. Function: net_tcp_accept
  501. Polls a listning socket for a new connection.
  502. Parameters:
  503. sock - Listning socket to poll.
  504. new_sock - Pointer to a socket to fill in with the new socket.
  505. addr - Pointer to an address that will be filled in the remote address (optional, can be NULL).
  506. Returns:
  507. Returns a non-negative integer on success. Negative integer on failure.
  508. */
  509. int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr);
  510. /*
  511. Function: net_tcp_connect
  512. Connects one socket to another.
  513. Parameters:
  514. sock - Socket to connect.
  515. addr - Address to connect to.
  516. Returns:
  517. Returns 0 on success.
  518. */
  519. int net_tcp_connect(NETSOCKET sock, const NETADDR *addr);
  520. /*
  521. Function: net_tcp_send
  522. Sends data to a TCP stream.
  523. Parameters:
  524. sock - Socket to send data to.
  525. data - Pointer to the data to send.
  526. size - Size of the data to send.
  527. Returns:
  528. Number of bytes sent. Negative value on failure.
  529. */
  530. int net_tcp_send(NETSOCKET sock, const void *data, int size);
  531. /*
  532. Function: net_tcp_recv
  533. Recvives data from a TCP stream.
  534. Parameters:
  535. sock - Socket to recvive data from.
  536. data - Pointer to a buffer to write the data to
  537. max_size - Maximum of data to write to the buffer.
  538. Returns:
  539. Number of bytes recvived. Negative value on failure. When in
  540. non-blocking mode, it returns 0 when there is no more data to
  541. be fetched.
  542. */
  543. int net_tcp_recv(NETSOCKET sock, void *data, int maxsize);
  544. /*
  545. Function: net_tcp_close
  546. Closes a TCP socket.
  547. Parameters:
  548. sock - Socket to close.
  549. Returns:
  550. Returns 0 on success. Negative value on failure.
  551. */
  552. int net_tcp_close(NETSOCKET sock);
  553. /* Group: Strings */
  554. /*
  555. Function: str_append
  556. Appends a string to another.
  557. Parameters:
  558. dst - Pointer to a buffer that contains a string.
  559. src - String to append.
  560. dst_size - Size of the buffer of the dst string.
  561. Remarks:
  562. - The strings are treated as zero-termineted strings.
  563. - Garantees that dst string will contain zero-termination.
  564. */
  565. void str_append(char *dst, const char *src, int dst_size);
  566. /*
  567. Function: str_copy
  568. Copies a string to another.
  569. Parameters:
  570. dst - Pointer to a buffer that shall recive the string.
  571. src - String to be copied.
  572. dst_size - Size of the buffer dst.
  573. Remarks:
  574. - The strings are treated as zero-termineted strings.
  575. - Garantees that dst string will contain zero-termination.
  576. */
  577. void str_copy(char *dst, const char *src, int dst_size);
  578. /*
  579. Function: str_length
  580. Returns the length of a zero terminated string.
  581. Parameters:
  582. str - Pointer to the string.
  583. Returns:
  584. Length of string in bytes excluding the zero termination.
  585. */
  586. int str_length(const char *str);
  587. /*
  588. Function: str_format
  589. Performs printf formating into a buffer.
  590. Parameters:
  591. buffer - Pointer to the buffer to recive the formated string.
  592. buffer_size - Size of the buffer.
  593. format - printf formating string.
  594. ... - Parameters for the formating.
  595. Remarks:
  596. - See the C manual for syntax for the printf formating string.
  597. - The strings are treated as zero-termineted strings.
  598. - Garantees that dst string will contain zero-termination.
  599. */
  600. void str_format(char *buffer, int buffer_size, const char *format, ...);
  601. /*
  602. Function: str_sanitize_strong
  603. Replaces all characters below 32 and above 127 with whitespace.
  604. Parameters:
  605. str - String to sanitize.
  606. Remarks:
  607. - The strings are treated as zero-termineted strings.
  608. */
  609. void str_sanitize_strong(char *str);
  610. /*
  611. Function: str_sanitize_cc
  612. Replaces all characters below 32 with whitespace.
  613. Parameters:
  614. str - String to sanitize.
  615. Remarks:
  616. - The strings are treated as zero-termineted strings.
  617. */
  618. void str_sanitize_cc(char *str);
  619. /*
  620. Function: str_sanitize
  621. Replaces all characters below 32 with whitespace with
  622. exception to \t, \n and \r.
  623. Parameters:
  624. str - String to sanitize.
  625. Remarks:
  626. - The strings are treated as zero-termineted strings.
  627. */
  628. void str_sanitize(char *str);
  629. /*
  630. Function: str_skip_to_whitespace
  631. Skips leading non-whitespace characters(all but ' ', '\t', '\n', '\r').
  632. Parameters:
  633. str - Pointer to the string.
  634. Returns:
  635. Pointer to the first whitespace character found
  636. within the string.
  637. Remarks:
  638. - The strings are treated as zero-termineted strings.
  639. */
  640. char *str_skip_to_whitespace(char *str);
  641. /*
  642. Function: str_skip_whitespaces
  643. Skips leading whitespace characters(' ', '\t', '\n', '\r').
  644. Parameters:
  645. str - Pointer to the string.
  646. Returns:
  647. Pointer to the first non-whitespace character found
  648. within the string.
  649. Remarks:
  650. - The strings are treated as zero-termineted strings.
  651. */
  652. char *str_skip_whitespaces(char *str);
  653. /*
  654. Function: str_comp_nocase
  655. Compares to strings case insensitive.
  656. Parameters:
  657. a - String to compare.
  658. b - String to compare.
  659. Returns:
  660. <0 - String a is lesser then string b
  661. 0 - String a is equal to string b
  662. >0 - String a is greater then string b
  663. Remarks:
  664. - Only garanted to work with a-z/A-Z.
  665. - The strings are treated as zero-termineted strings.
  666. */
  667. int str_comp_nocase(const char *a, const char *b);
  668. /*
  669. Function: str_comp_nocase_num
  670. Compares up to num characters of two strings case insensitive.
  671. Parameters:
  672. a - String to compare.
  673. b - String to compare.
  674. num - Maximum characters to compare
  675. Returns:
  676. <0 - String a is lesser than string b
  677. 0 - String a is equal to string b
  678. >0 - String a is greater than string b
  679. Remarks:
  680. - Only garanted to work with a-z/A-Z.
  681. - The strings are treated as zero-termineted strings.
  682. */
  683. int str_comp_nocase_num(const char *a, const char *b, const int num);
  684. /*
  685. Function: str_comp
  686. Compares to strings case sensitive.
  687. Parameters:
  688. a - String to compare.
  689. b - String to compare.
  690. Returns:
  691. <0 - String a is lesser then string b
  692. 0 - String a is equal to string b
  693. >0 - String a is greater then string b
  694. Remarks:
  695. - The strings are treated as zero-termineted strings.
  696. */
  697. int str_comp(const char *a, const char *b);
  698. /*
  699. Function: str_comp_num
  700. Compares up to num characters of two strings case sensitive.
  701. Parameters:
  702. a - String to compare.
  703. b - String to compare.
  704. num - Maximum characters to compare
  705. Returns:
  706. <0 - String a is lesser then string b
  707. 0 - String a is equal to string b
  708. >0 - String a is greater then string b
  709. Remarks:
  710. - The strings are treated as zero-termineted strings.
  711. */
  712. int str_comp_num(const char *a, const char *b, const int num);
  713. /*
  714. Function: str_comp_filenames
  715. Compares two strings case sensitive, digit chars will be compared as numbers.
  716. Parameters:
  717. a - String to compare.
  718. b - String to compare.
  719. Returns:
  720. <0 - String a is lesser then string b
  721. 0 - String a is equal to string b
  722. >0 - String a is greater then string b
  723. Remarks:
  724. - The strings are treated as zero-termineted strings.
  725. */
  726. int str_comp_filenames(const char *a, const char *b);
  727. /*
  728. Function: str_find_nocase
  729. Finds a string inside another string case insensitive.
  730. Parameters:
  731. haystack - String to search in
  732. needle - String to search for
  733. Returns:
  734. A pointer into haystack where the needle was found.
  735. Returns NULL of needle could not be found.
  736. Remarks:
  737. - Only garanted to work with a-z/A-Z.
  738. - The strings are treated as zero-termineted strings.
  739. */
  740. const char *str_find_nocase(const char *haystack, const char *needle);
  741. /*
  742. Function: str_find
  743. Finds a string inside another string case sensitive.
  744. Parameters:
  745. haystack - String to search in
  746. needle - String to search for
  747. Returns:
  748. A pointer into haystack where the needle was found.
  749. Returns NULL of needle could not be found.
  750. Remarks:
  751. - The strings are treated as zero-termineted strings.
  752. */
  753. const char *str_find(const char *haystack, const char *needle);
  754. /*
  755. Function: str_hex
  756. Takes a datablock and generates a hexstring of it.
  757. Parameters:
  758. dst - Buffer to fill with hex data
  759. dst_size - size of the buffer
  760. data - Data to turn into hex
  761. data - Size of the data
  762. Remarks:
  763. - The desination buffer will be zero-terminated
  764. */
  765. void str_hex(char *dst, int dst_size, const void *data, int data_size);
  766. /*
  767. Function: str_timestamp
  768. Copies a time stamp in the format year-month-day_hour-minute-second to the string.
  769. Parameters:
  770. buffer - Pointer to a buffer that shall receive the time stamp string.
  771. buffer_size - Size of the buffer.
  772. Remarks:
  773. - Guarantees that buffer string will contain zero-termination.
  774. */
  775. void str_timestamp(char *buffer, int buffer_size);
  776. /* Group: Filesystem */
  777. /*
  778. Function: fs_listdir
  779. Lists the files in a directory
  780. Parameters:
  781. dir - Directory to list
  782. cb - Callback function to call for each entry
  783. type - Type of the directory
  784. user - Pointer to give to the callback
  785. Returns:
  786. Always returns 0.
  787. */
  788. typedef int (*FS_LISTDIR_CALLBACK)(const char *name, int is_dir, int dir_type, void *user);
  789. int fs_listdir(const char *dir, FS_LISTDIR_CALLBACK cb, int type, void *user);
  790. /*
  791. Function: fs_makedir
  792. Creates a directory
  793. Parameters:
  794. path - Directory to create
  795. Returns:
  796. Returns 0 on success. Negative value on failure.
  797. Remarks:
  798. Does not create several directories if needed. "a/b/c" will result
  799. in a failure if b or a does not exist.
  800. */
  801. int fs_makedir(const char *path);
  802. /*
  803. Function: fs_storage_path
  804. Fetches per user configuration directory.
  805. Returns:
  806. Returns 0 on success. Negative value on failure.
  807. Remarks:
  808. - Returns ~/.appname on UNIX based systems
  809. - Returns ~/Library/Applications Support/appname on Mac OS X
  810. - Returns %APPDATA%/Appname on Windows based systems
  811. */
  812. int fs_storage_path(const char *appname, char *path, int max);
  813. /*
  814. Function: fs_is_dir
  815. Checks if directory exists
  816. Returns:
  817. Returns 1 on success, 0 on failure.
  818. */
  819. int fs_is_dir(const char *path);
  820. /*
  821. Function: fs_chdir
  822. Changes current working directory
  823. Returns:
  824. Returns 0 on success, 1 on failure.
  825. */
  826. int fs_chdir(const char *path);
  827. /*
  828. Function: fs_getcwd
  829. Gets the current working directory.
  830. Returns:
  831. Returns a pointer to the buffer on success, 0 on failure.
  832. */
  833. char *fs_getcwd(char *buffer, int buffer_size);
  834. /*
  835. Function: fs_parent_dir
  836. Get the parent directory of a directory
  837. Parameters:
  838. path - The directory string
  839. Returns:
  840. Returns 0 on success, 1 on failure.
  841. Remarks:
  842. - The string is treated as zero-termineted string.
  843. */
  844. int fs_parent_dir(char *path);
  845. /*
  846. Function: fs_remove
  847. Deletes the file with the specified name.
  848. Parameters:
  849. filename - The file to delete
  850. Returns:
  851. Returns 0 on success, 1 on failure.
  852. Remarks:
  853. - The strings are treated as zero-terminated strings.
  854. */
  855. int fs_remove(const char *filename);
  856. /*
  857. Function: fs_rename
  858. Renames the file or directory. If the paths differ the file will be moved.
  859. Parameters:
  860. oldname - The actual name
  861. newname - The new name
  862. Returns:
  863. Returns 0 on success, 1 on failure.
  864. Remarks:
  865. - The strings are treated as zero-terminated strings.
  866. */
  867. int fs_rename(const char *oldname, const char *newname);
  868. /*
  869. Group: Undocumented
  870. */
  871. /*
  872. Function: net_tcp_connect_non_blocking
  873. DOCTODO: serp
  874. */
  875. int net_tcp_connect_non_blocking(NETSOCKET sock, NETADDR bindaddr);
  876. /*
  877. Function: net_set_non_blocking
  878. DOCTODO: serp
  879. */
  880. int net_set_non_blocking(NETSOCKET sock);
  881. /*
  882. Function: net_set_non_blocking
  883. DOCTODO: serp
  884. */
  885. int net_set_blocking(NETSOCKET sock);
  886. /*
  887. Function: net_errno
  888. DOCTODO: serp
  889. */
  890. int net_errno();
  891. /*
  892. Function: net_would_block
  893. DOCTODO: serp
  894. */
  895. int net_would_block();
  896. int net_socket_read_wait(NETSOCKET sock, int time);
  897. void mem_debug_dump(IOHANDLE file);
  898. void swap_endian(void *data, unsigned elem_size, unsigned num);
  899. typedef void (*DBG_LOGGER)(const char *line);
  900. void dbg_logger(DBG_LOGGER logger);
  901. void dbg_logger_stdout();
  902. void dbg_logger_debugger();
  903. void dbg_logger_file(const char *filename);
  904. typedef struct
  905. {
  906. int allocated;
  907. int active_allocations;
  908. int total_allocations;
  909. } MEMSTATS;
  910. const MEMSTATS *mem_stats();
  911. typedef struct
  912. {
  913. int sent_packets;
  914. int sent_bytes;
  915. int recv_packets;
  916. int recv_bytes;
  917. } NETSTATS;
  918. void net_stats(NETSTATS *stats);
  919. int str_toint(const char *str);
  920. float str_tofloat(const char *str);
  921. int str_isspace(char c);
  922. char str_uppercase(char c);
  923. unsigned str_quickhash(const char *str);
  924. /*
  925. Function: gui_messagebox
  926. Display plain OS-dependent message box
  927. Parameters:
  928. title - title of the message box
  929. message - text to display
  930. */
  931. void gui_messagebox(const char *title, const char *message);
  932. /*
  933. Function: str_utf8_rewind
  934. Moves a cursor backwards in an utf8 string
  935. Parameters:
  936. str - utf8 string
  937. cursor - position in the string
  938. Returns:
  939. New cursor position.
  940. Remarks:
  941. - Won't move the cursor less then 0
  942. */
  943. int str_utf8_rewind(const char *str, int cursor);
  944. /*
  945. Function: str_utf8_forward
  946. Moves a cursor forwards in an utf8 string
  947. Parameters:
  948. str - utf8 string
  949. cursor - position in the string
  950. Returns:
  951. New cursor position.
  952. Remarks:
  953. - Won't move the cursor beyond the zero termination marker
  954. */
  955. int str_utf8_forward(const char *str, int cursor);
  956. /*
  957. Function: str_utf8_decode
  958. Decodes an utf8 character
  959. Parameters:
  960. ptr - pointer to an utf8 string. this pointer will be moved forward
  961. Returns:
  962. Unicode value for the character. -1 for invalid characters and 0 for end of string.
  963. Remarks:
  964. - This function will also move the pointer forward.
  965. */
  966. int str_utf8_decode(const char **ptr);
  967. /*
  968. Function: str_utf8_encode
  969. Encode an utf8 character
  970. Parameters:
  971. ptr - Pointer to a buffer that should recive the data. Should be able to hold at least 4 bytes.
  972. Returns:
  973. Number of bytes put into the buffer.
  974. Remarks:
  975. - Does not do zero termination of the string.
  976. */
  977. int str_utf8_encode(char *ptr, int chr);
  978. /*
  979. Function: str_utf8_check
  980. Checks if a strings contains just valid utf8 characters.
  981. Parameters:
  982. str - Pointer to a possible utf8 string.
  983. Returns:
  984. 0 - invalid characters found.
  985. 1 - only valid characters found.
  986. Remarks:
  987. - The string is treated as zero-terminated utf8 string.
  988. */
  989. int str_utf8_check(const char *str);
  990. #ifdef __cplusplus
  991. }
  992. #endif
  993. #endif