Skip to content
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

Moved micro_args to examples #731

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion examples/examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#define EXAMPLES_H_

#include <nats.h>
#include <micro_args.h>
#include <stdio.h>
#include <string.h>

Expand Down
1 change: 1 addition & 0 deletions examples/micro-arithmetics.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

#include "examples.h"
#include "micro_args.h"

// Sequence NATS microservice example.
//
Expand Down
1 change: 1 addition & 0 deletions examples/micro-func.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

#include "examples.h"
#include "micro_args.h"

// Sequence NATS microservice example.
//
Expand Down
1 change: 1 addition & 0 deletions examples/micro-hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

#include "examples.h"
#include "micro_args.h"

// Hello World! NATS microservice example.
//
Expand Down
1 change: 1 addition & 0 deletions examples/micro-sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

#include "examples.h"
#include "micro_args.h"

// Sequence NATS microservice example.
//
Expand Down
1 change: 1 addition & 0 deletions examples/micro-stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdlib.h>

#include "examples.h"
#include "micro_args.h"

typedef struct service_state_s
{
Expand Down
189 changes: 100 additions & 89 deletions src/micro_args.c → examples/micro_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,109 +11,44 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "microp.h"
#include "micro_args.h"
#ifndef MICRO_ARGS_H_
#define MICRO_ARGS_H_

/**
* Request unmarshaled as "arguments", a space-separated list of numbers and strings.
* TODO document the interface.
*/
typedef struct args_s microArgs;

struct args_s
{
void **args;
int count;
};

static microError *parse(void **args, int *args_len, const char *data, int data_len);

static inline microError *new_args(microArgs **ptr, int n)
{
*ptr = NATS_CALLOC(1, sizeof(microArgs));
*ptr = calloc(1, sizeof(microArgs));
if (*ptr == NULL)
return micro_ErrorOutOfMemory;

(*ptr)->args = NATS_CALLOC(n, sizeof(void *));
(*ptr)->args = calloc(n, sizeof(void *));
if ((*ptr)->args == NULL)
{
NATS_FREE(*ptr);
free(*ptr);
return micro_ErrorOutOfMemory;
}

(*ptr)->count = n;
return NULL;
}

microError *
micro_ParseArgs(microArgs **ptr, const char *data, int data_len)
{
microError *err = NULL;
microArgs *args = NULL;
int n = 0;

if ((ptr == NULL) || (data == NULL) || (data_len < 0))
return microError_Wrapf(micro_ErrorInvalidArg, "failed to parse args");

MICRO_CALL(err, parse(NULL, &n, data, data_len));
MICRO_CALL(err, new_args(&args, n));
MICRO_CALL(err, parse(args->args, &n, data, data_len));

if (err != NULL)
{
microArgs_Destroy(args);
return microError_Wrapf(err, "failed to parse args");
}
*ptr = args;
return NULL;
}

void microArgs_Destroy(microArgs *args)
{
int i;

if (args == NULL)
return;

for (i = 0; i < args->count; i++)
{
NATS_FREE(args->args[i]);
}
NATS_FREE(args->args);
NATS_FREE(args);
}

int microArgs_Count(microArgs *args)
{
if (args == NULL)
return 0;

return args->count;
}

microError *
microArgs_GetInt(int *val, microArgs *args, int index)
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = *((int *)args->args[index]);
return NULL;
}

microError *
microArgs_GetFloat(long double *val, microArgs *args, int index)
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = *((long double *)args->args[index]);
return NULL;
}

microError *
microArgs_GetString(const char **val, microArgs *args, int index)
typedef enum parserState
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = (const char *)args->args[index];
return NULL;
}
NewArg = 0,
NumberArg,
} parserState;

