Skip to content

Commit

Permalink
Added some additional utility functions
Browse files Browse the repository at this point in the history
Extended the standard library included functions with some
additional functions depending on <stdarg.h>.
  • Loading branch information
vurtun committed Mar 31, 2016
1 parent e1a1239 commit 08ab2b4
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 26 deletions.
12 changes: 0 additions & 12 deletions demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@ struct demo {
int show_basic;
};

static void
zr_labelf(struct zr_context *ctx, zr_flags align, const char *fmt, ...)
{
char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
buffer[1023] = 0;
zr_label(ctx, buffer, align);
va_end(args);
}

/* ===============================================================
*
* SIMPLE WINDOW
Expand Down
173 changes: 164 additions & 9 deletions zahnrad.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
#if ZR_COMPILE_WITH_DEFAULT_ALLOCATOR
#include <stdlib.h> /* malloc, free */
#endif
#if ZR_COMPILE_WITH_STANDARD_FILE_IO
#if ZR_COMPILE_WITH_STANDARD_IO
#include <stdio.h> /* fopen, fclose,... */
#include <stdarg.h>
#endif
/* ==============================================================
* MATH
Expand Down Expand Up @@ -425,7 +426,7 @@ zr_zero(void *ptr, zr_size size)
zr_memset(ptr, 0, size);
}

static zr_size
zr_size
zr_strlen(const char *str)
{
zr_size siz = 0;
Expand All @@ -434,7 +435,7 @@ zr_strlen(const char *str)
return siz;
}

static int
int
zr_strtof(float *number, const char *buffer)
{
float m;
Expand Down Expand Up @@ -491,6 +492,55 @@ zr_strtof(float *number, const char *buffer)
return 1;
}

int
zr_stricmp(const char *s1, const char *s2)
{
zr_int c1,c2,d;
do {
c1 = *s1++;
c2 = *s2++;
d = c1 - c2;
while (d) {
if (c1 <= 'Z' && c1 >= 'A') {
d += ('a' - 'A');
if (!d) break;
}
if (c2 <= 'Z' && c2 >= 'A') {
d -= ('a' - 'A');
if (!d) break;
}
return ((d >= 0) << 1) - 1;
}
} while (c1);
return 0;
}

int
zr_stricmpn(const char *s1, const char *s2, int n)
{
int c1,c2,d;
assert(n >= 0);
do {
c1 = *s1++;
c2 = *s2++;
if (!n--) return 0;

d = c1 - c2;
while (d) {
if (c1 <= 'Z' && c1 >= 'A') {
d += ('a' - 'A');
if (!d) break;
}
if (c2 <= 'Z' && c2 >= 'A') {
d -= ('a' - 'A');
if (!d) break;
}
return ((d >= 0) << 1) - 1;
}
} while (c1);
return 0;
}

static int
zr_str_match_here(const char *regexp, const char *text)
{
Expand All @@ -515,8 +565,8 @@ zr_str_match_star(int c, const char *regexp, const char *text)
return 0;
}

static int
zr_str_filter(const char *text, const char *regexp)
int
zr_strfilter(const char *text, const char *regexp)
{
/*
c matches any literal character c
Expand All @@ -533,8 +583,8 @@ zr_str_filter(const char *text, const char *regexp)
return 0;
}

static int
zr_str_fuzzy_match(char const *pattern, char const *str, int *out_score)
int
zr_strmatch_fuzzy(char const *pattern, char const *str, int *out_score)
{
/* Returns true if each character in pattern is found sequentially within str
* if found then outScore is also set. Score value has no intrinsic meaning.
Expand Down Expand Up @@ -657,6 +707,28 @@ zr_str_fuzzy_match(char const *pattern, char const *str, int *out_score)
return zr_true;
}

#if ZR_COMPILE_WITH_STANDARD_IO
int
zr_strfmt(char *buf, zr_size buf_size, const char *fmt,...)
{
int w;
va_list args;
va_start(args, fmt);
w = vsnprintf(buf, buf_size, fmt, args);
va_end(args);
buf[buf_size-1] = 0;
return (w == -1) ?(int)buf_size:w;
}

static int
zr_strfmtv(char *buf, zr_size buf_size, const char *fmt, va_list args)
{
int w = vsnprintf(buf, buf_size, fmt, args);
buf[buf_size-1] = 0;
return (w == -1) ? (int)buf_size:w;
}
#endif

static float
zr_pow(float x, int n)
{
Expand Down Expand Up @@ -831,7 +903,7 @@ zr_murmur_hash(const void * key, int len, zr_hash seed)
return h1;
}

#if ZR_COMPILE_WITH_STANDARD_FILE_IO
#if ZR_COMPILE_WITH_STANDARD_IO
static char*
zr_file_load(const char* path, zr_size* siz, struct zr_allocator *alloc)
{
Expand Down Expand Up @@ -4564,7 +4636,7 @@ zr_font_atlas_add_from_memory(struct zr_font_atlas *atlas, void *memory,
return zr_font_atlas_add(atlas, &cfg);
}

#if ZR_COMPILE_WITH_STANDARD_FILE_IO
#if ZR_COMPILE_WITH_STANDARD_IO
struct zr_font*
zr_font_atlas_add_from_file(struct zr_font_atlas *atlas, const char *file_path,
float height, const struct zr_font_config *config)
Expand Down Expand Up @@ -10863,6 +10935,89 @@ zr_text_wrap_colored(struct zr_context *ctx, const char *str,
ctx->last_widget_state = 0;
}

#if ZR_COMPILE_WITH_STANDARD_IO
void zr_labelf_colored(struct zr_context *ctx, zr_flags flags,
struct zr_color color, const char *fmt, ...)
{
char buf[256];
va_list args;
va_start(args, fmt);
zr_strfmtv(buf, ZR_LEN(buf), fmt, args);
zr_label_colored(ctx, buf, flags, color);
va_end(args);
}

void
zr_labelf_colored_wrap(struct zr_context *ctx, struct zr_color color,
const char *fmt, ...)
{
char buf[256];
va_list args;
va_start(args, fmt);
zr_strfmtv(buf, ZR_LEN(buf), fmt, args);
zr_label_colored_wrap(ctx, buf, color);
va_end(args);
}

void
zr_labelf(struct zr_context *ctx, zr_flags flags, const char *fmt, ...)
{
char buf[256];
va_list args;
va_start(args, fmt);
zr_strfmtv(buf, ZR_LEN(buf), fmt, args);
zr_label(ctx, buf, flags);
va_end(args);
}

void
zr_labelf_wrap(struct zr_context *ctx, const char *fmt,...)
{
char buf[256];
va_list args;
va_start(args, fmt);
zr_strfmtv(buf, ZR_LEN(buf), fmt, args);
zr_label_wrap(ctx, buf);
va_end(args);
}

void
zr_value_bool(struct zr_context *ctx, const char *prefix, int value)
{zr_labelf(ctx, ZR_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));}

void
zr_value_int(struct zr_context *ctx, const char *prefix, int value)
{zr_labelf(ctx, ZR_TEXT_LEFT, "%s: %d", prefix, value);}

void
zr_value_uint(struct zr_context *ctx, const char *prefix, unsigned int value)
{zr_labelf(ctx, ZR_TEXT_LEFT, "%s: %u", prefix, value);}

void
zr_value_float(struct zr_context *ctx, const char *prefix, float value)
{zr_labelf(ctx, ZR_TEXT_LEFT, "%s: %.3f", prefix, value);}

void
zr_value_color_byte(struct zr_context *ctx, const char *p, struct zr_color c)
{zr_labelf(ctx, ZR_TEXT_LEFT, "%s: (%c, %c, %c, %c)", p, c.r, c.g, c.b, c.a);}

void
zr_value_color_float(struct zr_context *ctx, const char *p, struct zr_color color)
{
float c[4]; zr_color_fv(c, color);
zr_labelf(ctx, ZR_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
p, c[0], c[1], c[2], c[3]);
}

void
zr_value_color_hex(struct zr_context *ctx, const char *prefix, struct zr_color color)
{
char hex[16];
zr_color_hex_rgba(hex, color);
zr_labelf(ctx, ZR_TEXT_LEFT, "%s: %s", prefix, hex);
}
#endif

void
zr_text(struct zr_context *ctx, const char *str, zr_size len, zr_flags alignment)
{
Expand Down
36 changes: 31 additions & 5 deletions zahnrad.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ extern "C" {
* memory block or a custom allocator.
* IMPORTANT: this adds <stdlib.h> with malloc and free so set 0 if you don't want
* to link to the standard library!*/
#define ZR_COMPILE_WITH_STANDARD_FILE_IO 1
#define ZR_COMPILE_WITH_STANDARD_IO 1
/* setting this to 1 create a default allocator to be used for memory management
* IMPORTANT: this adds <stdio.h> with fopen,fclose,... so set 0 if you don't want
* to link to the standard library!*/
* IMPORTANT: this adds <stdio.h> with fopen,fclose,... and <stdarg> so set 0
* if you don't want to link to the standard library!*/
#define ZR_COMPILE_WITH_VERTEX_BUFFER 1
/* setting this to 1 adds a vertex draw command list backend to this
library, which allows you to convert queue commands into vertex draw commands.
Expand Down Expand Up @@ -106,6 +106,7 @@ extern "C" {
*/
#if ZR_COMPILE_WITH_FIXED_TYPES
#include <stdint.h>
typedef int32_t zr_int;
typedef uint32_t zr_uint;
typedef uint32_t zr_hash;
typedef uintptr_t zr_size;
Expand All @@ -114,6 +115,7 @@ typedef uint32_t zr_flags;
typedef uint32_t zr_rune;
typedef uint8_t zr_byte;
#else
typedef int zr_int;
typedef unsigned int zr_uint;
typedef unsigned int zr_hash;
typedef unsigned long zr_size;
Expand Down Expand Up @@ -172,14 +174,24 @@ struct zr_rect zr_recta(struct zr_vec2 pos, struct zr_vec2 size);
struct zr_rect zr_rectv(const float *xywh);
struct zr_rect zr_rectiv(const int *xywh);

