Ejemplo n.º 1
0
TCOD_lex_t * TCOD_lex_new( const char **_symbols, const char **_keywords, const char *simpleComment,
		const char *commentStart, const char *commentStop, const char *javadocCommentStart, 
		const char *_stringDelim, int _flags)
{
	TCOD_lex_t *lex=(TCOD_lex_t *)TCOD_lex_new_intern();
    lex->flags = _flags;
	lex->last_javadoc_comment = (char *)calloc(sizeof(char),MAX_JAVADOC_COMMENT_SIZE );
    if ( _symbols )
    {
		while ( _symbols[ lex->nb_symbols ] )
		{
			if ( strlen( _symbols[ lex->nb_symbols ] ) >= TCOD_LEX_SYMBOL_SIZE )
			{
				static char msg[255];
				sprintf (msg, "symbol '%s' too long (max size %d)",
				       _symbols[ lex->nb_symbols ], TCOD_LEX_SYMBOL_SIZE );
				TCOD_last_error=TCOD_strdup(msg);
				return NULL;
			}
			strcpy(lex->symbols[ lex->nb_symbols ], _symbols[ lex->nb_symbols ] );
			lex->nb_symbols++;
		}
    }
    if ( _keywords )
    {
		while ( _keywords[ lex->nb_keywords ] )
		{
			if ( strlen( _keywords[ lex->nb_keywords ] ) >= TCOD_LEX_KEYWORD_SIZE )
			{
				static char msg[255];
				sprintf(msg,"keyword '%s' too long (max size %d)",
						   _keywords[ lex->nb_keywords ], TCOD_LEX_KEYWORD_SIZE);
				TCOD_last_error=TCOD_strdup(msg);
				return NULL;
			}
			if ( lex->flags & TCOD_LEX_FLAG_NOCASE )
			{
				char *ptr = (char *)_keywords[ lex->nb_keywords ];
				while ( *ptr )
				{
					*ptr = (char)toupper( *ptr);
					ptr++;
				}
			}
			strcpy(lex->keywords[ lex->nb_keywords ], _keywords[ lex->nb_keywords ] );
			lex->nb_keywords++;
		}
	}
	lex->simpleCmt = simpleComment;
	lex->cmtStart = commentStart;
	lex->cmtStop = commentStop;
	lex->javadocCmtStart = javadocCommentStart;
	lex->stringDelim = _stringDelim;
	lex->lastStringDelim='\0';
	lex->tok = (char *)calloc(sizeof(char),256);
	lex->toklen=256;
	return (TCOD_lex_t *)lex;
}
/* prune repeated "syllables", such as Arnarn */
bool namegen_word_prune_syllables (char *str) {
    char * data = TCOD_strdup(str);
    int len = strlen(data); /* length of the string */
    char check[8];
    int i; /* iteration in for loops */
    /* change to lowercase */
    for (i = 0; i < len; i++) data[i] = (char)(tolower(data[i]));
    /* start pruning */
    /* 2-character direct repetitions */
    for (i = 0; i < len - 4; i++) {
        memset(check,'\0',8);
        strncpy(check,data+i,2);
        strncat(check,data+i,2);
        if (strstr(data,check) != NULL) {
                free(data);
                return true;
            }
    }
    /* 3-character repetitions (anywhere in the word) - prunes everything, even 10-char repetitions */
    for (i = 0; i < len - 6; i++) {
        memset(check,'\0',8);
        strncpy(check,data+i,3);
        if (strstr(data+i+3,check) != NULL) {
            free(data);
            return true;
        }
    }
    free(data);
    return false;
}
/* fill the pointed list with syllable data by extracting tokens */
void namegen_populate_list (char * source, TCOD_list_t list, bool wildcards) {
    size_t len = strlen(source);
    size_t i = 0;
    char * token = malloc(strlen(source)+1); /* tokens will typically be many and very short, but let's be cautious. What if the entire string is a single token?*/
    memset(token,'\0',strlen(source)+1);
    do {
        /* do the tokenising using an iterator immitation :) */
        char * it = source + i;
        /* append a normal character */
        if ((*it >= 'a' && *it <= 'z') ||  (*it >= 'A' && *it <= 'Z') || *it == '\'' || *it == '-')
            strncat(token,it,1);
        /* special character */
        else if (*it == '/') {
            if (wildcards == true) strncat(token,it++,2);
            else strncat(token,++it,1);
            i++;
        }
        /* underscore is converted to space */
        else if (*it == '_') {
            if (wildcards == true) strncat(token,it,1);
            else strcat(token," ");
        }
        /* add wildcards if they are allowed */
        else if (wildcards == true && (*it == '$' || *it == '%' || (*it >= '0' && *it <= '9')))
            strncat(token,it,1);
        /* all other characters are treated as separators and cause adding the current token to the list */
        else if (strlen(token) > 0) {
            TCOD_list_push(list,TCOD_strdup(token));
            memset(token,'\0',strlen(source)+1);
        }
    } while (++i <= len);
    free(token);
}
Ejemplo n.º 4
0
int TCOD_lex_get_symbol(TCOD_lex_t *lex)
{
    int symb = 0;
	static char msg[255];

    while ( symb < lex->nb_symbols )
    {
		if ( ( ( lex->flags & TCOD_LEX_FLAG_NOCASE )
			&& TCOD_strncasecmp( lex->symbols[ symb ], lex->pos, strlen( lex->symbols[ symb ] ) ) == 0 )
			|| ( strncmp( lex->symbols[ symb ], lex->pos, strlen( lex->symbols[ symb ] ) ) == 0 ) )
		{
			strcpy( lex->tok, lex->symbols[ symb ] );
			lex->pos += strlen( lex->symbols[ symb ] );
			lex->token_idx = symb;
			lex->token_type = TCOD_LEX_SYMBOL;
			return TCOD_LEX_SYMBOL;
		}
		symb ++;
    }

    lex->pos++;
	sprintf(msg, "unknown symbol %.10s", lex->pos-1 );
	TCOD_last_error=TCOD_strdup(msg);
    return TCOD_LEX_ERROR;
}
Ejemplo n.º 5
0
bool TCOD_lex_set_data_file(TCOD_lex_t *lex, const char *_filename)
{
    FILE *f;
    char *ptr;
    long size;
    if ( ! _filename ) {
    	TCOD_last_error = (char *)"Lex.setDatafile(NULL) called";
    	return false;
    }
    f = fopen( _filename, "rb" );
    if ( f == NULL )
    {
		static char msg[255];
		sprintf(msg, "Cannot open '%s'", _filename);
		TCOD_last_error=TCOD_strdup(msg);
		return false;
    }
	fseek(f, 0, SEEK_END);
   	size = ftell(f);
   	fclose(f);
    f = fopen( _filename, "r" );
    
    lex->buf = (char*)calloc(sizeof(char),(size + 1));
    lex->filename = TCOD_strdup( _filename );
    if ( lex->buf == NULL || lex->filename == NULL )
    {
		fclose(f);
		if ( lex->buf ) free(lex->buf);
		if ( lex->filename ) {
			free( lex->filename );
		}
		TCOD_last_error=(char *)"Out of memory";
		return false;
    }
	ptr=lex->buf;
	/* can't rely on size to read because of MS/DOS dumb CR/LF handling */
	while ( fgets(ptr, size,f ) )
	{
		ptr += strlen(ptr);
	}
    fclose(f);
	TCOD_lex_set_data_buffer_internal(lex);
	lex->allocBuf=true;
	return true;
}
bool namegen_parser_property(const char *name, TCOD_value_type_t type, TCOD_value_t value) {
    if (strcmp(name,"syllablesStart") == 0)             parser_data->start = TCOD_strdup(value.s);
    else if (strcmp(name,"syllablesMiddle") == 0)       parser_data->middle = TCOD_strdup(value.s);
    else if (strcmp(name,"syllablesEnd") == 0)          parser_data->end = TCOD_strdup(value.s);
    else if (strcmp(name,"syllablesPre") == 0)          parser_data->pre = TCOD_strdup(value.s);
    else if (strcmp(name,"syllablesPost") == 0)         parser_data->post = TCOD_strdup(value.s);
    else if (strcmp(name,"phonemesVocals") == 0)        parser_data->vocals = TCOD_strdup(value.s);
    else if (strcmp(name,"phonemesConsonants") == 0)    parser_data->consonants = TCOD_strdup(value.s);
    else if (strcmp(name,"rules") == 0)                 parser_data->rules = TCOD_strdup(value.s);
    else if (strcmp(name,"illegal") == 0) { /* illegal strings are converted to lowercase */
        char * str ;
        int i;
        parser_data->illegal = TCOD_strdup(value.s);
        str = parser_data->illegal;
        for(i = 0; i < (int)strlen(str); i++) str[i] = (char)(tolower(str[i]));
    }
    else return false;
    return true;
}
/* run the parser */
void namegen_parser_run (const char * filename) {
    char ** it;
    /* prepare the parser --- this will be executed only once */
    namegen_parser_prepare();
    if (parsed_files == NULL) parsed_files = TCOD_list_new();
    if (TCOD_list_size(parsed_files) > 0) {
        for (it = (char **)TCOD_list_begin(parsed_files); it != (char **)TCOD_list_end(parsed_files); it++)
            if (strcmp(*it,filename) == 0) return;
    }
    /* if the file hasn't been parsed yet, add its name to the list so that it's never parsed twice */
    TCOD_list_push(parsed_files,(const void *)TCOD_strdup(filename));
    /* run the parser */
    TCOD_parser_run(namegen_parser,filename,&namegen_listener);
}
bool namegen_parser_end_struct(TCOD_parser_struct_t str, const char *name) {
    /* if there's no syllable set by this name, add it to the list */
    if (namegen_generator_check(name) == false) {
        parser_data->name = TCOD_strdup(name);
        parser_output = namegen_generator_new();
        namegen_populate(parser_output,parser_data);
        parser_output->random = namegen_random;
        if (namegen_generators_list == NULL) namegen_generators_list = TCOD_list_new();
        TCOD_list_push(namegen_generators_list, (const void*)parser_output);
    }
    /* free the allocated memory to prevent a memory leak */
    namegen_syllables_delete(parser_data);
    return true;
}
/* populate all lists of a namegen_t struct */
void namegen_populate (namegen_t * dst, namegen_syllables_t * src) {
    if (dst == NULL || src == NULL) {
        fprintf(stderr,"Couldn't populate the name generator with data.\n");
        exit(1);
    }
    if (src->vocals != NULL)        namegen_populate_list (src->vocals,dst->vocals,false);
    if (src->consonants != NULL)    namegen_populate_list (src->consonants,dst->consonants,false);
    if (src->pre != NULL)           namegen_populate_list (src->pre,dst->syllables_pre,false);
    if (src->start != NULL)         namegen_populate_list (src->start,dst->syllables_start,false);
    if (src->middle != NULL)        namegen_populate_list (src->middle,dst->syllables_middle,false);
    if (src->end != NULL)           namegen_populate_list (src->end,dst->syllables_end,false);
    if (src->post != NULL)          namegen_populate_list (src->post,dst->syllables_post,false);
    if (src->illegal != NULL)       namegen_populate_list (src->illegal,dst->illegal_strings,false);
    if (src->rules != NULL)         namegen_populate_list (src->rules,dst->rules,true);
    dst->name = TCOD_strdup(src->name);
}
Ejemplo n.º 10
0
/* search for occurrences of illegal strings */
bool namegen_word_has_illegal (namegen_t * data, char * str) {
    /* convert word to lowercase */
    char * haystack = TCOD_strdup(str);
    int i;
    for(i = 0; i < (int)strlen(haystack); i++) haystack[i] = (char)(tolower(haystack[i]));
    /* look for illegal strings */
    if (TCOD_list_size(data->illegal_strings) > 0) {
        char ** it;
        for (it = (char**)TCOD_list_begin(data->illegal_strings); it != (char**)TCOD_list_end(data->illegal_strings); it++) {
            if (strstr(haystack,*it) != NULL) {
                free(haystack);
                return true;
            }
        }
    }
    free(haystack);
    return false;
}
Ejemplo n.º 11
0
/* generate a name with one of the rules from the file */
char * TCOD_namegen_generate (char * name, bool allocate) {
    namegen_t * data;
    int rule_number;
    int chance;
    char * rule_rolled;
    int truncation;
    char * rule_parsed ;
    char * ret ;
    if (namegen_generator_check(name)) data = namegen_generator_get(name);
    else {
        fprintf(stderr,"The name \"%s\" has not been found.\n",name);
        namegen_get_sets_on_error();
        return NULL;
    }
    /* check if the rules list is present */
    if (TCOD_list_size(data->rules) == 0) {
        fprintf(stderr,"The rules list is empty!\n");
        exit(1);
    }
    /* choose the rule */
    do {
        rule_number = TCOD_random_get_int(data->random,0,TCOD_list_size(data->rules)-1);
        rule_rolled = (char*)TCOD_list_get(data->rules,rule_number);
        chance = 100;
        truncation = 0;
        if (rule_rolled[0] == '%') {
            truncation = 1;
            chance = 0;
            while (rule_rolled[truncation] >= '0' && rule_rolled[truncation] <= '9') {
                chance *= 10;
                chance += (int)(rule_rolled[truncation]) - (int)('0');
                truncation++;
            }
        }
    } while (TCOD_random_get_int(data->random,0,100) > chance);
    /* OK, we've got ourselves a new rule! */
    rule_parsed = TCOD_strdup(rule_rolled+truncation);
    ret = TCOD_namegen_generate_custom(name,rule_parsed,allocate);
    free(rule_parsed);
    return ret;
}
Ejemplo n.º 12
0
/* set cursor and prompt */
void TCOD_text_set_properties (TCOD_text_t txt, int cursor_char, int blink_interval, const char * prompt, int tab_size) {
    text_t * data = (text_t*)txt;
    TCOD_IFNOT(data && data->con ) return;
    data->interval = blink_interval;
    data->halfinterval = (blink_interval > 0 ? blink_interval / 2 : 0);
    data->ascii_cursor = cursor_char;
    if (data->prompt) free(data->prompt);
    data->prompt = prompt ? TCOD_strdup(prompt) : NULL;
    data->textx = data->texty = 0;
	data->tab_size=tab_size;
    if ( prompt ) {
		const char *ptr=prompt;
		while (*ptr) {
			data->textx++;
			if ( *ptr == '\n' || data->textx == data->w) {
				data->textx=0;data->texty++;
			}
			ptr++;
		}
	}
}
Ejemplo n.º 13
0
TCOD_list_t TCOD_sys_get_directory_content(const char *path, const char *pattern) {
    TCOD_list_t list=TCOD_list_new();
#ifdef TCOD_WINDOWS
    WIN32_FIND_DATA FileData;
    HANDLE          hList;
	char dname[ 512 ];
	sprintf(dname, "%s\\*",path);
    hList = FindFirstFile(dname, &FileData);
    if (hList == INVALID_HANDLE_VALUE)
    {
        return list;
    }
	do
	{
		if ( ! (strcmp(FileData.cFileName,".") == 0 || strcmp(FileData.cFileName,"..") == 0 ) )
		{
			if ( filename_match(FileData.cFileName,pattern) )
				TCOD_list_push(list,TCOD_strdup(FileData.cFileName));
		}

	} while ( FindNextFile(hList, &FileData) );
    FindClose(hList);
#else
    DIR *dir = opendir(path);
    struct dirent *dirent = NULL;
    if ( ! dir ) return list;
    while ( ( dirent = readdir(dir) ) )
    {
		if ( ! (strcmp(dirent->d_name,".") == 0 || strcmp(dirent->d_name,"..") == 0 ) )
		{
			if ( filename_match(dirent->d_name,pattern) )
				TCOD_list_push(list,strdup(dirent->d_name));
		}
	}
	closedir(dir);
#endif
	return list;
}
Ejemplo n.º 14
0
void Slider::setFormat(const char *fmt) {
	if ( this->fmt ) free(this->fmt);
	if ( fmt ) this->fmt = TCOD_strdup(fmt);
	else fmt=NULL;
	valueToText();
}
Ejemplo n.º 15
0
void Widget::setTip(const char *tip) {
	if ( this->tip ) free(this->tip);
	this->tip = TCOD_strdup(tip);
}
Ejemplo n.º 16
0
void Button::setLabel(const char *newLabel) {
	if ( label ) free(label);
	label=TCOD_strdup(newLabel);
}