Skip to content

Add new nullable functions that support null values in jimp #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion jimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ typedef struct {
const char *member;
} Jimp;

// TODO: how do null-s fit into this entire system?
typedef enum {
JIMP_ERR = 0,
JIMP_OK,
JIMP_IS_NULL
} Jimp_Result;

bool jimp_bool(Jimp *jimp, bool *boolean);
Jimp_Result jimp_nullable_bool(Jimp *jimp, bool *boolean);
bool jimp_number(Jimp *jimp, double *number);
Jimp_Result jimp_nullable_number(Jimp *jimp, double *number);
bool jimp_string(Jimp *jimp, const char **string);
Jimp_Result jimp_nullable_string(Jimp *jimp, const char **string);
bool jimp_object_begin(Jimp *jimp);
bool jimp_object_member(Jimp *jimp);
bool jimp_object_end(Jimp *jimp);
Expand Down Expand Up @@ -284,6 +292,20 @@ bool jimp_string(Jimp *jimp, const char **string)
return true;
}

Jimp_Result jimp_nullable_string(Jimp *jimp, const char **string)
{
jimp__get_token(jimp);
if (jimp->token == JIMP_STRING) {
*string = strdup(jimp->string);
} else if (jimp->token == JIMP_NULL) {
return JIMP_IS_NULL;
} else {
jimp_diagf(jimp, "ERROR: expected string, but got `%s`\n", jimp__token_kind(jimp->token));
return JIMP_ERR;
}
return JIMP_OK;
}

bool jimp_bool(Jimp *jimp, bool *boolean)
{
jimp__get_token(jimp);
Expand All @@ -298,13 +320,43 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
return true;
}

Jimp_Result jimp_nullable_bool(Jimp *jimp, bool *boolean)
{
jimp__get_token(jimp);
if (jimp->token == JIMP_TRUE) {
*boolean = true;
} else if (jimp->token == JIMP_FALSE) {
*boolean = false;
} else if (jimp->token == JIMP_NULL) {
return JIMP_IS_NULL;
} else {
jimp_diagf(jimp, "ERROR: expected boolean, but got `%s`\n", jimp__token_kind(jimp->token));
return JIMP_ERR;
}
return JIMP_OK;
}

bool jimp_number(Jimp *jimp, double *number)
{
if (!jimp__get_and_expect_token(jimp, JIMP_NUMBER)) return false;
*number = jimp->number;
return true;
}

Jimp_Result jimp_nullable_number(Jimp *jimp, double *number)
{
jimp__get_token(jimp);
if (jimp->token == JIMP_NUMBER) {
*number = jimp->number;
} else if (jimp ->token == JIMP_NULL) {
return JIMP_IS_NULL;
} else {
jimp_diagf(jimp, "ERROR: expected number, but got `%s`\n", jimp__token_kind(jimp->token));
return JIMP_ERR;
}
return JIMP_OK;
}

static bool jimp__get_and_expect_token(Jimp *jimp, Jimp_Token token)
{
if (!jimp__get_token(jimp)) return false;
Expand Down