/* string*/
zr_size zr_strlen(const char *str);
int zr_stricmp(const char *s1, const char *s2);
int zr_stricmpn(const char *s1, const char *s2, int n);
int zr_strtof(float *number, const char *buffer);
int zr_strfilter(const char *text, const char *regexp);
int zr_strmatch_fuzzy(char const *pattern, char const *str, int *out_score);
#if ZR_COMPILE_WITH_STANDARD_IO
int zr_strfmt(char *buf, zr_size len, const char *fmt,...);
#endif

/* UTF-8 */
zr_size zr_utf_decode(const char*, zr_rune*, zr_size);
zr_size zr_utf_encode(zr_rune, char*, zr_size);
zr_size zr_utf_len(const char*, zr_size byte_len);
const char* zr_utf_at(const char *buffer, zr_size length, int index,
zr_rune *unicode, zr_size *len);


/* color (conversion user --> zahnrad) */
struct zr_color zr_rgb(int r, int g, int b);
struct zr_color zr_rgb_iv(const int *rgb);
Expand Down Expand Up @@ -571,7 +583,7 @@ struct zr_font* zr_font_atlas_add_default(struct zr_font_atlas*, float height,
struct zr_font* zr_font_atlas_add_from_memory(struct zr_font_atlas *atlas, void *memory,
zr_size size, float height,
const struct zr_font_config *config);
#if ZR_COMPILE_WITH_STANDARD_FILE_IO
#if ZR_COMPILE_WITH_STANDARD_IO
struct zr_font* zr_font_atlas_add_from_file(struct zr_font_atlas *atlas,
const char *file_path, float height,
const struct zr_font_config*);
Expand Down Expand Up @@ -2179,6 +2191,20 @@ void zr_label_colored(struct zr_context*, const char*, zr_flags align, struct zr
void zr_label_wrap(struct zr_context*, const char*);
void zr_label_colored_wrap(struct zr_context*, const char*, struct zr_color);
void zr_image(struct zr_context*, struct zr_image);
#if ZR_COMPILE_WITH_STANDARD_IO
void zr_labelf(struct zr_context*, zr_flags, const char*, ...);
void zr_labelf_colored(struct zr_context*, zr_flags align, struct zr_color, const char*,...);
void zr_labelf_wrap(struct zr_context*, const char*,...);
void zr_labelf_colored_wrap(struct zr_context*, struct zr_color, const char*,...);

void zr_value_bool(struct zr_context*, const char *prefix, int);
void zr_value_int(struct zr_context*, const char *prefix, int);
void zr_value_uint(struct zr_context*, const char *prefix, unsigned int);
void zr_value_float(struct zr_context*, const char *prefix, float);
void zr_value_color_byte(struct zr_context*, const char *prefix, struct zr_color);
void zr_value_color_float(struct zr_context*, const char *prefix, struct zr_color);
void zr_value_color_hex(struct zr_context*, const char *prefix, struct zr_color);
#endif

/* buttons */
int zr_button_text(struct zr_context *ctx, const char *title, zr_size len, enum zr_button_behavior);
Expand Down

0 comments on commit 08ab2b4

Please sign in to comment.