lua_ucl.c (d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a) lua_ucl.c (11dd9ed6647d821e7b43d4f8e64412a2623fbab5)
1/* Copyright (c) 2014, Vsevolod Stakhov
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright

--- 15 unchanged lines hidden (view full) ---

24/**
25 * @file lua ucl bindings
26 */
27
28#include "ucl.h"
29#include "ucl_internal.h"
30#include "lua_ucl.h"
31#include <strings.h>
1/* Copyright (c) 2014, Vsevolod Stakhov
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright

--- 15 unchanged lines hidden (view full) ---

24/**
25 * @file lua ucl bindings
26 */
27
28#include "ucl.h"
29#include "ucl_internal.h"
30#include "lua_ucl.h"
31#include <strings.h>
32#include <zconf.h>
33
34/***
35 * @module ucl
36 * This lua module allows to parse objects from strings and to store data into
37 * ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
38 * @example
39local ucl = require("ucl")
40

--- 141 unchanged lines hidden (view full) ---

182 it = ucl_object_iterate_new (obj);
183 lua_createtable (L, nelt, 0);
184
185 while ((cur = ucl_object_iterate_safe (it, true))) {
186 ucl_object_push_lua (L, cur, false);
187 lua_rawseti (L, -2, i);
188 i ++;
189 }
32
33/***
34 * @module ucl
35 * This lua module allows to parse objects from strings and to store data into
36 * ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
37 * @example
38local ucl = require("ucl")
39

--- 141 unchanged lines hidden (view full) ---

181 it = ucl_object_iterate_new (obj);
182 lua_createtable (L, nelt, 0);
183
184 while ((cur = ucl_object_iterate_safe (it, true))) {
185 ucl_object_push_lua (L, cur, false);
186 lua_rawseti (L, -2, i);
187 i ++;
188 }
189
190 ucl_object_iterate_free (it);
190 }
191 else {
192 /* Optimize allocation by preallocation of table */
193 LL_FOREACH (obj, cur) {
194 nelt ++;
195 }
196
197 lua_createtable (L, nelt, 0);

--- 279 unchanged lines hidden (view full) ---

