hirlite 0.0.2 → 0.0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/ext/hirlite_ext/extconf.rb +2 -2
  3. data/lib/hirlite/version.rb +1 -1
  4. data/vendor/rlite/deps/lua/Makefile +128 -0
  5. data/vendor/rlite/deps/lua/src/fpconv.c +205 -0
  6. data/vendor/rlite/deps/lua/src/fpconv.h +22 -0
  7. data/vendor/rlite/deps/lua/src/lapi.c +1087 -0
  8. data/vendor/rlite/deps/lua/src/lapi.h +16 -0
  9. data/vendor/rlite/deps/lua/src/lauxlib.c +652 -0
  10. data/vendor/rlite/deps/lua/src/lauxlib.h +174 -0
  11. data/vendor/rlite/deps/lua/src/lbaselib.c +653 -0
  12. data/vendor/rlite/deps/lua/src/lcode.c +831 -0
  13. data/vendor/rlite/deps/lua/src/lcode.h +76 -0
  14. data/vendor/rlite/deps/lua/src/ldblib.c +398 -0
  15. data/vendor/rlite/deps/lua/src/ldebug.c +638 -0
  16. data/vendor/rlite/deps/lua/src/ldebug.h +33 -0
  17. data/vendor/rlite/deps/lua/src/ldo.c +519 -0
  18. data/vendor/rlite/deps/lua/src/ldo.h +57 -0
  19. data/vendor/rlite/deps/lua/src/ldump.c +164 -0
  20. data/vendor/rlite/deps/lua/src/lfunc.c +174 -0
  21. data/vendor/rlite/deps/lua/src/lfunc.h +34 -0
  22. data/vendor/rlite/deps/lua/src/lgc.c +710 -0
  23. data/vendor/rlite/deps/lua/src/lgc.h +110 -0
  24. data/vendor/rlite/deps/lua/src/linit.c +38 -0
  25. data/vendor/rlite/deps/lua/src/liolib.c +556 -0
  26. data/vendor/rlite/deps/lua/src/llex.c +463 -0
  27. data/vendor/rlite/deps/lua/src/llex.h +81 -0
  28. data/vendor/rlite/deps/lua/src/llimits.h +128 -0
  29. data/vendor/rlite/deps/lua/src/lmathlib.c +263 -0
  30. data/vendor/rlite/deps/lua/src/lmem.c +86 -0
  31. data/vendor/rlite/deps/lua/src/lmem.h +49 -0
  32. data/vendor/rlite/deps/lua/src/loadlib.c +666 -0
  33. data/vendor/rlite/deps/lua/src/lobject.c +214 -0
  34. data/vendor/rlite/deps/lua/src/lobject.h +381 -0
  35. data/vendor/rlite/deps/lua/src/lopcodes.c +102 -0
  36. data/vendor/rlite/deps/lua/src/lopcodes.h +268 -0
  37. data/vendor/rlite/deps/lua/src/loslib.c +243 -0
  38. data/vendor/rlite/deps/lua/src/lparser.c +1339 -0
  39. data/vendor/rlite/deps/lua/src/lparser.h +82 -0
  40. data/vendor/rlite/deps/lua/src/lstate.c +214 -0
  41. data/vendor/rlite/deps/lua/src/lstate.h +169 -0
  42. data/vendor/rlite/deps/lua/src/lstring.c +111 -0
  43. data/vendor/rlite/deps/lua/src/lstring.h +31 -0
  44. data/vendor/rlite/deps/lua/src/lstrlib.c +871 -0
  45. data/vendor/rlite/deps/lua/src/ltable.c +588 -0
  46. data/vendor/rlite/deps/lua/src/ltable.h +40 -0
  47. data/vendor/rlite/deps/lua/src/ltablib.c +287 -0
  48. data/vendor/rlite/deps/lua/src/ltm.c +75 -0
  49. data/vendor/rlite/deps/lua/src/ltm.h +54 -0
  50. data/vendor/rlite/deps/lua/src/lua.c +392 -0
  51. data/vendor/rlite/deps/lua/src/lua.h +388 -0
  52. data/vendor/rlite/deps/lua/src/lua_bit.c +189 -0
  53. data/vendor/rlite/deps/lua/src/lua_cjson.c +1427 -0
  54. data/vendor/rlite/deps/lua/src/lua_cmsgpack.c +957 -0
  55. data/vendor/rlite/deps/lua/src/lua_struct.c +421 -0
  56. data/vendor/rlite/deps/lua/src/luac.c +200 -0
  57. data/vendor/rlite/deps/lua/src/luaconf.h +763 -0
  58. data/vendor/rlite/deps/lua/src/lualib.h +53 -0
  59. data/vendor/rlite/deps/lua/src/lundump.c +227 -0
  60. data/vendor/rlite/deps/lua/src/lundump.h +36 -0
  61. data/vendor/rlite/deps/lua/src/lvm.c +767 -0
  62. data/vendor/rlite/deps/lua/src/lvm.h +36 -0
  63. data/vendor/rlite/deps/lua/src/lzio.c +82 -0
  64. data/vendor/rlite/deps/lua/src/lzio.h +67 -0
  65. data/vendor/rlite/deps/lua/src/print.c +227 -0
  66. data/vendor/rlite/deps/lua/src/strbuf.c +251 -0
  67. data/vendor/rlite/deps/lua/src/strbuf.h +154 -0
  68. data/vendor/rlite/src/Makefile +97 -25
  69. data/vendor/rlite/{deps → src}/crc64.c +0 -0
  70. data/vendor/rlite/{deps → src}/crc64.h +0 -0
  71. data/vendor/rlite/src/dump.c +2 -2
  72. data/vendor/rlite/{deps → src}/endianconv.h +0 -0
  73. data/vendor/rlite/src/hirlite.c +134 -90
  74. data/vendor/rlite/src/hirlite.h +34 -1
  75. data/vendor/rlite/{deps → src}/hyperloglog.c +0 -0
  76. data/vendor/rlite/{deps → src}/hyperloglog.h +0 -0
  77. data/vendor/rlite/{deps → src}/lzf.h +0 -0
  78. data/vendor/rlite/{deps → src}/lzfP.h +0 -0
  79. data/vendor/rlite/{deps → src}/lzf_c.c +0 -0
  80. data/vendor/rlite/{deps → src}/lzf_d.c +0 -0
  81. data/vendor/rlite/src/page_btree.c +10 -10
  82. data/vendor/rlite/src/page_btree.h +2 -2
  83. data/vendor/rlite/src/page_list.c +3 -3
  84. data/vendor/rlite/src/page_list.h +1 -1
  85. data/vendor/rlite/src/page_multi_string.c +1 -1
  86. data/vendor/rlite/src/page_skiplist.c +1 -1
  87. data/vendor/rlite/src/page_skiplist.h +1 -1
  88. data/vendor/rlite/src/rand.c +93 -0
  89. data/vendor/rlite/src/rand.h +38 -0
  90. data/vendor/rlite/src/restore.c +286 -155
  91. data/vendor/rlite/src/restore.h +6 -0
  92. data/vendor/rlite/src/rlite.c +30 -30
  93. data/vendor/rlite/src/rlite.h +1 -1
  94. data/vendor/rlite/src/scripting.c +1138 -0
  95. data/vendor/rlite/src/scripting.h +10 -0
  96. data/vendor/rlite/{deps → src}/sha1.c +0 -0
  97. data/vendor/rlite/{deps → src}/sha1.h +0 -0
  98. data/vendor/rlite/src/solarisfixes.h +54 -0
  99. data/vendor/rlite/src/sort.c +4 -2
  100. data/vendor/rlite/src/sort.h +1 -1
  101. data/vendor/rlite/src/type_string.c +1 -1
  102. data/vendor/rlite/src/util.c +4 -4
  103. data/vendor/rlite/src/util.h +3 -3
  104. data/vendor/rlite/{deps → src}/utilfromredis.c +0 -0
  105. data/vendor/rlite/{deps → src}/utilfromredis.h +0 -0
  106. metadata +84 -15
