File: e_script.c

package info (click to toggle)
kobodeluxe 0.5.1-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,324 kB
  • ctags: 4,853
  • sloc: ansic: 18,747; cpp: 18,203; sh: 3,191; makefile: 160
file content (159 lines) | stat: -rw-r--r-- 4,034 bytes parent folder | download | duplicates (5)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*(LGPL)
---------------------------------------------------------------------------
	e_script.c - EEL source code management
---------------------------------------------------------------------------
 * Copyright (C) 2002, David Olofson
 * Copyright (C) 2002, Florian Schulze <fs@crowproductions.de>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "_e_script.h"
#include "e_util.h"

#define	DBG(x)


/*----------------------------------------------------------
	Script source management
----------------------------------------------------------*/

script_t eel_scripttab[MAX_SCRIPTS];

int eel_script_alloc()
{
        int h = 0;
        while(h < MAX_SCRIPTS)
        {
                if(!eel_scripttab[h].data)
                        return h;
                ++h;
        }
        return -1;
}


int eel_load(const char *filename)
{
	FILE *f;
	int h = eel_script_alloc();
	if(h < 0)
		return h;

	/* Construct full name with path */
	eel_scripttab[h].name = malloc(strlen(filename) +
			strlen(eel_path()) + 2);
	if(!eel_scripttab[h].name)
		return -1;
	strcpy(eel_scripttab[h].name, eel_path());
#ifdef WIN32
	strcat(eel_scripttab[h].name, "\\");
#elif defined MACOS
	strcat(eel_scripttab[h].name, ":");
#else
	strcat(eel_scripttab[h].name, "/");
#endif
	strcat(eel_scripttab[h].name, filename);

	f = fopen(eel_scripttab[h].name, "rb");
	if(!f)
		return -1;

	if(fseek(f, 0, SEEK_END) == 0)
	{
		eel_scripttab[h].len = ftell(f);
		if(fseek(f, 0, SEEK_SET) == 0)
		{
			eel_scripttab[h].data = (unsigned char *)malloc(
					eel_scripttab[h].len + 1);
			if(eel_scripttab[h].data)
			{
				if(fread(eel_scripttab[h].data,
						eel_scripttab[h].len,
						1, f) == 1)
				{
					fclose(f);
					DBG(log_printf(DLOG, "Loaded script \"%s\"."
							" (Handle %d)\n",
							eel_scripttab[h].name,
							h);)
					return h;
				}
				DBG(printf("eel_load(): Read error!\n");)
				eel_free(h);
			}
			DBG(printf("eel_load(): Memory allocation error!\n");)
		}
		DBG(printf("eel_load(): Rewind error!\n");)
	}
	DBG(printf("eel_load(): Seek-to-end error!\n");)
	fclose(f);
	return -2;
}


int eel_load_from_mem(const char *script, unsigned len)
{
	char name_buffer[128];
	int h = eel_script_alloc();
	if(h < 0)
		return h;

	/* Construct name */
	snprintf(name_buffer, 127, "mem_script (%p, %u)", script, len);
	name_buffer[127] = '\0';
	eel_scripttab[h].name = (char *)malloc(strlen(name_buffer) + 1);
	if(!eel_scripttab[h].name)
		return -1;

	strcpy(eel_scripttab[h].name, name_buffer);

	eel_scripttab[h].len = len;
	eel_scripttab[h].data =
			(unsigned char *)malloc(eel_scripttab[h].len + 1);
	if(!eel_scripttab[h].data)	
	{
		DBG(printf("eel_load_from_mem(): Memory allocation error!\n");)
		return -2;
	}
	memcpy(eel_scripttab[h].data, script, len);
	DBG(printf("Loaded script \"%s\" (Handle %d)\n",
				eel_scripttab[h].name, h);)
	return h;
}


void eel_free(int handle)
{
	if(handle < 0)
		return;
	if(handle > MAX_SCRIPTS)
		return;
	if(!eel_scripttab[handle].data)
		return;

	DBG(printf("Freeing script \"%s\". (Handle %d)\n",
			eel_scripttab[handle].name, handle);)
	free(eel_scripttab[handle].data);
	eel_scripttab[handle].data = NULL;
	free(eel_scripttab[handle].name);
	eel_scripttab[handle].name = NULL;
	eel_scripttab[handle].len = 0;
}