Skip to main content
added 1 character in body
Source Link
Will
  • 148
  • 6

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersomeslightly more straightforward than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Which in OP's case wouldmight be invoked like thisas:

char * buff = NULL;
CG_ASPRINTF(buff,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Which in OP's case would be invoked like this:

char * buff = NULL;
CG_ASPRINTF(buff,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and slightly more straightforward than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Which in OP's case might be invoked as:

char * buff = NULL;
CG_ASPRINTF(buff,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

added 29 characters in body
Source Link
Will
  • 148
  • 6

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Example invocationWhich in OP's case would be invoked like this:

char * strbuff = NULL;
FOO_ASPRINTFCG_ASPRINTF(strbuff,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Example invocation:

char * str = NULL;
FOO_ASPRINTF(str,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Which in OP's case would be invoked like this:

char * buff = NULL;
CG_ASPRINTF(buff,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

added 91 characters in body
Source Link
Will
  • 148
  • 6

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* wherereplace FOO iswith your codebase's preferred prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    char * str(_str) = malloc(byte_c + 1); \
    if (!str(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(str_str, __VA_ARGS__); \
} while (0)

Example invocation:

char * str = NULL;
FOO_ASPRINTF(str,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macro'smacros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(...) /* where FOO is your codebase's preferred prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    char * str = malloc(byte_c + 1); \
    if (!str) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(str, __VA_ARGS__); \
} while (0)

Some people might be allergic to macro's, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

There's nothing wrong with the other answers, but if you'd prefer a solution more portable than asprinf() and a bit less cumbersome than Jerry Coffin's vsnprintf() helper function solution, consider this variable length macro solution:

#define FOO_ASPRINTF(_str, ...) /* replace FOO with your codebase's prefix */ \
do { \
    int byte_c = snprintf(NULL, 0, __VA_ARGS__); \
    (_str) = malloc(byte_c + 1); \
    if (!(_str)) { \
        FOO_MALLOC_ERR; /* should depend on your codebase's malloc() policy */ \
    } \
    sprintf(_str, __VA_ARGS__); \
} while (0)

Example invocation:

char * str = NULL;
FOO_ASPRINTF(str,
    "%lld %f %f %f %f %f %f %f %f %f %f",
    state->last_update_time,
    p->health,
    p->max_health,
    p->satiation,
    p->max_satiation,
    s->milliseconds_per_tick,
    s->hunger_pain_per_tick,
    s->hunger_per_tick,
    s->satiated_heal_per_tick,
    s->pain_per_wrong_answer,
    s->satiation_per_right_answer
);

Some people might be allergic to macros, but in this case it allows easy implementation, avoids any overhead, and is perfectly portable.

Source Link
Will
  • 148
  • 6
Loading