util.h
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 72 d049c92d0809
permissions -rw-r--r--
Fix module loading problem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     1
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     2
/* Copyright 2009 Myhailo Danylenko
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     3
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     4
This code is free software: you can redistribute it and/or modify
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     5
it under the terms of the GNU General Public License as published by
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     6
the Free Software Foundation, either version 2 of the License, or
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     7
(at your option) any later version.
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     8
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
     9
This program is distributed in the hope that it will be useful,
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    10
but WITHOUT ANY WARRANTY; without even the implied warranty of
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    12
GNU General Public License for more details.
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    13
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    14
You should have received a copy of the GNU General Public License
d049c92d0809 True print
Myhailo Danylenko <isbear@ukrpost.net>
parents: 70
diff changeset
    15
along with this program.  If not, see <http://www.gnu.org/licenses/>. */
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
#ifndef MISC_LUA_UTIL_H
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
#define MISC_LUA_UTIL_H
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
#include <lua.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
// You can set this if anther type is required
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
#ifndef enum_value_t
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
#define enum_value_t int
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
#endif
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
// Array of string2eunm_t's must be ended with pair { NULL, fallback_value }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
typedef struct {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
	const char   *string;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
	enum_value_t  value;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
} string2enum_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
// If not found, fallback_value is returned
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
enum_value_t  string2enum (const char *string, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
// If not found, NULL is returned
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
const char   *enum2string (enum_value_t value, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
// returns result of string2enum on specified stack index, can handle plain numbers (no s2e called in this case)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
// pushes onto a stack result of enum2string on a given value, if not recognized, pushes value as number
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
void         luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
// as luaL_checknum, but additionally handles tables as a multiple flags set.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
// table can contain both array and hash key-value pairs
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
// hash entries are ORed with string2enum(key) if value resolves to lua true value
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
// array entries are always ORed with string2enum(value)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
// eg { flag1, flag2, 16, flag3 = true, flag4 = { }, flag5 = nil } will set all flags except flag5 (and 16 too will be ORed)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
// pushes to stack table with all matched values from a set in a hash format
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
// eg { flag1 = true, flag2 = true, flag6 = true, 16 } - that's it, if some bits will not match, they will be passed as a first array element.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
void         luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
void *luaL_malloc (lua_State *L, size_t size);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
void *luaL_realloc (lua_State *L, void *ptr, size_t osize, size_t nsize);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
void  luaL_free (lua_State *L, void *ptr);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
#endif
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58