@@ -0,0 +1,36 @@
1
+ /*
2
+ ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Lua virtual machine
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+ #ifndef lvm_h
8
+ #define lvm_h
9
+
10
+
11
+ #include "ldo.h"
12
+ #include "lobject.h"
13
+ #include "ltm.h"
14
+
15
+
16
+ #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
17
+
18
+ #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
19
+ (((o) = luaV_tonumber(o,n)) != NULL))
20
+
21
+ #define equalobj(L,o1,o2) \
22
+ (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
23
+
24
+
25
+ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
26
+ LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
27
+ LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
28
+ LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
29
+ LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
30
+ StkId val);
31
+ LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
32
+ StkId val);
33
+ LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
34
+ LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
35
+
36
+ #endif
@@ -0,0 +1,82 @@
1
+ /*
2
+ ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** a generic input stream interface
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #include <string.h>
9
+
10
+ #define lzio_c
11
+ #define LUA_CORE
12
+
13
+ #include "lua.h"
14
+
15
+ #include "llimits.h"
16
+ #include "lmem.h"
17
+ #include "lstate.h"
18
+ #include "lzio.h"
19
+
20
+
21
+ int luaZ_fill (ZIO *z) {
22
+ size_t size;
23
+ lua_State *L = z->L;
24
+ const char *buff;
25
+ lua_unlock(L);
26
+ buff = z->reader(L, z->data, &size);
27
+ lua_lock(L);
28
+ if (buff == NULL || size == 0) return EOZ;
29
+ z->n = size - 1;
30
+ z->p = buff;
31
+ return char2int(*(z->p++));
32
+ }
33
+
34
+
35
+ int luaZ_lookahead (ZIO *z) {
36
+ if (z->n == 0) {
37
+ if (luaZ_fill(z) == EOZ)
38
+ return EOZ;
39
+ else {
40
+ z->n++; /* luaZ_fill removed first byte; put back it */
41
+ z->p--;
42
+ }
43
+ }
44
+ return char2int(*z->p);
45
+ }
46
+
47
+
48
+ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
49
+ z->L = L;
50
+ z->reader = reader;
51
+ z->data = data;
52
+ z->n = 0;
53
+ z->p = NULL;
54
+ }
55
+
56
+
57
+ /* --------------------------------------------------------------- read --- */
58
+ size_t luaZ_read (ZIO *z, void *b, size_t n) {
59
+ while (n) {
60
+ size_t m;
61
+ if (luaZ_lookahead(z) == EOZ)
62
+ return n; /* return number of missing bytes */
63
+ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
64
+ memcpy(b, z->p, m);
65
+ z->n -= m;
66
+ z->p += m;
67
+ b = (char *)b + m;
68
+ n -= m;
69
+ }
70
+ return 0;
71
+ }
72
+
73
+ /* ------------------------------------------------------------------------ */
74
+ char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
75
+ if (n > buff->buffsize) {
76
+ if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
77
+ luaZ_resizebuffer(L, buff, n);
78
+ }
79
+ return buff->buffer;
80
+ }
81
+
82
+
@@ -0,0 +1,67 @@
1
+ /*
2
+ ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Buffered streams
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #ifndef lzio_h
9
+ #define lzio_h
10
+
11
+ #include "lua.h"
12
+
13
+ #include "lmem.h"
14
+
15
+
16
+ #define EOZ (-1) /* end of stream */
17
+
18
+ typedef struct Zio ZIO;
19
+
20
+ #define char2int(c) cast(int, cast(unsigned char, (c)))
21
+
22
+ #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
23
+
24
+ typedef struct Mbuffer {
25
+ char *buffer;
26
+ size_t n;
27
+ size_t buffsize;
28
+ } Mbuffer;
29
+
30
+ #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
31
+
32
+ #define luaZ_buffer(buff) ((buff)->buffer)
33
+ #define luaZ_sizebuffer(buff) ((buff)->buffsize)
34
+ #define luaZ_bufflen(buff) ((buff)->n)
35
+
36
+ #define luaZ_resetbuffer(buff) ((buff)->n = 0)
37
+
38
+
39
+ #define luaZ_resizebuffer(L, buff, size) \
40
+ (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
41
+ (buff)->buffsize = size)
42
+
43
+ #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
44
+
45
+
46
+ LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
47
+ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
48
+ void *data);
49
+ LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
50
+ LUAI_FUNC int luaZ_lookahead (ZIO *z);
51
+
52
+
53
+
54
+ /* --------- Private Part ------------------ */
55
+
56
+ struct Zio {
57
+ size_t n; /* bytes still unread */
58
+ const char *p; /* current position in buffer */
59
+ lua_Reader reader;
60
+ void* data; /* additional data */
61
+ lua_State *L; /* Lua state (for reader) */
62
+ };
63
+
64
+
65
+ LUAI_FUNC int luaZ_fill (ZIO *z);
66
+
67
+ #endif
@@ -0,0 +1,227 @@
1
+ /*
2
+ ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
3
+ ** print bytecodes
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+ #include <ctype.h>
8
+ #include <stdio.h>
9
+
10
+ #define luac_c
11
+ #define LUA_CORE
12
+
13
+ #include "ldebug.h"
14
+ #include "lobject.h"
15
+ #include "lopcodes.h"
16
+ #include "lundump.h"
17
+
18
+ #define PrintFunction luaU_print
19
+
20
+ #define Sizeof(x) ((int)sizeof(x))
21
+ #define VOID(p) ((const void*)(p))
22
+
23
+ static void PrintString(const TString* ts)
24
+ {
25
+ const char* s=getstr(ts);
26
+ size_t i,n=ts->tsv.len;
27
+ putchar('"');
28
+ for (i=0; i<n; i++)
29
+ {
30
+ int c=s[i];
31
+ switch (c)
32
+ {
33
+ case '"': printf("\\\""); break;
34
+ case '\\': printf("\\\\"); break;
35
+ case '\a': printf("\\a"); break;
36
+ case '\b': printf("\\b"); break;
37
+ case '\f': printf("\\f"); break;
38
+ case '\n': printf("\\n"); break;
39
+ case '\r': printf("\\r"); break;
40
+ case '\t': printf("\\t"); break;
41
+ case '\v': printf("\\v"); break;
42
+ default: if (isprint((unsigned char)c))
43
+ putchar(c);
44
+ else
45
+ printf("\\%03u",(unsigned char)c);
46
+ }
47
+ }
48
+ putchar('"');
49
+ }
50
+
51
+ static void PrintConstant(const Proto* f, int i)
52
+ {
53
+ const TValue* o=&f->k[i];
54
+ switch (ttype(o))
55
+ {
56
+ case LUA_TNIL:
57
+ printf("nil");
58
+ break;
59
+ case LUA_TBOOLEAN:
60
+ printf(bvalue(o) ? "true" : "false");
61
+ break;
62
+ case LUA_TNUMBER:
63
+ printf(LUA_NUMBER_FMT,nvalue(o));
64
+ break;
65
+ case LUA_TSTRING:
66
+ PrintString(rawtsvalue(o));
67
+ break;
68
+ default: /* cannot happen */
69
+ printf("? type=%d",ttype(o));
70
+ break;
71
+ }
72
+ }
73
+
74
+ static void PrintCode(const Proto* f)
75
+ {
76
+ const Instruction* code=f->code;
77
+ int pc,n=f->sizecode;
78
+ for (pc=0; pc<n; pc++)
79
+ {
80
+ Instruction i=code[pc];
81
+ OpCode o=GET_OPCODE(i);
82
+ int a=GETARG_A(i);
83
+ int b=GETARG_B(i);
84
+ int c=GETARG_C(i);
85
+ int bx=GETARG_Bx(i);
86
+ int sbx=GETARG_sBx(i);
87
+ int line=getline(f,pc);
88
+ printf("\t%d\t",pc+1);
89
+ if (line>0) printf("[%d]\t",line); else printf("[-]\t");
90
+ printf("%-9s\t",luaP_opnames[o]);
91
+ switch (getOpMode(o))
92
+ {
93
+ case iABC:
94
+ printf("%d",a);
95
+ if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
96
+ if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
97
+ break;
98
+ case iABx:
99
+ if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
100
+ break;
101
+ case iAsBx:
102
+ if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
103
+ break;
104
+ }
105
+ switch (o)
106
+ {
107
+ case OP_LOADK:
108
+ printf("\t; "); PrintConstant(f,bx);
109
+ break;
110
+ case OP_GETUPVAL:
111
+ case OP_SETUPVAL:
112
+ printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
113
+ break;
114
+ case OP_GETGLOBAL:
115
+ case OP_SETGLOBAL:
116
+ printf("\t; %s",svalue(&f->k[bx]));
117
+ break;
118
+ case OP_GETTABLE:
119
+ case OP_SELF:
120
+ if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
121
+ break;
122
+ case OP_SETTABLE:
123
+ case OP_ADD:
124
+ case OP_SUB:
125
+ case OP_MUL:
126
+ case OP_DIV:
127
+ case OP_POW:
128
+ case OP_EQ:
129
+ case OP_LT:
130
+ case OP_LE:
131
+ if (ISK(b) || ISK(c))
132
+ {
133
+ printf("\t; ");
134
+ if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
135
+ printf(" ");
136
+ if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
137
+ }
138
+ break;
139
+ case OP_JMP:
140
+ case OP_FORLOOP:
141
+ case OP_FORPREP:
142
+ printf("\t; to %d",sbx+pc+2);
143
+ break;
144
+ case OP_CLOSURE:
145
+ printf("\t; %p",VOID(f->p[bx]));
146
+ break;
147
+ case OP_SETLIST:
148
+ if (c==0) printf("\t; %d",(int)code[++pc]);
149
+ else printf("\t; %d",c);
150
+ break;
151
+ default:
152
+ break;
153
+ }
154
+ printf("\n");
155
+ }
156
+ }
157
+
158
+ #define SS(x) (x==1)?"":"s"
159
+ #define S(x) x,SS(x)
160
+
161
+ static void PrintHeader(const Proto* f)
162
+ {
163
+ const char* s=getstr(f->source);
164
+ if (*s=='@' || *s=='=')
165
+ s++;
166
+ else if (*s==LUA_SIGNATURE[0])
167
+ s="(bstring)";
168
+ else
169
+ s="(string)";
170
+ printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
171
+ (f->linedefined==0)?"main":"function",s,
172
+ f->linedefined,f->lastlinedefined,
173
+ S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
174
+ printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
175
+ f->numparams,f->is_vararg?"+":"",SS(f->numparams),
176
+ S(f->maxstacksize),S(f->nups));
177
+ printf("%d local%s, %d constant%s, %d function%s\n",
178
+ S(f->sizelocvars),S(f->sizek),S(f->sizep));
179
+ }
180
+
181
+ static void PrintConstants(const Proto* f)
182
+ {
183
+ int i,n=f->sizek;
184
+ printf("constants (%d) for %p:\n",n,VOID(f));
185
+ for (i=0; i<n; i++)
186
+ {
187
+ printf("\t%d\t",i+1);
188
+ PrintConstant(f,i);
189
+ printf("\n");
190
+ }
191
+ }
192
+
193
+ static void PrintLocals(const Proto* f)
194
+ {
195
+ int i,n=f->sizelocvars;
196
+ printf("locals (%d) for %p:\n",n,VOID(f));
197
+ for (i=0; i<n; i++)
198
+ {
199
+ printf("\t%d\t%s\t%d\t%d\n",
200
+ i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
201
+ }
202
+ }
203
+
204
+ static void PrintUpvalues(const Proto* f)
205
+ {
206
+ int i,n=f->sizeupvalues;
207
+ printf("upvalues (%d) for %p:\n",n,VOID(f));
208
+ if (f->upvalues==NULL) return;
209
+ for (i=0; i<n; i++)
210
+ {
211
+ printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
212
+ }
213
+ }
214
+
215
+ void PrintFunction(const Proto* f, int full)
216
+ {
217
+ int i,n=f->sizep;
218
+ PrintHeader(f);
219
+ PrintCode(f);
220
+ if (full)
221
+ {
222
+ PrintConstants(f);
223
+ PrintLocals(f);
224
+ PrintUpvalues(f);
225
+ }
226
+ for (i=0; i<n; i++) PrintFunction(f->p[i],full);
227
+ }
@@ -0,0 +1,251 @@
1
+ /* strbuf - String buffer routines
2
+ *
3
+ * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <stdarg.h>
28
+ #include <string.h>
29
+
30
+ #include "strbuf.h"
31
+
32
+ static void die(const char *fmt, ...)
33
+ {
34
+ va_list arg;
35
+
36
+ va_start(arg, fmt);
37
+ vfprintf(stderr, fmt, arg);
38
+ va_end(arg);
39
+ fprintf(stderr, "\n");
40
+
41
+ exit(-1);
42
+ }
43
+
44
+ void strbuf_init(strbuf_t *s, int len)
45
+ {
46
+ int size;
47
+
48
+ if (len <= 0)
49
+ size = STRBUF_DEFAULT_SIZE;
50
+ else
51
+ size = len + 1; /* \0 terminator */
52
+
53
+ s->buf = NULL;
54
+ s->size = size;
55
+ s->length = 0;
56
+ s->increment = STRBUF_DEFAULT_INCREMENT;
57
+ s->dynamic = 0;
58
+ s->reallocs = 0;
59
+ s->debug = 0;
60
+
61
+ s->buf = malloc(size);
62
+ if (!s->buf)
63
+ die("Out of memory");
64
+
65
+ strbuf_ensure_null(s);
66
+ }
67
+
68
+ strbuf_t *strbuf_new(int len)
69
+ {
70
+ strbuf_t *s;
71
+
72
+ s = malloc(sizeof(strbuf_t));
73
+ if (!s)
74
+ die("Out of memory");
75
+
76
+ strbuf_init(s, len);
77
+
78
+ /* Dynamic strbuf allocation / deallocation */
79
+ s->dynamic = 1;
80
+
81
+ return s;
82
+ }
83
+
84
+ void strbuf_set_increment(strbuf_t *s, int increment)
85
+ {
86
+ /* Increment > 0: Linear buffer growth rate
87
+ * Increment < -1: Exponential buffer growth rate */
88
+ if (increment == 0 || increment == -1)
89
+ die("BUG: Invalid string increment");
90
+
91
+ s->increment = increment;
92
+ }
93
+
94
+ static inline void debug_stats(strbuf_t *s)
95
+ {
96
+ if (s->debug) {
97
+ fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n",
98
+ (long)s, s->reallocs, s->length, s->size);
99
+ }
100
+ }
101
+
102
+ /* If strbuf_t has not been dynamically allocated, strbuf_free() can
103
+ * be called any number of times strbuf_init() */
104
+ void strbuf_free(strbuf_t *s)
105
+ {
106
+ debug_stats(s);
107
+
108
+ if (s->buf) {
109
+ free(s->buf);
110
+ s->buf = NULL;
111
+ }
112
+ if (s->dynamic)
113
+ free(s);
114
+ }
115
+
116
+ char *strbuf_free_to_string(strbuf_t *s, int *len)
117
+ {
118
+ char *buf;
119
+
120
+ debug_stats(s);
121
+
122
+ strbuf_ensure_null(s);
123
+
124
+ buf = s->buf;
125
+ if (len)
126
+ *len = s->length;
127
+
128
+ if (s->dynamic)
129
+ free(s);
130
+
131
+ return buf;
132
+ }
133
+
134
+ static int calculate_new_size(strbuf_t *s, int len)
135
+ {
136
+ int reqsize, newsize;
137
+
138
+ if (len <= 0)
139
+ die("BUG: Invalid strbuf length requested");
140
+
141
+ /* Ensure there is room for optional NULL termination */
142
+ reqsize = len + 1;
143
+
144
+ /* If the user has requested to shrink the buffer, do it exactly */
145
+ if (s->size > reqsize)
146
+ return reqsize;
147
+
148
+ newsize = s->size;
149
+ if (s->increment < 0) {
150
+ /* Exponential sizing */
151
+ while (newsize < reqsize)
152
+ newsize *= -s->increment;
153
+ } else {
154
+ /* Linear sizing */
155
+ newsize = ((newsize + s->increment - 1) / s->increment) * s->increment;
156
+ }
157
+
158
+ return newsize;
159
+ }
160
+
161
+
162
+ /* Ensure strbuf can handle a string length bytes long (ignoring NULL
163
+ * optional termination). */
164
+ void strbuf_resize(strbuf_t *s, int len)
165
+ {
166
+ int newsize;
167
+
168
+ newsize = calculate_new_size(s, len);
169
+
170
+ if (s->debug > 1) {
171
+ fprintf(stderr, "strbuf(%lx) resize: %d => %d\n",
172
+ (long)s, s->size, newsize);
173
+ }
174
+
175
+ s->size = newsize;
176
+ s->buf = realloc(s->buf, s->size);
177
+ if (!s->buf)
178
+ die("Out of memory");
179
+ s->reallocs++;
180
+ }
181
+
182
+ void strbuf_append_string(strbuf_t *s, const char *str)
183
+ {
184
+ int space, i;
185
+
186
+ space = strbuf_empty_length(s);
187
+
188
+ for (i = 0; str[i]; i++) {
189
+ if (space < 1) {
190
+ strbuf_resize(s, s->length + 1);
191
+ space = strbuf_empty_length(s);
192
+ }
193
+
194
+ s->buf[s->length] = str[i];
195
+ s->length++;
196
+ space--;
197
+ }
198
+ }
199
+
200
+ /* strbuf_append_fmt() should only be used when an upper bound
201
+ * is known for the output string. */
202
+ void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...)
203
+ {
204
+ va_list arg;
205
+ int fmt_len;
206
+
207
+ strbuf_ensure_empty_length(s, len);
208
+
209
+ va_start(arg, fmt);
210
+ fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg);
211
+ va_end(arg);
212
+
213
+ if (fmt_len < 0)
214
+ die("BUG: Unable to convert number"); /* This should never happen.. */
215
+
216
+ s->length += fmt_len;
217
+ }
218
+
219
+ /* strbuf_append_fmt_retry() can be used when the there is no known
220
+ * upper bound for the output string. */
221
+ void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
222
+ {
223
+ va_list arg;
224
+ int fmt_len, try;
225
+ int empty_len;
226
+
227
+ /* If the first attempt to append fails, resize the buffer appropriately
228
+ * and try again */
229
+ for (try = 0; ; try++) {
230
+ va_start(arg, fmt);
231
+ /* Append the new formatted string */
232
+ /* fmt_len is the length of the string required, excluding the
233
+ * trailing NULL */
234
+ empty_len = strbuf_empty_length(s);
235
+ /* Add 1 since there is also space to store the terminating NULL. */
236
+ fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
237
+ va_end(arg);
238
+
239
+ if (fmt_len <= empty_len)
240
+ break; /* SUCCESS */
241
+ if (try > 0)
242
+ die("BUG: length of formatted string changed");
243
+
244
+ strbuf_resize(s, s->length + fmt_len);
245
+ }
246
+
247
+ s->length += fmt_len;
248
+ }
249
+
250
+ /* vi:ai et sw=4 ts=4:
251
+ */