From bdd26d4dde3b3d3180619cc81622081025ca6cb5 Mon Sep 17 00:00:00 2001 From: FreeBirdLjj Date: Sun, 5 Oct 2014 00:23:09 +0800 Subject: [PATCH 1/1] Add support for bool type. --- include/common.h | 17 +++++++++++++++++ include/cstack.h | 10 ++++++---- include/parser.h | 2 +- src/cstack.c | 8 +++----- src/parser.c | 4 ++-- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/include/common.h b/include/common.h index 99c4382..1e4c07a 100644 --- a/include/common.h +++ b/include/common.h @@ -26,6 +26,23 @@ * */ +#if defined( __STDC__ ) // for standard C compiler +#if __STDC_VERSION__ >= 199901L // for C99 and later +#include +#else // __STDC_VERSION__ >= 199901L +#if !defined( bool ) +typedef enum { + false = 0, + true +} bool; +#endif // !defined( bool ) +#endif // __STDC_VERSION__ >= 199901L +#else // defined( __STDC__ ) +#define bool int +#define true 1 +#define false 0 +#endif // defined( __STDC__ ) + #define MAX(a, b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b? _a : _b; }) #define MIN(a, b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b? _a : _b; }) diff --git a/include/cstack.h b/include/cstack.h index bea73a6..fd6c60d 100644 --- a/include/cstack.h +++ b/include/cstack.h @@ -38,6 +38,8 @@ * */ +#include "common.h" + typedef struct _cstack_t { char *content; size_t alloc; @@ -45,16 +47,16 @@ typedef struct _cstack_t { int head; void (*push)(struct _cstack_t *self, char c); char (*pop)(struct _cstack_t *self); - int (*top)(struct _cstack_t *self, char c); - int (*empty)(struct _cstack_t *self); + bool (*top)(struct _cstack_t *self, char c); + bool (*empty)(struct _cstack_t *self); void (*delete)(struct _cstack_t *self); } cstack_t; cstack_t *cstack_init(); void cstack_push(cstack_t *self, char c); char cstack_pop(cstack_t *self); -int cstack_top(cstack_t *self, char c); -int cstack_empty(cstack_t *self); +bool cstack_top(cstack_t *self, char c); +bool cstack_empty(cstack_t *self); void cstack_delete(cstack_t *self); #endif // !defined( CSTACK_H ) diff --git a/include/parser.h b/include/parser.h index 4eb9dfe..99f68ff 100644 --- a/include/parser.h +++ b/include/parser.h @@ -45,7 +45,7 @@ deck_t *markdown_load(FILE *input); int markdown_analyse(cstring_t *text); void markdown_debug(deck_t *deck, int debug); -int is_utf8(char ch); +bool is_utf8(char ch); int length_utf8(char ch); int next_nonblank(cstring_t *text, int i); int prev_blank(cstring_t *text, int i); diff --git a/src/cstack.c b/src/cstack.c index 31611c2..4763b5b 100644 --- a/src/cstack.c +++ b/src/cstack.c @@ -59,13 +59,11 @@ char cstack_pop(cstack_t *self) { return self->content[self->head--]; } -int cstack_top(cstack_t *self, char c) { - if(self->head >= 0 && self->content[self->head] == c) - return 1; - return 0; +bool cstack_top(cstack_t *self, char c) { + return self->head >= 0 && self->content[self->head] == c; } -int cstack_empty(cstack_t *self) { +bool cstack_empty(cstack_t *self) { return self->head == -1; } diff --git a/src/parser.c b/src/parser.c index 6294500..ce3f702 100644 --- a/src/parser.c +++ b/src/parser.c @@ -478,8 +478,8 @@ void markdown_debug(deck_t *deck, int debug) { } } -int is_utf8(char ch) { - return (ch & 0x80); +bool is_utf8(char ch) { + return (ch & 0x80) != 0x00; } int length_utf8(char ch) { -- 2.20.1