diff options
| author | Tyge Løvset <[email protected]> | 2020-11-02 12:24:32 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-11-02 12:24:32 +0100 |
| commit | 7171182ce7e0106f8f0fc47ed8f1fe1f58b2ea5d (patch) | |
| tree | 6debbbe87d6b671ec32b1c97bca1c48cf9f1e986 | |
| parent | 0737c3feea3b8015d6db9440b8221374363272c9 (diff) | |
| download | STC-modified-7171182ce7e0106f8f0fc47ed8f1fe1f58b2ea5d.tar.gz STC-modified-7171182ce7e0106f8f0fc47ed8f1fe1f58b2ea5d.zip | |
Changed (half)-internal *_INIT to *__init macros. Minor reformatting.
| -rw-r--r-- | examples/README.md | 2 | ||||
| -rw-r--r-- | examples/advanced.c | 2 | ||||
| -rw-r--r-- | examples/benchmark.c | 7 | ||||
| -rw-r--r-- | examples/birthday.c | 2 | ||||
| -rw-r--r-- | examples/complex.c | 6 | ||||
| -rw-r--r-- | examples/demos.c | 22 | ||||
| -rw-r--r-- | examples/inits.c | 20 | ||||
| -rw-r--r-- | examples/list.c | 2 | ||||
| -rw-r--r-- | examples/mapmap.c | 4 | ||||
| -rw-r--r-- | examples/phonebook.c | 2 | ||||
| -rw-r--r-- | examples/words.c | 6 | ||||
| -rw-r--r-- | stc/cbitset.h | 10 | ||||
| -rw-r--r-- | stc/cfmt.h | 16 | ||||
| -rw-r--r-- | stc/clist.h | 12 | ||||
| -rw-r--r-- | stc/cmap.h | 21 | ||||
| -rw-r--r-- | stc/coption.h | 73 | ||||
| -rw-r--r-- | stc/cpqueue.h | 1 | ||||
| -rw-r--r-- | stc/cptr.h | 6 | ||||
| -rw-r--r-- | stc/cqueue.h | 6 | ||||
| -rw-r--r-- | stc/crandom.h | 7 | ||||
| -rw-r--r-- | stc/cstack.h | 6 | ||||
| -rw-r--r-- | stc/cstr.h | 8 | ||||
| -rw-r--r-- | stc/cvec.h | 8 |
23 files changed, 119 insertions, 130 deletions
diff --git a/examples/README.md b/examples/README.md index 4c24d32a..114f6aac 100644 --- a/examples/README.md +++ b/examples/README.md @@ -58,7 +58,7 @@ using_cmap(vk, Viking, int, c_default_del, vikingraw_equals, vikingraw_hash, cmap_vk uses vikingraw_hash() for hash value calculations, and vikingraw_equals() for equality test. cmap_vk_del() will free all memory allocated for Viking keys and the hash table values. Finally, main which also demos the generic c_push_items() of multiple elements:
```C
int main() {
- cmap_vk vikings = cmap_INIT;
+ cmap_vk vikings = cmap_vk_init();
c_push_items(&vikings, cmap_vk, {
{ {"Einar", "Norway"}, 20},
{ {"Olaf", "Denmark"}, 24},
diff --git a/examples/advanced.c b/examples/advanced.c index 83155405..d559e154 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -60,7 +60,7 @@ using_cmap(vk, Viking, int, c_default_del, vikingvw_equals, vikingvw_hash, int main() { - cmap_vk vikings = cmap_INIT; + cmap_vk vikings = cmap_vk_init(); c_push_items(&vikings, cmap_vk, { {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, diff --git a/examples/benchmark.c b/examples/benchmark.c index f831f417..419e7a8f 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -1,11 +1,10 @@ +#include <stdio.h>
+#include <time.h>
#include <stc/crandom.h>
#include <stc/cfmt.h>
#include <stc/cmap.h>
#include "others/khash.h"
-#include <stdio.h>
-#include <time.h>
-
#ifdef __cplusplus
#include <unordered_map>
#include "others/bytell_hash_map.hpp"
@@ -34,7 +33,7 @@ crand_rng64_t rng; #define RAND(N) (crand_i64(&rng) & ((1 << N) - 1))
-#define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_INIT \
+#define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap__init \
; cmap_##X##_set_load_factors(&map, max_load_factor, 0.0)
#define CMAP_PUT(X, key, val) cmap_##X##_put(&map, key, val).first->second
#define CMAP_EMPLACE(X, key, val) cmap_##X##_emplace(&map, key, val)
diff --git a/examples/birthday.c b/examples/birthday.c index bd3c85f3..fd7f9f46 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -16,7 +16,7 @@ const static uint64_t mask = (1ull << 52) - 1; void repeats(void)
{
crand_rng64_t rng = crand_rng64_init(seed);
- cmap_ic m = cmap_INIT;
+ cmap_ic m = cmap_ic_init();
cmap_ic_reserve(&m, N);
clock_t now = clock();
c_forrange (i, N) {
diff --git a/examples/complex.c b/examples/complex.c index db22f235..ff97c141 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -14,14 +14,14 @@ int main() { int xdim = 4, ydim = 6;
int x = 1, y = 5, tableKey = 42;
const char* strKey = "first";
- cmap_s myMap = cmap_INIT;
+ cmap_s myMap = cmap__init;
{ // Construct.
carray2f table = carray2f_init(ydim, xdim, 0.f);
c_print(1, "table: ({}, {})\n", carray2_ydim(table), carray2_xdim(table));
- clist_y tableList = clist_INIT;
+ clist_y tableList = clist__init;
// Put in some data.
- cmap_g listMap = cmap_INIT;
+ cmap_g listMap = cmap__init;
*carray2f_at(&table, y, x) = 3.1415927f; // table[y][x]
clist_y_push_back(&tableList, table);
diff --git a/examples/demos.c b/examples/demos.c index 16d77319..580942f1 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -39,15 +39,15 @@ using_cvec(ix, int64_t); // ix is just an example tag name. void vectordemo1()
{
c_print(1, "\nVECTORDEMO1\n");
- cvec_ix bignums = cvec_INIT; // = (cvec_ix) cvec_INIT; if initializing after declaration.
+ cvec_ix bignums = cvec__init; // = (cvec_ix) cvec__init; if initializing after declaration.
cvec_ix_reserve(&bignums, 100);
- for (size_t i = 0; i<=100; ++i)
+ c_forrange (i, 101)
cvec_ix_push_back(&bignums, i * i * i);
c_print(1, "erase - {}: {}\n", 100, bignums.data[100]);
cvec_ix_pop_back(&bignums); // erase the last
- for (size_t i = 0; i < cvec_size(bignums); ++i) {
+ c_forrange (i, cvec_size(bignums)) {
if (i >= 90) c_print(1, "{}: {}\n", i, bignums.data[i]);
}
cvec_ix_del(&bignums);
@@ -60,7 +60,7 @@ using_cvec_str(); void vectordemo2()
{
c_print(1, "\nVECTORDEMO2\n");
- cvec_str names = cvec_INIT;
+ cvec_str names = cvec__init;
cvec_str_emplace_back(&names, "Mary");
cvec_str_emplace_back(&names, "Joe");
cvec_str_emplace_back(&names, "Chris");
@@ -78,10 +78,10 @@ using_clist(ix, int); void listdemo1()
{
c_print(1, "\nLISTDEMO1\n");
- clist_ix nums = clist_INIT, nums2 = clist_INIT;
- for (int i = 0; i < 10; ++i)
+ clist_ix nums = clist__init, nums2 = clist__init;
+ c_forrange (i, 10)
clist_ix_push_back(&nums, i);
- for (int i = 100; i < 110; ++i)
+ c_forrange (i, int, 100, 110)
clist_ix_push_back(&nums2, i);
c_foreach (i, clist_ix, nums)
c_print(1, "value: {}\n", *i.val);
@@ -105,7 +105,7 @@ using_cset(i, int); void setdemo1()
{
c_print(1, "\nSETDEMO1\n");
- cset_i nums = cset_INIT;
+ cset_i nums = cset__init;
cset_i_insert(&nums, 8);
cset_i_insert(&nums, 11);
@@ -120,7 +120,7 @@ using_cmap(ii, int, int); void mapdemo1()
{
c_print(1, "\nMAPDEMO1\n");
- cmap_ii nums = cmap_INIT;
+ cmap_ii nums = cmap__init;
cmap_ii_put(&nums, 8, 64);
cmap_ii_put(&nums, 11, 121);
c_print(1, "val 8: {}\n", *cmap_ii_at(&nums, 8));
@@ -133,7 +133,7 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans void mapdemo2()
{
c_print(1, "\nMAPDEMO2\n");
- cmap_si nums = cmap_INIT;
+ cmap_si nums = cmap__init;
cmap_si_put(&nums, "Hello", 64);
cmap_si_put(&nums, "Groovy", 121);
cmap_si_put(&nums, "Groovy", 200); // overwrite previous
@@ -155,7 +155,7 @@ using_cmap_str(); void mapdemo3()
{
c_print(1, "\nMAPDEMO3\n");
- cmap_str table = cmap_INIT;
+ cmap_str table = cmap__init;
cmap_str_put(&table, "Map", "test");
cmap_str_put(&table, "Make", "my");
cmap_str_put(&table, "Sunny", "day");
diff --git a/examples/inits.c b/examples/inits.c index 9436310e..42be17fb 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -23,7 +23,7 @@ int main(void) {
// CVEC FLOAT / PRIORITY QUEUE
- cvec_f floats = cvec_INIT;
+ cvec_f floats = cvec__init;
c_push_items(&floats, cvec_f, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f});
c_foreach (i, cvec_f, floats) c_print(1, "{:.1f} ", *i.val);
@@ -45,7 +45,7 @@ int main(void) // CMAP ID
int year = 2020;
- cmap_id idnames = cmap_INIT;
+ cmap_id idnames = cmap__init;
c_push_items(&idnames, cmap_id, {
{100, cstr_from("Hello")},
{110, cstr_from("World")},
@@ -59,7 +59,7 @@ int main(void) // CMAP CNT
- cmap_cnt countries = cmap_INIT;
+ cmap_cnt countries = cmap__init;
c_push_items(&countries, cmap_cnt, {
{"Norway", 100},
{"Denmark", 50},
@@ -82,12 +82,9 @@ int main(void) // CVEC PAIR
- cvec_ip pairs1 = cvec_INIT;
+ cvec_ip pairs1 = cvec__init;
c_push_items(&pairs1, cvec_ip, {
- {5, 6},
- {3, 4},
- {1, 2},
- {7, 8},
+ {5, 6}, {3, 4}, {1, 2}, {7, 8},
});
cvec_ip_sort(&pairs1);
@@ -98,12 +95,9 @@ int main(void) // CLIST PAIR
- clist_ip pairs2 = clist_INIT;
+ clist_ip pairs2 = clist__init;
c_push_items(&pairs2, clist_ip, {
- {5, 6},
- {3, 4},
- {1, 2},
- {7, 8},
+ {5, 6}, {3, 4}, {1, 2}, {7, 8},
});
clist_ip_sort(&pairs2);
diff --git a/examples/list.c b/examples/list.c index 84a13bf5..bdac3ef0 100644 --- a/examples/list.c +++ b/examples/list.c @@ -9,7 +9,7 @@ int main() { int k;
const int n = 2000000;
- clist_fx list = clist_INIT;
+ clist_fx list = clist__init;
crand_rng64_t eng = crand_rng64_init(1234);
crand_uniform_f64_t dist = crand_uniform_f64_init(100.0f, n);
int m = 0;
diff --git a/examples/mapmap.c b/examples/mapmap.c index 4e6c2f1a..0e4b6991 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -7,8 +7,8 @@ using_cmap_str(); using_cmap_strkey(cfg, cmap_str, cmap_str_del);
int main(void) {
- cmap_cfg config = cmap_INIT;
- cmap_str init = cmap_INIT;
+ cmap_cfg config = cmap__init;
+ cmap_str init = cmap__init;
cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "name", "Joe");
cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "groups", "proj1,proj3");
cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj1", "Energy");
diff --git a/examples/phonebook.c b/examples/phonebook.c index e567c442..a1249ae5 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -36,7 +36,7 @@ void print_phone_book(cmap_str phone_book) int main(int argc, char **argv)
{
bool erased;
- cmap_str phone_book = cmap_INIT;
+ cmap_str phone_book = cmap__init;
c_push_items(&phone_book, cmap_str, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
diff --git a/examples/words.c b/examples/words.c index b72251c6..3ee285e8 100644 --- a/examples/words.c +++ b/examples/words.c @@ -11,7 +11,7 @@ using_cmap_strkey(si, int); int main1() { - clist_str lwords = clist_INIT; + clist_str lwords = clist__init; c_push_items(&lwords, clist_str, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" @@ -21,13 +21,13 @@ int main1() c_print(1, "{}\n", w.val->str); puts(""); - cvec_str words = cvec_INIT; + cvec_str words = cvec__init; c_push_items(&words, cvec_str, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); - cmap_si word_map = cmap_INIT; + cmap_si word_map = cmap__init; c_foreach (w, cvec_str, words) cmap_si_emplace(&word_map, w.val->str, 0).first->second += 1; diff --git a/stc/cbitset.h b/stc/cbitset.h index cc437ce1..28c1a9ef 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -20,6 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#ifndef CBITSET__H__
+#define CBITSET__H__
+
/*
Similar to boost::dynamic_bitset / std::bitset
@@ -42,15 +45,10 @@ int main() { cbitset_del(&bset);
}
*/
-#ifndef CBITSET__H__
-#define CBITSET__H__
-
#include "cstr.h"
typedef struct cbitset { uint64_t* _arr; size_t size; } cbitset_t;
-#define cbitset_INIT {NULL, 0}
-
STC_API cbitset_t cbitset_with_size(size_t size, bool value);
STC_API cbitset_t cbitset_from_str(const char* str);
STC_API cstr_t cbitset_to_str(cbitset_t set);
@@ -64,7 +62,7 @@ STC_API bool cbitset_is_superset(cbitset_t set, cbitset_t other); STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;}
STC_INLINE cbitset_t cbitset_init() {
- cbitset_t bs = cbitset_INIT; return bs;
+ cbitset_t bs = {NULL, 0}; return bs;
}
STC_INLINE void cbitset_set(cbitset_t *self, size_t i) {
self->_arr[i >> 6] |= 1ull << (i & 63);
@@ -20,6 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#ifndef CFMT__H__
+#define CFMT__H__
+
/*
// https://gist.github.com/alexameen/4440e4bcad557a464dcc9ff884763049
#include <time.h>
@@ -44,9 +47,6 @@ int main() { cstr_del(&string);
}
*/
-#ifndef CFMT__H__
-#define CFMT__H__
-
#include <stc/cstr.h>
STC_API void _cfmt_printf(int s, const char* fmt, ...);
@@ -66,15 +66,15 @@ STC_INLINE const char* _cfmt_strftime(char* n, char buf[][_cfmt_sn], size_t maxs int: _cfmt_printf, \
cstr_t *: cstr_fmt)
-#if defined(_MSC_VER) && !defined(__clang__)
-# define _cfmt_uschar() char: "c", unsigned char: "hhu"
-#else
-# define _cfmt_uschar() char: "c", signed char: "c", unsigned char: "hhu"
+#if !defined(_MSC_VER) || defined(__clang__)
+#define _cfmt_uchar() signed char: "c",
#endif
#define _cfmt(x) _Generic (x, \
_Bool: "hhu", \
- _cfmt_uschar(), \
+ char: "c", \
+ unsigned char: "hhu", \
+ _cfmt_uchar() \
short: "hd", \
unsigned short: "hu", \
int: "d", \
diff --git a/stc/clist.h b/stc/clist.h index a9b363a0..55034815 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -23,9 +23,6 @@ #ifndef CLIST__H__
#define CLIST__H__
-#include "ccommon.h"
-#include <stdlib.h>
-
/* Circular Singly-linked Lists.
This implements a std::forward_list-like class in C, but because it is circular,
@@ -38,7 +35,7 @@ using_clist(ix, int64_t);
int main() {
- clist_ix list = clist_INIT;
+ clist_ix list = clist__init;
crand_rng32_t pcg = crand_rng32_init(12345);
int n;
for (int i=0; i<1000000; ++i) // one million
@@ -55,6 +52,9 @@ clist_ix_del(&list);
}
*/
+#include "ccommon.h"
+#include <stdlib.h>
+
#define using_clist(...) c_MACRO_OVERLOAD(using_clist, __VA_ARGS__)
#define using_clist_2(X, Value) \
@@ -85,7 +85,7 @@ int _state; \
} clist_##X##_iter_t
-#define clist_INIT {NULL}
+#define clist__init {NULL}
#define clist_empty(list) ((list).last == NULL)
#define c_emplace_after(self, ctype, pos, ...) do { \
@@ -108,7 +108,7 @@ STC_API size_t _clist_size(const clist_void* self); typedef clist_##X##_rawvalue_t clist_##X##_input_t; \
\
STC_INLINE clist_##X \
- clist_##X##_init(void) {clist_##X x = clist_INIT; return x;} \
+ clist_##X##_init(void) {clist_##X x = clist__init; return x;} \
STC_INLINE bool \
clist_##X##_empty(clist_##X ls) {return clist_empty(ls);} \
STC_INLINE size_t \
@@ -20,6 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#ifndef CMAP__H__
+#define CMAP__H__
+
/*
#include <stdio.h>
#include <stc/cmap.h>
@@ -27,14 +30,14 @@ using_cset(sx, int); // Set of int using_cmap(mx, int, char); // Map of int -> char
int main(void) {
- cset_sx s = cset_INIT;
+ cset_sx s = cset__init;
cset_sx_insert(&s, 5);
cset_sx_insert(&s, 8);
c_foreach (i, cset_sx, s)
printf("set %d\n", i.val->second);
cset_sx_del(&s);
- cmap_mx m = cmap_INIT;
+ cmap_mx m = cmap__init;
cmap_mx_put(&m, 5, 'a');
cmap_mx_put(&m, 8, 'b');
cmap_mx_put(&m, 12, 'c');
@@ -45,18 +48,16 @@ int main(void) { c_foreach (i, cmap_mx, m)
printf("map %d: %c\n", i.val->first, i.val->second);
cmap_mx_del(&m);
-}*/
-#ifndef CMAP__H__
-#define CMAP__H__
-
+}
+*/
#include "ccommon.h"
#include <stdlib.h>
#include <string.h>
-#define cmap_INIT {NULL, NULL, 0, 0, 0.85f, 0.15f}
+#define cmap__init {NULL, NULL, 0, 0, 0.85f, 0.15f}
#define cmap_empty(m) ((m).size == 0)
#define cmap_size(m) ((size_t) (m).size)
-#define cset_INIT cmap_INIT
+#define cset__init cmap__init
#define cset_empty(s) cmap_empty(s)
#define cset_size(s) cmap_size(s)
@@ -205,7 +206,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; } ctype##_##X##_iter_t; \
\
STC_INLINE ctype##_##X \
- ctype##_##X##_init(void) {ctype##_##X m = cmap_INIT; return m;} \
+ ctype##_##X##_init(void) {ctype##_##X m = cmap__init; return m;} \
STC_INLINE bool \
ctype##_##X##_empty(ctype##_##X m) {return m.size == 0;} \
STC_INLINE size_t \
@@ -321,7 +322,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; keyDestroy, RawKey, keyToRaw, keyFromRaw, RawVal, valueFromRaw) \
STC_DEF ctype##_##X \
ctype##_##X##_with_capacity(size_t cap) { \
- ctype##_##X h = ctype##_INIT; \
+ ctype##_##X h = ctype##__init; \
ctype##_##X##_reserve(&h, cap); \
return h; \
} \
diff --git a/stc/coption.h b/stc/coption.h index fe961ad9..ef2da4a7 100644 --- a/stc/coption.h +++ b/stc/coption.h @@ -23,42 +23,44 @@ #ifndef COPTIONS__H__
#define COPTIONS__H__
-/* Inspired by https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c
- Fixed major bugs with option arguments (both long and short).
- Added arg->faulty output field, and has a more consistent API.
+/*
+// Inspired by https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c
+// Fixed major bugs with option arguments (both long and short).
+// Added arg->faulty output field, and has a more consistent API.
+//
+// coption_get() is similar to GNU's getopt_long(). Each call parses one option and
+// returns the option name. opt->arg points to the option argument if present.
+// The function returns -1 when all command-line arguments are parsed. In this case,
+// opt->ind is the index of the first non-option argument.
+#include <stdio.h>
+#include <stc/coption.h>
- coption_get() is similar to GNU's getopt_long(). Each call parses one option and
- returns the option name. opt->arg points to the option argument if present.
- The function returns -1 when all command-line arguments are parsed. In this case,
- opt->ind is the index of the first non-option argument.
-Example:
- int main(int argc, char *argv[])
- {
- coption_long_t longopts[] = {
- {"foo", coption_no_argument, 'f'},
- {"bar", coption_required_argument, 'b'},
- {"opt", coption_optional_argument, 'o'},
- {NULL}
- };
- const char* optstr = "xy:z::123";
- printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n");
- int c;
- coption_t opt = coption_INIT;
- while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) {
- switch (c) {
- case '?': printf("error: unknown option: %s\n", opt.faulty); break;
- case ':': printf("error: missing argument for %s\n", opt.faulty); break;
- default: printf("option: %c [%s]\n", c, opt.arg ? opt.arg : ""); break;
- }
+int main(int argc, char *argv[])
+{
+ coption_long_t longopts[] = {
+ {"foo", coption_no_argument, 'f'},
+ {"bar", coption_required_argument, 'b'},
+ {"opt", coption_optional_argument, 'o'},
+ {NULL}
+ };
+ const char* optstr = "xy:z::123";
+ printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n");
+ int c;
+ coption_t opt = coption_init();
+ while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) {
+ switch (c) {
+ case '?': printf("error: unknown option: %s\n", opt.faulty); break;
+ case ':': printf("error: missing argument for %s\n", opt.faulty); break;
+ default: printf("option: %c [%s]\n", c, opt.arg ? opt.arg : ""); break;
}
- printf("\nNon-option arguments:");
- for (int i = opt.ind; i < argc; ++i)
- printf(" %s", argv[i]);
- putchar('\n');
- return 0;
}
+ printf("\nNon-option arguments:");
+ for (int i = opt.ind; i < argc; ++i)
+ printf(" %s", argv[i]);
+ putchar('\n');
+ return 0;
+}
*/
-
#include <string.h>
#include <stdbool.h>
@@ -83,7 +85,10 @@ typedef struct { int val;
} coption_long_t;
-static const coption_t coption_INIT = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
+static inline coption_t coption_init() {
+ static const coption_t init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
+ return init;
+}
static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */
int k;
@@ -93,7 +98,7 @@ static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over argv[j - k] = p;
}
-/* @param opt output; must be initialized to coption_INIT on first call
+/* @param opt output; must be initialized to coption_init() on first call
* @return ASCII val for a short option; longopt.val for a long option;
* -1 if argv[] is fully processed; '?' for an unknown option or
* an ambiguous long option; ':' if an option argument is missing
diff --git a/stc/cpqueue.h b/stc/cpqueue.h index 0c01c017..b23560d0 100644 --- a/stc/cpqueue.h +++ b/stc/cpqueue.h @@ -44,7 +44,6 @@ cpqueue_f_del(&queue);
}
*/
-
#ifndef CPQUEUE__H__
#define CPQUEUE__H__
@@ -23,10 +23,7 @@ #ifndef CPTR__H__
#define CPTR__H__
-#include "ccommon.h"
-
-/* cptr: std::unique_ptr -like type: */
-/*
+/* cptr: pointers in containers, csptr: std::unique_ptr -like type:
#include <stc/cptr.h>
#include <stc/cstr.h>
#include <stc/cvec.h>
@@ -59,6 +56,7 @@ int main() { cvec_pe_del(&vec);
}
*/
+#include "ccommon.h"
#define using_cptr(...) c_MACRO_OVERLOAD(using_cptr, __VA_ARGS__)
diff --git a/stc/cqueue.h b/stc/cqueue.h index c81b8092..cf549cd2 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -20,6 +20,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#ifndef CQUEUE__H__
+#define CQUEUE__H__
/* Priority Queue using heap, with adapter class (normally cvec).
@@ -52,10 +54,6 @@ cqueue_i_del(&queue);
}
*/
-
-#ifndef CQUEUE__H__
-#define CQUEUE__H__
-
#include "clist.h"
#define using_cqueue(X, ctype) \
diff --git a/stc/crandom.h b/stc/crandom.h index 326a9a20..edd31113 100644 --- a/stc/crandom.h +++ b/stc/crandom.h @@ -20,13 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-
#ifndef CRANDOM__H__
#define CRANDOM__H__
-#include "ccommon.h"
-#include <string.h>
-#include <math.h>
/*
crand_rng32_t rng = crand_rng32_init(seed);
crand_uniform_f32_t fdist = crand_uniform_f32_init(rng, 1.0f, 6.0f);
@@ -36,6 +32,9 @@ int j = crand_uniform_i32(&idist);
float r = crand_uniform_f32(&fdist);
*/
+#include "ccommon.h"
+#include <string.h>
+#include <math.h>
/* 32-BIT RANDOM NUMBER GENERATOR */
diff --git a/stc/cstack.h b/stc/cstack.h index b23c11c3..6a4ec35f 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -20,6 +20,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#ifndef CSTACK__H__
+#define CSTACK__H__
/* Stack adapter (normally uses cvec).
@@ -41,10 +43,6 @@ printf("top: %d\n", *cstack_i_top(&stack));
}
*/
-
-#ifndef CSTACK__H__
-#define CSTACK__H__
-
#include "cvec.h"
#define using_cstack(X, ctype) \
@@ -33,10 +33,10 @@ typedef struct cstr { char* str; } cstr_t; typedef struct { char *val; } cstr_iter_t;
typedef char cstr_value_t;
-#define cstr_size(s) ((const size_t *) (s).str)[-2]
-#define cstr_capacity(s) ((const size_t *) (s).str)[-1]
-#define cstr_empty(s) (cstr_size(s) == 0)
-#define cstr_NPOS ((size_t) (-1))
+#define cstr_size(s) ((const size_t *) (s).str)[-2]
+#define cstr_capacity(s) ((const size_t *) (s).str)[-1]
+#define cstr_empty(s) (cstr_size(s) == 0)
+#define cstr_NPOS ((size_t) (-1))
STC_API cstr_t
cstr_init(void);
@@ -27,7 +27,7 @@ #include <stdlib.h>
#include <string.h>
-#define cvec_INIT {NULL}
+#define cvec__init {NULL}
#define cvec_size(v) _cvec_safe_size((v).data)
#define cvec_capacity(v) _cvec_safe_capacity((v).data)
#define cvec_empty(v) (cvec_size(v) == 0)
@@ -55,7 +55,7 @@ } cvec_##X; \
\
STC_INLINE cvec_##X \
- cvec_##X##_init(void) {cvec_##X v = cvec_INIT; return v;} \
+ cvec_##X##_init(void) {cvec_##X v = cvec__init; return v;} \
STC_INLINE bool \
cvec_##X##_empty(cvec_##X v) {return cvec_empty(v);} \
STC_INLINE size_t \
@@ -77,13 +77,13 @@ \
STC_INLINE cvec_##X \
cvec_##X##_with_size(size_t size, Value null_val) { \
- cvec_##X x = cvec_INIT; \
+ cvec_##X x = cvec__init; \
cvec_##X##_resize(&x, size, null_val); \
return x; \
} \
STC_INLINE cvec_##X \
cvec_##X##_with_capacity(size_t size) { \
- cvec_##X x = cvec_INIT; \
+ cvec_##X x = cvec__init; \
cvec_##X##_reserve(&x, size); \
return x; \
} \
|