// decodes the rest of a string into a pre-allocated buffer of sufficient
// length, or just calculates the needed buffer size. The opening quote must
Expand Down Expand Up @@ -205,7 +140,7 @@ decode_and_dupe_rest_of_string(char **dup, int *i, const char *data, int data_le

*i = start;

*dup = NATS_CALLOC(decoded_len + 1, sizeof(char));
*dup = calloc(decoded_len + 1, sizeof(char));
if (*dup == NULL)
{
return micro_ErrorOutOfMemory;
Expand All @@ -218,12 +153,6 @@ decode_and_dupe_rest_of_string(char **dup, int *i, const char *data, int data_le
return NULL;
}

typedef enum parserState
{
NewArg = 0,
NumberArg,
} parserState;

static microError *
parse(void **args, int *args_len, const char *data, int data_len)
{
Expand Down Expand Up @@ -318,7 +247,7 @@ parse(void **args, int *args_len, const char *data, int data_len)
numbuf[num_len] = 0;
if (is_float)
{
args[n] = NATS_CALLOC(1, sizeof(long double));
args[n] = calloc(1, sizeof(long double));
if (args[n] == NULL)
{
return micro_ErrorOutOfMemory;
Expand All @@ -327,7 +256,7 @@ parse(void **args, int *args_len, const char *data, int data_len)
}
else
{
args[n] = NATS_CALLOC(1, sizeof(int));
args[n] = calloc(1, sizeof(int));
if (args[n] == NULL)
{
return micro_ErrorOutOfMemory;
Expand All @@ -353,3 +282,85 @@ parse(void **args, int *args_len, const char *data, int data_len)
*args_len = n;
return NULL;
}

static inline void microArgs_Destroy(microArgs *args)
{
int i;

if (args == NULL)
return;

for (i = 0; i < args->count; i++)
{
free(args->args[i]);
}
free(args->args);
free(args);
}

static microError *
micro_ParseArgs(microArgs **ptr, const char *data, int data_len)
{
microError *err = NULL;
microArgs *args = NULL;
int n = 0;

if ((ptr == NULL) || (data == NULL) || (data_len < 0))
return microError_Wrapf(micro_ErrorInvalidArg, "failed to parse args");

if (err == NULL)
err = parse(NULL, &n, data, data_len);
if (err == NULL)
err = new_args(&args, n);
if (err == NULL)
err = parse(args->args, &n, data, data_len);

if (err != NULL)
{
microArgs_Destroy(args);
return microError_Wrapf(err, "failed to parse args");
}
*ptr = args;
return NULL;
}

static inline int microArgs_Count(microArgs *args)
{
if (args == NULL)
return 0;

return args->count;
}

static inline microError *
microArgs_GetInt(int *val, microArgs *args, int index)
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = *((int *)args->args[index]);
return NULL;
}

static inline microError *
microArgs_GetFloat(long double *val, microArgs *args, int index)
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = *((long double *)args->args[index]);
return NULL;
}

static inline microError *
microArgs_GetString(const char **val, microArgs *args, int index)
{
if ((args == NULL) || (index < 0) || (index >= args->count) || (val == NULL))
return micro_ErrorInvalidArg;

*val = (const char *)args->args[index];
return NULL;
}


#endif /* MICRO_H_ */
43 changes: 0 additions & 43 deletions src/micro_args.h

This file was deleted.

3 changes: 0 additions & 3 deletions src/microp.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ struct micro_request_s
microEndpoint *Endpoint;
};

extern microError *micro_ErrorOutOfMemory;
extern microError *micro_ErrorInvalidArg;

microError *micro_add_endpoint(microEndpoint **new_ep, microService *m, const char *prefix, microEndpointConfig *cfg, bool is_internal);
microError *micro_clone_endpoint_config(microEndpointConfig **out, microEndpointConfig *cfg);
microError *micro_init_monitoring(microService *m);
Expand Down
4 changes: 4 additions & 0 deletions src/nats.h
Original file line number Diff line number Diff line change
Expand Up @@ -7404,6 +7404,10 @@ typedef struct micro_service_info_s microServiceInfo;
*/
typedef struct micro_service_stats_s microServiceStats;


NATS_EXTERN microError *micro_ErrorOutOfMemory;
NATS_EXTERN microError *micro_ErrorInvalidArg;

/** @} */ // end of microTypes

/** \defgroup microCallbacks Callbacks
Expand Down
2 changes: 1 addition & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24325,7 +24325,7 @@ test_JetStreamMgtConsumers(void)
cfg.Name = "my_name";
cfg.DeliverSubject = "mn.foo";
cfg.FilterSubject = "bar.>";
#define TIME_20350101 (2051251200L * 1000000000L)
#define TIME_20350101 ((int64_t)2051251200L * (int64_t)1000000000L)
if (serverVersionAtLeast(2, 11, 0))
{
cfg.PauseUntil = TIME_20350101;
Expand Down