summaryrefslogtreecommitdiff
path: root/libs/honeysuckle/src/hs_pushstring.c
blob: f7da0d0f2d633a6b2e7053a5b8654dbd089cc505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdlib.h>

#include "honeysuckle.h"

void hs_vpushstring(lua_State *L, const char *format_string, va_list args)
{
   va_list args_;
   va_copy(args_, args);

   int string_size = vsnprintf(NULL, 0, format_string, args_);
   va_end(args_);

   char *string = malloc((string_size+1) * sizeof(char));
   if (string == NULL) {
      lua_pushstring(L, "there was an error allocating memory for a string");
      lua_error(L);
   }

   vsnprintf(string, string_size+1, format_string, args);
   lua_pushstring(L, string);
   free(string);
}


void hs_pushstring(lua_State *L, const char *format_string, ...)
{
   va_list args;
   va_start(args, format_string);
   hs_vpushstring(L, format_string, args);
   va_end(args);
}