UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scriplib.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 1997-2001 Id Software, Inc.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 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 =============================================================================
32 PARSING STUFF
33 =============================================================================
34 */
35 
36 typedef struct {
38  char* buffer;
39  const char* script_p;
40  const char* end_p;
41 } script_t;
42 
44 
46 
47 void 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 
62 void 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 
75 const 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 
92 bool 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 }
bool Q_strnull(const char *string)
Definition: shared.h:138
void Sys_Error(const char *error,...)
Definition: g_main.cpp:421
char * buffer
Definition: scriplib.cpp:38
const char * filename
Definition: ioapi.h:41
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 LoadScriptFile(const char *filename)
Definition: scriplib.cpp:47
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
GLsizei size
Definition: r_gl.h:152
#define MAX_TOKEN_CHARS
Definition: defines.h:372
char parsedToken[MAX_TOKEN_CHARS]
Definition: scriplib.cpp:45
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
const char * script_p
Definition: scriplib.cpp:39
const char * end_p
Definition: scriplib.cpp:40
#define Mem_Free(ptr)
Definition: mem.h:35
const char * GetToken()
Parses the next token from the current script on the stack and store the result in parsedToken...
Definition: scriplib.cpp:75
#define Q_streq(a, b)
Definition: shared.h:136
bool TokenAvailable(void)
Returns true if there is another token on the line.
Definition: scriplib.cpp:92
char filename[MAX_OSPATH]
Definition: scriplib.cpp:37
uint8_t byte
Definition: ufotypes.h:34
void ParseFromMemory(char *buffer, int size)
Parses e.g. the entity string that is already stored in memory.
Definition: scriplib.cpp:62
static script_t script
Definition: scriplib.cpp:43