UFO: Alien Invasion
Loading...
Searching...
No Matches
scriplib.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 1997-2001 Id Software, Inc.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24*/
25
26
27#include "shared.h"
28#include "scriplib.h"
29
30/*
31=============================================================================
32PARSING STUFF
33=============================================================================
34*/
35
36typedef struct {
38 char* buffer;
39 const char* script_p;
40 const char* end_p;
41} script_t;
42
44
46
47void LoadScriptFile (const char* filename)
48{
49 strncpy(script.filename, filename, sizeof(script.filename));
50
51 const int size = FS_LoadFile(script.filename, (byte**)&script.buffer);
52 if (size == -1)
53 Sys_Error("file '%s' doesn't exist", script.filename);
54
55 script.script_p = script.buffer;
56 script.end_p = script.buffer + size;
57}
58
62void ParseFromMemory (char* buffer, int size)
63{
64 Q_strncpyz(script.filename, "memory buffer", sizeof(script.filename));
65
66 script.buffer = buffer;
67 script.script_p = script.buffer;
68 script.end_p = script.buffer + size;
69}
70
75const char* GetToken ()
76{
77 const char* token = Com_Parse(&script.script_p, parsedToken, sizeof(parsedToken));
78 if (!script.script_p) {
79 /* not if the current script is a memory buffer */
80 if (!Q_streq(script.filename, "memory buffer"))
81 Mem_Free(script.buffer);
82 assert(Q_strnull(parsedToken));
83 return parsedToken;
84 }
85
86 return token;
87}
88
92bool TokenAvailable (void)
93{
94 const char* search_p = script.script_p;
95
96 if (search_p >= script.end_p)
97 return false;
98
99 while (*search_p <= ' ') {
100 if (*search_p == '\n')
101 return false;
102 search_p++;
103 if (search_p == script.end_p)
104 return false;
105 }
106
107 return true;
108}
#define MAX_TOKEN_CHARS
Definition defines.h:372
int FS_LoadFile(const char *path, byte **buffer)
Filenames are relative to the quake search path.
Definition files.cpp:384
#define MAX_OSPATH
Definition filesys.h:44
void Sys_Error(const char *error,...)
Definition g_main.cpp:421
voidpf void uLong size
Definition ioapi.h:42
const char * filename
Definition ioapi.h:41
#define Mem_Free(ptr)
Definition mem.h:35
const char * Com_Parse(const char *data_p[], char *target, size_t size, bool replaceWhitespaces)
Parse a token out of a string.
Definition parse.cpp:107
bool TokenAvailable(void)
Returns true if there is another token on the line.
Definition scriplib.cpp:92
void LoadScriptFile(const char *filename)
Definition scriplib.cpp:47
void ParseFromMemory(char *buffer, int size)
Parses e.g. the entity string that is already stored in memory.
Definition scriplib.cpp:62
char parsedToken[MAX_TOKEN_CHARS]
Definition scriplib.cpp:45
const char * GetToken()
Parses the next token from the current script on the stack and store the result in parsedToken.
Definition scriplib.cpp:75
static script_t script
Definition scriplib.cpp:43
#define Q_streq(a, b)
Definition shared.h:136
bool Q_strnull(const char *string)
Definition shared.h:138
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
char filename[MAX_OSPATH]
Definition scriplib.cpp:37
char * buffer
Definition scriplib.cpp:38
const char * script_p
Definition scriplib.cpp:39
const char * end_p
Definition scriplib.cpp:40