1 #ifndef YACASL2_CMEM_H_INCLUDED
2 #define YACASL2_CMEM_H_INCLUDED
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <stdbool.h>
9 #include <limits.h>
10
11 /**
12 * @brief 配列のサイズを返すマクロ
13 */
14 #ifndef ARRAYSIZE
15 #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
16 #endif
17
18 /**
19 * @brief メモリを解放するマクロ
20 */
21 #ifndef FREE
22 #define FREE(ptr) {free(ptr); ptr = NULL;}
23 #endif
24
25 /**
26 * @brief 数値文字列が特定の範囲の数値かチェックし、正の場合は変換した数値、不正の場合は0を返す
27 *
28 * @param str 数値文字列
29 * @param min 範囲最小値
30 * @param max 範囲最大値
31 * @param name エラーメッセージなどで表示される名前
32 *
33 */
34 long str2l_range(const char *str, long min, long max, const char *name);
35
36 /**
37 * @brief mallocを実行し、0で初期化する
38 *
39 * メモリを確保できない場合はエラーを出力して終了
40 *
41 * @param size メモリーのサイズ
42 * @param tag エラーメッセージなどで表示されるタグ
43 */
44 void *malloc_chk(size_t size, const char *tag);
45
46 /**
47 * @brief 領域の数とサイズを指定してメモリーを確保するcallocを実行する
48 *
49 * メモリを確保できない場合はエラーを出力して終了
50 *
51 * @param nmemb 領域の数
52 * @param size 領域1個あたりのメモリーサイズ
53 * @param tag エラーメッセージなどで表示されるタグ
54 */
55 void *calloc_chk(size_t nmemb, size_t size, const char *tag);
56
57 /**
58 * @brief malloc_chkを実行してメモリを確保し、コピーした文字列を返す
59 *
60 * @return コピーした文字列
61 *
62 * @param s 文字列
63 * @param tag エラーメッセージなどで表示されるタグ
64 */
65 char *strdup_chk(const char *s, const char *tag);
66
67 /**
68 * @brief malloc_chkを実行してメモリを確保し、コピーした文字列の指定した長さの部分を返す
69 *
70 * @return コピーした文字列
71 *
72 * @param s 文字列
73 * @param len 文字列の長さ
74 * @param tag エラーメッセージなどで表示されるタグ
75 */
76 char *strndup_chk(const char *s, size_t len, const char *tag);
77
78 /**
79 * @brief 文字列の末尾から、改行と空白とタブを削除する
80 *
81 * @param s 文字列
82 */
83 void strip_end(char *s);
84
85 /**
86 * @brief 文字列から「'」以降の文字列をCASL IIのコメントとして削除する。「''」の場合は除く
87 *
88 * @param s 文字列
89 */
90 void strip_casl2_comment(char *s);
91
92 /**
93 * @brief 逆にした文字列を返す
94 *
95 * @return 逆にしたした文字列
96 *
97 * @param s 文字列
98 */
99 char *strrev(const char *s);
100
101 #endif