477
478 return 1;
479}
480
481static int
482lua_ucl_parser_init (lua_State *L)
483{
484 struct ucl_parser *parser, **pparser;
191 }
192 else {
193 /* Optimize allocation by preallocation of table */
194 LL_FOREACH (obj, cur) {
195 nelt ++;
196 }
197
198 lua_createtable (L, nelt, 0);

--- 279 unchanged lines hidden (view full) ---

478
479 return 1;
480}
481
482static int
483lua_ucl_parser_init (lua_State *L)
484{
485 struct ucl_parser *parser, **pparser;
485 int flags = 0;
486 int flags = UCL_PARSER_NO_FILEVARS;
486
487 if (lua_gettop (L) >= 1) {
488 flags = lua_tonumber (L, 1);
489 }
490
491 parser = ucl_parser_new (flags);
492 if (parser == NULL) {
493 lua_pushnil (L);

--- 25 unchanged lines hidden (view full) ---

519 ucl_object_t **pobj;
520
521 pobj = lua_newuserdata (L, sizeof (*pobj));
522 *pobj = obj;
523 luaL_getmetatable (L, OBJECT_META);
524 lua_setmetatable (L, -2);
525}
526
487
488 if (lua_gettop (L) >= 1) {
489 flags = lua_tonumber (L, 1);
490 }
491
492 parser = ucl_parser_new (flags);
493 if (parser == NULL) {
494 lua_pushnil (L);

--- 25 unchanged lines hidden (view full) ---

520 ucl_object_t **pobj;
521
522 pobj = lua_newuserdata (L, sizeof (*pobj));
523 *pobj = obj;
524 luaL_getmetatable (L, OBJECT_META);
525 lua_setmetatable (L, -2);
526}
527
528static inline enum ucl_parse_type
529lua_ucl_str_to_parse_type (const char *str)
530{
531 enum ucl_parse_type type = UCL_PARSE_UCL;
532
533 if (str != NULL) {
534 if (strcasecmp (str, "msgpack") == 0) {
535 type = UCL_PARSE_MSGPACK;
536 }
537 else if (strcasecmp (str, "sexp") == 0 ||
538 strcasecmp (str, "csexp") == 0) {
539 type = UCL_PARSE_CSEXP;
540 }
541 else if (strcasecmp (str, "auto") == 0) {
542 type = UCL_PARSE_AUTO;
543 }
544 }
545
546 return type;
547}
548
527/***
528 * @method parser:parse_file(name)
529 * Parse UCL object from file.
530 * @param {string} name filename to parse
531 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
532@example
533local parser = ucl.parser()
534local res,err = parser:parse_file('/some/file.conf')

--- 39 unchanged lines hidden (view full) ---

574 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
575 */
576static int
577lua_ucl_parser_parse_string (lua_State *L)
578{
579 struct ucl_parser *parser;
580 const char *string;
581 size_t llen;
549/***
550 * @method parser:parse_file(name)
551 * Parse UCL object from file.
552 * @param {string} name filename to parse
553 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
554@example
555local parser = ucl.parser()
556local res,err = parser:parse_file('/some/file.conf')

--- 39 unchanged lines hidden (view full) ---

596 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
597 */
598static int
599lua_ucl_parser_parse_string (lua_State *L)
600{
601 struct ucl_parser *parser;
602 const char *string;
603 size_t llen;
604 enum ucl_parse_type type = UCL_PARSE_UCL;
582 int ret = 2;
583
584 parser = lua_ucl_parser_get (L, 1);
585 string = luaL_checklstring (L, 2, &llen);
586
605 int ret = 2;
606
607 parser = lua_ucl_parser_get (L, 1);
608 string = luaL_checklstring (L, 2, &llen);
609
610 if (lua_type (L, 3) == LUA_TSTRING) {
611 type = lua_ucl_str_to_parse_type (lua_tostring (L, 3));
612 }
613
587 if (parser != NULL && string != NULL) {
614 if (parser != NULL && string != NULL) {
588 if (ucl_parser_add_chunk (parser, (const unsigned char *)string, llen)) {
615 if (ucl_parser_add_chunk_full (parser, (const unsigned char *)string,
616 llen, 0, UCL_DUPLICATE_APPEND, type)) {
589 lua_pushboolean (L, true);
590 ret = 1;
591 }
592 else {
593 lua_pushboolean (L, false);
594 lua_pushstring (L, ucl_parser_get_error (parser));
595 }
596 }

--- 159 unchanged lines hidden (view full) ---

756 }
757 else {
758 lua_pushnil (L);
759 }
760
761 return 1;
762}
763
617 lua_pushboolean (L, true);
618 ret = 1;
619 }
620 else {
621 lua_pushboolean (L, false);
622 lua_pushstring (L, ucl_parser_get_error (parser));
623 }
624 }

--- 159 unchanged lines hidden (view full) ---

784 }
785 else {
786 lua_pushnil (L);
787 }
788
789 return 1;
790}
791
792static inline enum ucl_emitter
793lua_ucl_str_to_emit_type (const char *strtype)
794{
795 enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
796
797 if (strcasecmp (strtype, "json") == 0) {
798 format = UCL_EMIT_JSON;
799 }
800 else if (strcasecmp (strtype, "json-compact") == 0) {
801 format = UCL_EMIT_JSON_COMPACT;
802 }
803 else if (strcasecmp (strtype, "yaml") == 0) {
804 format = UCL_EMIT_YAML;
805 }
806 else if (strcasecmp (strtype, "config") == 0 ||
807 strcasecmp (strtype, "ucl") == 0) {
808 format = UCL_EMIT_CONFIG;
809 }
810
811 return format;
812}
813
764/***
765 * @method object:tostring(type)
766 * Unwraps opaque ucl object to string (json by default). Optionally you can
767 * specify output format:
768 *
769 * - `json` - fine printed json
770 * - `json-compact` - compacted json
771 * - `config` - fine printed configuration

--- 10 unchanged lines hidden (view full) ---

782
783 obj = lua_ucl_object_get (L, 1);
784
785 if (obj) {
786 if (lua_gettop (L) > 1) {
787 if (lua_type (L, 2) == LUA_TSTRING) {
788 const char *strtype = lua_tostring (L, 2);
789
814/***
815 * @method object:tostring(type)
816 * Unwraps opaque ucl object to string (json by default). Optionally you can
817 * specify output format:
818 *
819 * - `json` - fine printed json
820 * - `json-compact` - compacted json
821 * - `config` - fine printed configuration

--- 10 unchanged lines hidden (view full) ---

832
833 obj = lua_ucl_object_get (L, 1);
834
835 if (obj) {
836 if (lua_gettop (L) > 1) {
837 if (lua_type (L, 2) == LUA_TSTRING) {
838 const char *strtype = lua_tostring (L, 2);
839
790 if (strcasecmp (strtype, "json") == 0) {
791 format = UCL_EMIT_JSON;
792 }
793 else if (strcasecmp (strtype, "json-compact") == 0) {
794 format = UCL_EMIT_JSON_COMPACT;
795 }
796 else if (strcasecmp (strtype, "yaml") == 0) {
797 format = UCL_EMIT_YAML;
798 }
799 else if (strcasecmp (strtype, "config") == 0 ||
800 strcasecmp (strtype, "ucl") == 0) {
801 format = UCL_EMIT_CONFIG;
802 }
840 format = lua_ucl_str_to_emit_type (strtype);
803 }
804 }
805
806 return lua_ucl_to_string (L, obj, format);
807 }
808 else {
809 lua_pushnil (L);
810 }

--- 272 unchanged lines hidden (view full) ---

1083 }
1084 else if (strcasecmp (strtype, "yaml") == 0) {
1085 format = UCL_EMIT_YAML;
1086 }
1087 else if (strcasecmp (strtype, "config") == 0 ||
1088 strcasecmp (strtype, "ucl") == 0) {
1089 format = UCL_EMIT_CONFIG;
1090 }
841 }
842 }
843
844 return lua_ucl_to_string (L, obj, format);
845 }
846 else {
847 lua_pushnil (L);
848 }

--- 272 unchanged lines hidden (view full) ---

1121 }
1122 else if (strcasecmp (strtype, "yaml") == 0) {
1123 format = UCL_EMIT_YAML;
1124 }
1125 else if (strcasecmp (strtype, "config") == 0 ||
1126 strcasecmp (strtype, "ucl") == 0) {
1127 format = UCL_EMIT_CONFIG;
1128 }
1129 else if (strcasecmp (strtype, "msgpack") == 0) {
1130 format = UCL_EMIT_MSGPACK;
1131 }
1091 }
1092 }
1093
1094 obj = ucl_object_lua_import (L, 1);
1095 if (obj != NULL) {
1096 lua_ucl_to_string (L, obj, format);
1097 ucl_object_unref (obj);
1098 }

--- 75 unchanged lines hidden ---
1132 }
1133 }
1134
1135 obj = ucl_object_lua_import (L, 1);
1136 if (obj != NULL) {
1137 lua_ucl_to_string (L, obj, format);
1138 ucl_object_unref (obj);
1139 }

--- 75 unchanged lines hidden ---