Move save_string and make_string into strpool

Might not be the perfect place, but at least they're strongly related.
This commit is contained in:
Bill Currie 2011-01-24 21:31:32 +09:00
parent 483cbeba03
commit d618e51dc8
15 changed files with 231 additions and 217 deletions

View file

@ -115,18 +115,6 @@ extern struct symtab_s *current_symtab;
const char *strip_path (const char *filename);
/** Smart strdup.
Create a unique copy of a string. If the same string has been seen
before, does not create a new copy but rather returns the previously
seen string.
\param str The string to copy.
\return The unique copy of the string.
*/
const char *save_string (const char *str);
const char *make_string (char *token, char **end);
void clear_frame_macros (void);
extern FILE *yyin;
int yyparse (void);

View file

@ -43,4 +43,16 @@ strpool_t *strpool_build (const char *strings, int size);
void strpool_delete (strpool_t *strpool);
int strpool_addstr (strpool_t *strpool, const char *str);
/** Smart strdup.
Create a unique copy of a string. If the same string has been seen
before, does not create a new copy but rather returns the previously
seen string.
\param str The string to copy.
\return The unique copy of the string.
*/
const char *save_string (const char *str);
const char *make_string (char *token, char **end);
#endif//__strpool_h

View file

@ -49,6 +49,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "options.h"
#include "qc-parse.h"
#include "qfcc.h"
#include "strpool.h"
#include "type.h"
static expr_t *

View file

@ -49,6 +49,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "expr.h"
#include "immediate.h"
#include "qfcc.h"
#include "strpool.h"
static srcline_t *free_srclines;

View file

@ -60,6 +60,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "options.h"
#include "reloc.h"
#include "statements.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"

View file

@ -48,6 +48,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "grab.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
int grab_frame;
int grab_other;

View file

@ -52,6 +52,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "linker.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
const char *this_program;
const char **source_files;

View file

@ -48,13 +48,14 @@ static __attribute__ ((used)) const char rcsid[] =
#include <QF/hash.h>
#include <QF/sys.h>
#include "qfcc.h"
#include "class.h"
#include "debug.h"
#include "expr.h"
#include "grab.h"
#include "immediate.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
#include "struct.h"
#include "symtab.h"
#include "type.h"

View file

@ -57,6 +57,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "options.h"
#include "qfcc.h"
#include "reloc.h"
#include "strpool.h"
#include "struct.h"
#include "switch.h"
#include "symtab.h"

View file

@ -55,7 +55,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#endif
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <QF/cbuf.h>
@ -108,209 +107,6 @@ const char *big_function = 0;
ddef_t *fields;
int numfielddefs;
hashtab_t *saved_strings;
static const char *
ss_get_key (void *s, void *unused)
{
return (const char *)s;
}
const char *
save_string (const char *str)
{
char *s;
if (!saved_strings)
saved_strings = Hash_NewTable (16381, ss_get_key, 0, 0);
s = Hash_Find (saved_strings, str);
if (s)
return s;
s = strdup (str);
Hash_Add (saved_strings, s);
return s;
}
const char *
make_string (char *token, char **end)
{
char s[2];
int c;
int i;
int mask;
int boldnext;
int quote;
static dstring_t *str;
if (!str)
str = dstring_newstr ();
dstring_clearstr (str);
s[1] = 0;
mask = 0x00;
boldnext = 0;
quote = *token++;
do {
c = *token++;
if (!c)
error (0, "EOF inside quote");
if (c == '\n')
error (0, "newline inside quote");
if (c == '\\') { // escape char
c = *token++;
if (!c)
error (0, "EOF inside quote");
switch (c) {
case '\\':
c = '\\';
break;
case 'n':
c = '\n';
break;
case '"':
c = '\"';
break;
case '\'':
c = '\'';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if (!options.qccx_escapes) {
for (i = c = 0; i < 3
&& *token >= '0'
&& *token <= '7'; i++, token++) {
c *= 8;
c += *token - '0';
}
if (!*token)
error (0, "EOF inside quote");
break;
}
case '8':
case '9':
c = 18 + c - '0';
break;
case 'x':
c = 0;
while (*token && isxdigit ((unsigned char)*token)) {
c *= 16;
if (*token <= '9')
c += *token - '0';
else if (*token <= 'F')
c += *token - 'A' + 10;
else
c += *token - 'a' + 10;
token++;
}
if (!*token)
error (0, "EOF inside quote");
break;
case 'a':
c = '\a';
break;
case 'b':
if (options.qccx_escapes)
mask ^= 0x80;
else
c = '\b';
break;
case 'e':
c = '\033';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case '^':
if (*token == '\"')
error (0, "Unexpected end of string after \\^");
boldnext = 1;
continue;
case '[':
c = 0x90; // gold [
break;
case ']':
c = 0x91; // gold ]
break;
case '.':
c = 28; // center dot
break;
case '<':
if (options.qccx_escapes)
c = 29; // brown left end
else
mask = 0x80;
continue;
case '-':
c = 30; // brown center bit
break;
case '>':
if (options.qccx_escapes)
c = 31; // broun right end
else
mask = 0x00;
continue;
case '(':
c = 128; // left slider end
break;
case '=':
c = 129; // slider center
break;
case ')':
c = 130; // right slider end
break;
case '{':
c = 0;
while (*token && *token != '}'
&& isdigit ((unsigned char)*token)) {
c *= 10;
c += *token++ - '0';
}
if (!*token)
error (0, "EOF inside quote");
if (*token != '}')
error (0, "non-digit inside \\{}");
else
token++;
if (c > 255)
warning (0, "\\{%d} > 255", c);
break;
default:
error (0, "Unknown escape char");
break;
}
} else if (c == quote) {
break;
}
if (boldnext)
c = c ^ 0x80;
boldnext = 0;
c = c ^ mask;
s[0] = c;
dstring_appendstr (str, s);
} while (1);
if (end)
*end = token;
return save_string (str->str);
}
#ifdef _WIN32
char *
fix_backslash (char *path)

View file

@ -40,6 +40,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "expr.h"
#include "grab.h"
#include "qfcc.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"

View file

@ -46,6 +46,7 @@
#include "qc-parse.h"
#include "qfcc.h"
#include "statements.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"

View file

@ -41,11 +41,17 @@ static __attribute__ ((used)) const char rcsid[] =
# include <strings.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include "QF/dstring.h"
#include "QF/hash.h"
#include "expr.h"
#include "options.h"
#include "strpool.h"
static hashtab_t *saved_strings;
static const char *
strpool_get_key (void *_str, void *_strpool)
{
@ -116,3 +122,204 @@ strpool_addstr (strpool_t *strpool, const char *str)
Hash_Add (strpool->str_tab, (void *) s);
return s;
}
static const char *
ss_get_key (void *s, void *unused)
{
return (const char *)s;
}
const char *
save_string (const char *str)
{
char *s;
if (!saved_strings)
saved_strings = Hash_NewTable (16381, ss_get_key, 0, 0);
s = Hash_Find (saved_strings, str);
if (s)
return s;
s = strdup (str);
Hash_Add (saved_strings, s);
return s;
}
const char *
make_string (char *token, char **end)
{
char s[2];
int c;
int i;
int mask;
int boldnext;
int quote;
static dstring_t *str;
if (!str)
str = dstring_newstr ();
dstring_clearstr (str);
s[1] = 0;
mask = 0x00;
boldnext = 0;
quote = *token++;
do {
c = *token++;
if (!c)
error (0, "EOF inside quote");
if (c == '\n')
error (0, "newline inside quote");
if (c == '\\') { // escape char
c = *token++;
if (!c)
error (0, "EOF inside quote");
switch (c) {
case '\\':
c = '\\';
break;
case 'n':
c = '\n';
break;
case '"':
c = '\"';
break;
case '\'':
c = '\'';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if (!options.qccx_escapes) {
for (i = c = 0; i < 3
&& *token >= '0'
&& *token <= '7'; i++, token++) {
c *= 8;
c += *token - '0';
}
if (!*token)
error (0, "EOF inside quote");
break;
}
case '8':
case '9':
c = 18 + c - '0';
break;
case 'x':
c = 0;
while (*token && isxdigit ((unsigned char)*token)) {
c *= 16;
if (*token <= '9')
c += *token - '0';
else if (*token <= 'F')
c += *token - 'A' + 10;
else
c += *token - 'a' + 10;
token++;
}
if (!*token)
error (0, "EOF inside quote");
break;
case 'a':
c = '\a';
break;
case 'b':
if (options.qccx_escapes)
mask ^= 0x80;
else
c = '\b';
break;
case 'e':
c = '\033';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case '^':
if (*token == '\"')
error (0, "Unexpected end of string after \\^");
boldnext = 1;
continue;
case '[':
c = 0x90; // gold [
break;
case ']':
c = 0x91; // gold ]
break;
case '.':
c = 28; // center dot
break;
case '<':
if (options.qccx_escapes)
c = 29; // brown left end
else
mask = 0x80;
continue;
case '-':
c = 30; // brown center bit
break;
case '>':
if (options.qccx_escapes)
c = 31; // broun right end
else
mask = 0x00;
continue;
case '(':
c = 128; // left slider end
break;
case '=':
c = 129; // slider center
break;
case ')':
c = 130; // right slider end
break;
case '{':
c = 0;
while (*token && *token != '}'
&& isdigit ((unsigned char)*token)) {
c *= 10;
c += *token++ - '0';
}
if (!*token)
error (0, "EOF inside quote");
if (*token != '}')
error (0, "non-digit inside \\{}");
else
token++;
if (c > 255)
warning (0, "\\{%d} > 255", c);
break;
default:
error (0, "Unknown escape char");
break;
}
} else if (c == quote) {
break;
}
if (boldnext)
c = c ^ 0x80;
boldnext = 0;
c = c ^ mask;
s[0] = c;
dstring_appendstr (str, s);
} while (1);
if (end)
*end = token;
return save_string (str->str);
}

View file

@ -8,6 +8,7 @@
#include "defspace.h"
#include "function.h"
#include "qfcc.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"

View file

@ -54,6 +54,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "function.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
#include "struct.h"
#include "symtab.h"
#include "type.h"