Esempio n. 1
0
bool file_open(struct File* f, const char *filename, const char *directory, const char *mode) {
    char buf[512];
    rb->snprintf(buf, 512, "%s/%s", directory, filename);
    char *p = buf + rb->strlen(directory) + 1;
    string_lower(p);

    int flags = 0;
    for(int i = 0; mode[i]; ++i)
    {
        switch(mode[i])
        {
        case 'w':
            flags |= O_WRONLY | O_CREAT | O_TRUNC;
            break;
        case 'r':
            flags |= O_RDONLY;
            break;
        default:
            break;
        }
    }
    f->fd = -1;
    debug(DBG_FILE, "trying %s first", buf);
    f->fd = rb->open(buf, flags, 0666);
    if (f->fd < 0) { // let's try uppercase
        string_upper(p);
        debug(DBG_FILE, "now trying %s uppercase", buf);
        f->fd = rb->open(buf, flags, 0666);
    }
    if(f->fd > 0)
        return true;
    else
        return false;
}
Esempio n. 2
0
bool geodisambig_add_geoname_features(cstring_array *features, geoname_t *geoname) {
    if (geoname == NULL) return false;

    char *name = char_array_get_string(geoname->name);
    char *lang = char_array_get_string(geoname->iso_language);
    bool add_language = strlen(lang) != 0 && strcmp(lang, "abbr") != 0;

    char country_code_normalized[3];
    char *country_code = char_array_get_string(geoname->country_code);
    size_t country_code_len = strlen(country_code);
    bool add_country = country_code_len == 2;

    if (add_country) {
        strcpy(country_code_normalized, country_code);
        string_lower(country_code_normalized);
    }

    // Don't add neighbors at index time, only for queries
    bool add_neighbors = false;

    return (geodisambig_add_name_feature(features, name)
            && (!add_country || geodisambig_add_country_code_feature(features, name, country_code_normalized))
            && geodisambig_add_country_id_feature(features, name, geoname->country_geonames_id)
            && (geoname->admin1_geonames_id == 0 || geodisambig_add_admin1_feature(features, name, geoname->admin1_geonames_id))
            && (geoname->admin2_geonames_id == 0 || geodisambig_add_admin2_feature(features, name, geoname->admin2_geonames_id))
            && (geodisambig_add_boundary_type_feature(features, name, geoname->type))
            && (!add_language || geodisambig_add_language_feature(features, name, lang))
            && (geodisambig_add_geo_features(features, name, geoname->latitude, geoname->longitude, add_neighbors))
            );
}
bool File::open(const char *filename, const char *directory, const char *mode) {	
	_impl->close();
	char buf[512];
	sprintf(buf, "%s/%s", directory, filename);
	char *p = buf + strlen(directory) + 1;
	string_lower(p);
	bool opened = _impl->open(buf, mode);
	if (!opened) { // let's try uppercase
		string_upper(p);
		opened = _impl->open(buf, mode);
	}
	return opened;
}
Esempio n. 4
0
mulk_type_return_t extract_mime_type(const char *mime_type, char **type, char **subtype)
{
	char *token, *subtoken, *token_mime, *buf;

	if (!mime_type)
		return MULK_RET_ERR;

	if (type)
		*type = NULL;
	if (subtype)
		*subtype = NULL;

	buf = string_new(mime_type);
	if ((token = strtok(buf, TYPE_DELIM)) == NULL) {
		string_free(&buf);
		return MULK_RET_ERR;
	}

	token_mime = string_new(token);
	string_lower(token_mime);

	string_free(&buf);

	if ((subtoken = strtok(token_mime, SUBTYPE_DELIM)) == NULL) {
		string_free(&token_mime);
		return MULK_RET_ERR;
	}

	if (type)
		*type = string_new(subtoken);

	if ((subtoken = strtok(NULL, SUBTYPE_DELIM)) == NULL) {
		string_free(&token_mime);
		string_free(type);
		return MULK_RET_ERR;
	}

	if (subtype)
		*subtype = string_new(subtoken);

	string_free(&token_mime);

	return MULK_RET_OK;
}
Esempio n. 5
0
void set_buffer_mime_type(CURL *id, const char *mimetype)
{
	int i;
	char *ptr;
	buffer_t *buffer;

	if ((i = get_buffer(id)) < 0)
		return;

	buffer = buffer_array + i;

	string_free(&buffer->url->mimetype);
	
	if (mimetype) {
		buffer->url->mimetype = string_new(mimetype);
		if ((ptr = strchr(buffer->url->mimetype, ';')) != NULL)
			*ptr = 0;
		string_lower(buffer->url->mimetype);
	}
}
Esempio n. 6
0
/*
* BEE::append_input() - Append the given keyboard event's key to the given string and return the added character
* @output: the string to append to
* @k: the keyboard event to append
*/
char BEE::append_input(std::string* output, SDL_KeyboardEvent* k) {
	std::string s = "";
	switch (k->keysym.sym) {
		// Handle capitalization for all alphabetical characters based on shift and caps lock
		case SDLK_a:
		case SDLK_b:
		case SDLK_c:
		case SDLK_d:
		case SDLK_e:
		case SDLK_f:
		case SDLK_g:
		case SDLK_h:
		case SDLK_i:
		case SDLK_j:
		case SDLK_k:
		case SDLK_l:
		case SDLK_m:
		case SDLK_n:
		case SDLK_o:
		case SDLK_p:
		case SDLK_q:
		case SDLK_r:
		case SDLK_s:
		case SDLK_t:
		case SDLK_u:
		case SDLK_v:
		case SDLK_w:
		case SDLK_x:
		case SDLK_y:
		case SDLK_z: {
			s = SDL_GetKeyName(k->keysym.sym);

			bool should_capitalize = false;
			if (k->keysym.mod & KMOD_SHIFT) {
				should_capitalize = true;
			}
			if (k->keysym.mod & KMOD_CAPS) {
				should_capitalize = !should_capitalize;
			}
			if (!should_capitalize) {
				s = string_lower(s);
			}

			break;
		}

		// Handle backspace
		case SDLK_BACKSPACE: {
			if (!output->empty()) {
				output->pop_back();
			}
			break;
		}

		// Handle spacing keys
		case SDLK_SPACE: {
			s = " ";
			break;
		}
		case SDLK_TAB: {
			s = "	";
			break;
		}

		// Handle numpad keys which aren't effected by numlock
		case SDLK_KP_DIVIDE: {
			s = "/";
			break;
		}
		case SDLK_KP_MULTIPLY: {
			s = "*";
			break;
		}
		case SDLK_KP_MINUS: {
			s = "-";
			break;
		}
		case SDLK_KP_PLUS: {
			s = "+";
			break;
		}

		// Handle shifted numbers
		case SDLK_0: {
			s = "0";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = ")";
			}
			break;
		}
		case SDLK_1: {
			s = "1";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "!";
			}
			break;
		}
		case SDLK_2: {
			s = "2";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "@";
			}
			break;
		}
		case SDLK_3: {
			s = "3";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "#";
			}
			break;
		}
		case SDLK_4: {
			s = "4";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "$";
			}
			break;
		}
		case SDLK_5: {
			s = "5";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "%";
			}
			break;
		}
		case SDLK_6: {
			s = "6";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "^";
			}
			break;
		}
		case SDLK_7: {
			s = "7";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "&";
			}
			break;
		}
		case SDLK_8: {
			s = "8";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "*";
			}
			break;
		}
		case SDLK_9: {
			s = "9";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "(";
			}
			break;
		}

		// Handle shifted symbols
		case SDLK_BACKSLASH: {
			s = "\\";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "|";
			}
			break;
		}
		case SDLK_BACKQUOTE: {
			s = "`";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "~";
			}
			break;
		}
		case SDLK_QUOTE: {
			s = "'";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "\"";
			}
			break;
		}
		case SDLK_COMMA: {
			s = ",";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "<";
			}
			break;
		}
		case SDLK_EQUALS: {
			s = "=";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "+";
			}
			break;
		}
		case SDLK_LEFTBRACKET: {
			s = "[";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "{";
			}
			break;
		}
		case SDLK_MINUS: {
			s = "-";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "_";
			}
			break;
		}
		case SDLK_PERIOD: {
			s = ".";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = ">";
			}
			break;
		}
		case SDLK_RIGHTBRACKET: {
			s = "]";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "}";
			}
			break;
		}
		case SDLK_SEMICOLON: {
			s = ";";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = ":";
			}
			break;
		}
		case SDLK_SLASH: {
			s = "/";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "?";
			}
			break;
		}
	}

	if ((s.empty())&&(k->keysym.mod & KMOD_NUM)) {
		switch(k->keysym.sym) {
			// Handle the numpad
			case SDLK_KP_0:
			case SDLK_KP_00:
			case SDLK_KP_000: {
				s = "0";
				break;
			}
			case SDLK_KP_1: {
				s = "1";
				break;
			}
			case SDLK_KP_2: {
				s = "2";
				break;
			}
			case SDLK_KP_3: {
				s = "3";
				break;
			}
			case SDLK_KP_4: {
				s = "4";
				break;
			}
			case SDLK_KP_5: {
				s = "5";
				break;
			}
			case SDLK_KP_6: {
				s = "6";
				break;
			}
			case SDLK_KP_7: {
				s = "7";
				break;
			}
			case SDLK_KP_8: {
				s = "8";
				break;
			}
			case SDLK_KP_9: {
				s = "9";
				break;
			}
			case SDLK_KP_PERIOD: {
				s = ".";
				break;
			}
		}
	}

	output->append(s); // Append the character to the given string
	return s.c_str()[0]; // Return the character
}
Esempio n. 7
0
bool client_submit(YAAMP_CLIENT *client, json_value *json_params)
{
	// submit(worker_name, jobid, extranonce2, ntime, nonce):
	if(json_params->u.array.length<5)
	{
		debuglog("%s - %s bad message\n", client->username, client->sock->ip);
		client->submit_bad++;
		return false;
	}

//	char name[1024];
	char extranonce2[32];
	char ntime[32];
	char nonce[32];

	memset(extranonce2, 0, 32);
	memset(ntime, 0, 32);
	memset(nonce, 0, 32);

	int jobid = htoi(json_params->u.array.values[1]->u.string.ptr);
	strncpy(extranonce2, json_params->u.array.values[2]->u.string.ptr, 31);
	strncpy(ntime, json_params->u.array.values[3]->u.string.ptr, 31);
	strncpy(nonce, json_params->u.array.values[4]->u.string.ptr, 31);

//	debuglog("submit %s %d, %s, %s, %s\n", client->sock->ip, jobid, extranonce2, ntime, nonce);

	string_lower(extranonce2);
	string_lower(ntime);
	string_lower(nonce);

	YAAMP_JOB *job = (YAAMP_JOB *)object_find(&g_list_job, jobid, true);
	if(!job)
	{
		client_submit_error(client, NULL, 21, "Invalid job id", extranonce2, ntime, nonce);
		return true;
	}

	if(job->deleted)
	{
		client_send_result(client, "true");
		object_unlock(job);

		return true;
	}

	YAAMP_JOB_TEMPLATE *templ = job->templ;
//	dump_submit_debug(client, job, extranonce2, ntime, nonce);

	if(strlen(nonce) != YAAMP_NONCE_SIZE*2)
	{
		client_submit_error(client, job, 20, "Invalid nonce size", extranonce2, ntime, nonce);
		return true;
	}

//	if(strcmp(ntime, templ->ntime))
//	{
//		client_submit_error(client, job, 23, "Invalid time rolling", extranonce2, ntime, nonce);
//		return true;
//	}

	YAAMP_SHARE *share = share_find(job->id, extranonce2, ntime, nonce, client->extranonce1);
	if(share)
	{
		client_submit_error(client, job, 22, "Duplicate share", extranonce2, ntime, nonce);
		return true;
	}

	if(strlen(extranonce2) != client->extranonce2size*2)
	{
		client_submit_error(client, job, 24, "Invalid extranonce2 size", extranonce2, ntime, nonce);
		return true;
	}

	///////////////////////////////////////////////////////////////////////////////////////////

	YAAMP_JOB_VALUES submitvalues;
	memset(&submitvalues, 0, sizeof(submitvalues));

	build_submit_values(&submitvalues, templ, client->extranonce1, extranonce2, ntime, nonce);
	if(submitvalues.hash_bin[30] || submitvalues.hash_bin[31])
	{
		client_submit_error(client, job, 25, "Invalid share", extranonce2, ntime, nonce);
		return true;
	}

	uint64_t hash_int = get_hash_difficulty(submitvalues.hash_bin);
	uint64_t user_target = diff_to_target(client->difficulty_actual);
	uint64_t coin_target = decode_compact(templ->nbits);

//	debuglog("%016llx actual\n", hash_int);
//	debuglog("%016llx target\n", user_target);
//	debuglog("%016llx coin\n", coin_target);

	if(hash_int > user_target && hash_int > coin_target)
	{
		client_submit_error(client, job, 26, "Low difficulty share", extranonce2, ntime, nonce);
		return true;
	}

	if(job->coind)
		client_do_submit(client, job, &submitvalues, extranonce2, ntime, nonce);
	else
		remote_submit(client, job, &submitvalues, extranonce2, ntime, nonce);

	client_send_result(client, "true");
	client_record_difficulty(client);
	client->submit_bad = 0;

	share_add(client, job, true, extranonce2, ntime, nonce, 0);
	object_unlock(job);

	return true;
}
Esempio n. 8
0
/*
 * Recursive file perusal.
 *
 * Return FALSE on "?", otherwise TRUE.
 *
 * This function could be made much more efficient with the use of "seek"
 * functionality, especially when moving backwards through a file, or
 * forwards through a file by less than a page at a time.  XXX XXX XXX
 */
bool show_file(const char *name, const char *what, int line, int mode)
{
	int i, k, n;

	struct keypress ch;

	/* Number of "real" lines passed by */
	int next = 0;

	/* Number of "real" lines in the file */
	int size;

	/* Backup value for "line" */
	int back = 0;

	/* This screen has sub-screens */
	bool menu = FALSE;

	/* Case sensitive search */
	bool case_sensitive = FALSE;

	/* Current help file */
	ang_file *fff = NULL;

	/* Find this string (if any) */
	char *find = NULL;

	/* Jump to this tag */
	const char *tag = NULL;

	/* Hold a string to find */
	char finder[80] = "";

	/* Hold a string to show */
	char shower[80] = "";

	/* Filename */
	char filename[1024];

	/* Describe this thing */
	char caption[128] = "";

	/* Path buffer */
	char path[1024];

	/* General buffer */
	char buf[1024];

	/* Lower case version of the buffer, for searching */
	char lc_buf[1024];

	/* Sub-menu information */
	char hook[26][32];

	int wid, hgt;
	
	/* TRUE if we are inside a RST block that should be skipped */
	bool skip_lines = FALSE;



	/* Wipe the hooks */
	for (i = 0; i < 26; i++) hook[i][0] = '\0';

	/* Get size */
	Term_get_size(&wid, &hgt);

	/* Copy the filename */
	my_strcpy(filename, name, sizeof(filename));

	n = strlen(filename);

	/* Extract the tag from the filename */
	for (i = 0; i < n; i++)
	{
		if (filename[i] == '#')
		{
			filename[i] = '\0';
			tag = filename + i + 1;
			break;
		}
	}

	/* Redirect the name */
	name = filename;

	/* Hack XXX XXX XXX */
	if (what)
	{
		my_strcpy(caption, what, sizeof(caption));

		my_strcpy(path, name, sizeof(path));
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Look in "help" */
	if (!fff)
	{
		strnfmt(caption, sizeof(caption), "Help file '%s'", name);

		path_build(path, sizeof(path), ANGBAND_DIR_HELP, name);
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Look in "info" */
	if (!fff)
	{
		strnfmt(caption, sizeof(caption), "Info file '%s'", name);

		path_build(path, sizeof(path), ANGBAND_DIR_INFO, name);
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Oops */
	if (!fff)
	{
		/* Message */
		msg("Cannot open '%s'.", name);
		event_signal(EVENT_MESSAGE_FLUSH);

		/* Oops */
		return (TRUE);
	}


	/* Pre-Parse the file */
	while (TRUE)
	{
		/* Read a line or stop */
		if (!file_getl(fff, buf, sizeof(buf))) break;

		/* Skip lines if we are inside a RST directive*/
		if(skip_lines){
			if(contains_only_spaces(buf))
				skip_lines=FALSE;
			continue;
		}

		/* Parse a very small subset of RST */
		/* TODO: should be more flexible */
		if (prefix(buf, ".. "))
		{
			/* parse ".. menu:: [x] filename.txt" (with exact spacing)*/
			if(prefix(buf+strlen(".. "), "menu:: [") && 
                           buf[strlen(".. menu:: [x")]==']')
			{
				/* This is a menu file */
				menu = TRUE;

				/* Extract the menu item */
				k = A2I(buf[strlen(".. menu:: [")]);

				/* Store the menu item (if valid) */
				if ((k >= 0) && (k < 26))
					my_strcpy(hook[k], buf + strlen(".. menu:: [x] "), sizeof(hook[0]));
			}
			/* parse ".. _some_hyperlink_target:" */
			else if (buf[strlen(".. ")] == '_')
			{
				if (tag)
				{
					/* Remove the closing '>' of the tag */
					buf[strlen(buf) - 1] = '\0';

					/* Compare with the requested tag */
					if (streq(buf + strlen(".. _"), tag))
					{
						/* Remember the tagged line */
						line = next;
					}
				}
			}

			/* Skip this and enter skip mode*/
			skip_lines = TRUE;
			continue;
		}

		/* Count the "real" lines */
		next++;
	}

	/* Save the number of "real" lines */
	size = next;


	/* Display the file */
	while (TRUE)
	{
		/* Clear screen */
		Term_clear();


		/* Restrict the visible range */
		if (line > (size - (hgt - 4))) line = size - (hgt - 4);
		if (line < 0) line = 0;

		skip_lines = FALSE;
		/* Re-open the file if needed */
		if (next > line)
		{
			/* Close it */
			file_close(fff);

			/* Hack -- Re-Open the file */
			fff = file_open(path, MODE_READ, FTYPE_TEXT);
			if (!fff) return (TRUE);

			/* File has been restarted */
			next = 0;
		}


		/* Goto the selected line */
		while (next < line)
		{
			/* Get a line */
			if (!file_getl(fff, buf, sizeof(buf))) break;

			/* Skip lines if we are inside a RST directive*/
			if(skip_lines){
				if(contains_only_spaces(buf))
					skip_lines=FALSE;
				continue;
			}

			/* Skip RST directives */
			if (prefix(buf, ".. "))
			{
				skip_lines=TRUE;
				continue;
			}

			/* Count the lines */
			next++;
		}


		/* Dump the next lines of the file */
		for (i = 0; i < hgt - 4; )
		{
			/* Hack -- track the "first" line */
			if (!i) line = next;

			/* Get a line of the file or stop */
			if (!file_getl(fff, buf, sizeof(buf))) break;

			/* Skip lines if we are inside a RST directive*/
			if(skip_lines){
				if(contains_only_spaces(buf))
					skip_lines=FALSE;
				continue;
			}

			/* Skip RST directives */
			if (prefix(buf, ".. "))
			{
				skip_lines=TRUE;
				continue;
			}

			/* skip | characters */
			strskip(buf,'|');

			/* escape backslashes */
			strescape(buf,'\\');

			/* Count the "real" lines */
			next++;

			/* Make a copy of the current line for searching */
			my_strcpy(lc_buf, buf, sizeof(lc_buf));

			/* Make the line lower case */
			if (!case_sensitive) string_lower(lc_buf);

			/* Hack -- keep searching */
			if (find && !i && !strstr(lc_buf, find)) continue;

			/* Hack -- stop searching */
			find = NULL;

			/* Dump the line */
			Term_putstr(0, i+2, -1, COLOUR_WHITE, buf);

			/* Highlight "shower" */
			if (shower[0])
			{
				const char *str = lc_buf;

				/* Display matches */
				while ((str = strstr(str, shower)) != NULL)
				{
					int len = strlen(shower);

					/* Display the match */
					Term_putstr(str-lc_buf, i+2, len, COLOUR_YELLOW,
								&buf[str-lc_buf]);

					/* Advance */
					str += len;
				}
			}

			/* Count the printed lines */
			i++;
		}

		/* Hack -- failed search */
		if (find)
		{
			bell("Search string not found!");
			line = back;
			find = NULL;
			continue;
		}


		/* Show a general "title" */
		prt(format("[%s, %s, Line %d-%d/%d]", buildid,
		           caption, line, line + hgt - 4, size), 0, 0);


		/* Prompt -- menu screen */
		if (menu)
		{
			/* Wait for it */
			prt("[Press a Letter, or ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- small files */
		else if (size <= hgt - 4)
		{
			/* Wait for it */
			prt("[Press ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- large files */
		else
		{
			/* Wait for it */
			prt("[Press Space to advance, or ESC to exit.]", hgt - 1, 0);
		}

		/* Get a keypress */
		ch = inkey();

		/* Exit the help */
		if (ch.code == '?') break;

		/* Toggle case sensitive on/off */
		if (ch.code == '!')
		{
			case_sensitive = !case_sensitive;
		}

		/* Try showing */
		if (ch.code == '&')
		{
			/* Get "shower" */
			prt("Show: ", hgt - 1, 0);
			(void)askfor_aux(shower, sizeof(shower), NULL);

			/* Make the "shower" lowercase */
			if (!case_sensitive) string_lower(shower);
		}

		/* Try finding */
		if (ch.code == '/')
		{
			/* Get "finder" */
			prt("Find: ", hgt - 1, 0);
			if (askfor_aux(finder, sizeof(finder), NULL))
			{
				/* Find it */
				find = finder;
				back = line;
				line = line + 1;

				/* Make the "finder" lowercase */
				if (!case_sensitive) string_lower(finder);

				/* Show it */
				my_strcpy(shower, finder, sizeof(shower));
			}
		}

		/* Go to a specific line */
		if (ch.code == '#')
		{
			char tmp[80] = "0";

			prt("Goto Line: ", hgt - 1, 0);
			if (askfor_aux(tmp, sizeof(tmp), NULL))
				line = atoi(tmp);
		}

		/* Go to a specific file */
		if (ch.code == '%')
		{
			char ftmp[80] = "help.hlp";

			prt("Goto File: ", hgt - 1, 0);
			if (askfor_aux(ftmp, sizeof(ftmp), NULL))
			{
				if (!show_file(ftmp, NULL, 0, mode))
					ch.code = ESCAPE;
			}
		}

		switch (ch.code) {
			/* up a line */
			case ARROW_UP:
			case '8': line--; break;

			/* up a page */
			case KC_PGUP:
			case '9':
			case '-': line -= (hgt - 4); break;

			/* home */
			case KC_HOME:
			case '7': line = 0; break;

			/* down a line */
			case ARROW_DOWN:
			case '2':
			case KC_ENTER: line++; break;

			/* down a page */
			case KC_PGDOWN:
			case '3':
			case ' ': line += hgt - 4; break;

			/* end */
			case KC_END:
			case '1': line = size; break;
		}

		/* Recurse on letters */
		if (menu && isalpha((unsigned char)ch.code))
		{
			/* Extract the requested menu item */
			k = A2I(ch.code);

			/* Verify the menu item */
			if ((k >= 0) && (k <= 25) && hook[k][0])
			{
				/* Recurse on that file */
				if (!show_file(hook[k], NULL, 0, mode)) ch.code = ESCAPE;
			}
		}

		/* Exit on escape */
		if (ch.code == ESCAPE) break;
	}

	/* Close the file */
	file_close(fff);

	/* Done */
	return (ch.code != '?');
}
Esempio n. 9
0
/*
 * Recursive file perusal.
 *
 * Return FALSE on "ESCAPE", otherwise TRUE.
 *
 * Process various special text in the input file, including the "menu"
 * structures used by the "help file" system.
 *
 * This function could be made much more efficient with the use of "seek"
 * functionality, especially when moving backwards through a file, or
 * forwards through a file by less than a page at a time.  XXX XXX XXX
 *
 * Consider using a temporary file, in which special lines do not appear,
 * and which could be pre-padded to 80 characters per line, to allow the
 * use of perfect seeking.  XXX XXX XXX
 *
 * Allow the user to "save" the current file.  XXX XXX XXX
 */
bool show_file(cptr name, cptr what, int line, int mode)
{
	int i, k, n;

	char ch;

	/* Number of "real" lines passed by */
	int next = 0;

	/* Number of "real" lines in the file */
	int size;

	/* Backup value for "line" */
	int back = 0;

	/* This screen has sub-screens */
	bool menu = FALSE;

	/* Case sensitive search */
	bool case_sensitive = FALSE;

	/* Current help file */
	FILE *fff = NULL;

	/* Find this string (if any) */
	char *find = NULL;

	/* Jump to this tag */
	cptr tag = NULL;

	/* Hold a string to find */
	char finder[80];

	/* Hold a string to show */
	char shower[80];

	/* Filename */
	char filename[1024];

	/* Describe this thing */
	char caption[128];

	/* Path buffer */
	char path[1024];

	/* General buffer */
	char buf[1024];

	/* Lower case version of the buffer, for searching */
	char lc_buf[1024];

	/* Sub-menu information */
	char hook[10][32];

	int wid, hgt;


	/* Wipe finder */
	strcpy(finder, "");

	/* Wipe shower */
	strcpy(shower, "");

	/* Wipe caption */
	strcpy(caption, "");

	/* Wipe the hooks */
	for (i = 0; i < 10; i++) hook[i][0] = '\0';

	/* Get size */
	Term_get_size(&wid, &hgt);

	/* Copy the filename */
	my_strcpy(filename, name, sizeof(filename));

	n = strlen(filename);

	/* Extract the tag from the filename */
	for (i = 0; i < n; i++)
	{
		if (filename[i] == '#')
		{
			filename[i] = '\0';
			tag = filename + i + 1;
			break;
		}
	}

	/* Redirect the name */
	name = filename;


	/* Hack XXX XXX XXX */
	if (what)
	{
		/* Caption */
		my_strcpy(caption, what, sizeof(caption));

		/* Get the filename */
		my_strcpy(path, name, sizeof(path));

		/* Open */
		fff = my_fopen(path, "r");
	}

	/* Look in "help" */
	if (!fff)
	{
		/* Caption */
		strnfmt(caption, sizeof(caption), "Help file '%s'", name);

		/* Build the filename */
		path_build(path, sizeof(path), ANGBAND_DIR_HELP, name);

		/* Open the file */
		fff = my_fopen(path, "r");
	}

	/* Look in "info" */
	if (!fff)
	{
		/* Caption */
		strnfmt(caption, sizeof(caption), "Info file '%s'", name);

		/* Build the filename */
		path_build(path, sizeof(path), ANGBAND_DIR_INFO, name);

		/* Open the file */
		fff = my_fopen(path, "r");
	}

	/* Oops */
	if (!fff)
	{
		/* Message */
		msg_format("Cannot open '%s'.", name);
		message_flush();

		/* Oops */
		return (TRUE);
	}


	/* Pre-Parse the file */
	while (TRUE)
	{
		/* Read a line or stop */
		if (my_fgets(fff, buf, sizeof(buf))) break;

		/* XXX Parse "menu" items */
		if (prefix(buf, "***** "))
		{
			char b1 = '[', b2 = ']';

			/* Notice "menu" requests */
			if ((buf[6] == b1) && isdigit((unsigned char)buf[7]) &&
			    (buf[8] == b2) && (buf[9] == ' '))
			{
				/* This is a menu file */
				menu = TRUE;

				/* Extract the menu item */
				k = D2I(buf[7]);

				/* Extract the menu item */
				my_strcpy(hook[k], buf + 10, sizeof(hook[0]));
			}
			/* Notice "tag" requests */
			else if (buf[6] == '<')
			{
				if (tag)
				{
					/* Remove the closing '>' of the tag */
					buf[strlen(buf) - 1] = '\0';

					/* Compare with the requested tag */
					if (streq(buf + 7, tag))
					{
						/* Remember the tagged line */
						line = next;
					}
				}
			}

			/* Skip this */
			continue;
		}

		/* Count the "real" lines */
		next++;
	}

	/* Save the number of "real" lines */
	size = next;



	/* Display the file */
	while (TRUE)
	{
		/* Clear screen */
		Term_clear();


		/* Restart when necessary */
		if (line >= size) line = 0;


		/* Re-open the file if needed */
		if (next > line)
		{
			/* Close it */
			my_fclose(fff);

			/* Hack -- Re-Open the file */
			fff = my_fopen(path, "r");

			/* Oops */
			if (!fff) return (TRUE);

			/* File has been restarted */
			next = 0;
		}


		/* Goto the selected line */
		while (next < line)
		{
			/* Get a line */
			if (my_fgets(fff, buf, sizeof(buf))) break;

			/* Skip tags/links */
			if (prefix(buf, "***** ")) continue;

			/* Count the lines */
			next++;
		}


		/* Dump the next lines of the file */
		for (i = 0; i < hgt - 4; )
		{
			/* Hack -- track the "first" line */
			if (!i) line = next;

			/* Get a line of the file or stop */
			if (my_fgets(fff, buf, sizeof(buf))) break;

			/* Hack -- skip "special" lines */
			if (prefix(buf, "***** ")) continue;

			/* Count the "real" lines */
			next++;

			/* Make a copy of the current line for searching */
			my_strcpy(lc_buf, buf, sizeof(lc_buf));

			/* Make the line lower case */
			if (!case_sensitive) string_lower(lc_buf);

			/* Hack -- keep searching */
			if (find && !i && !strstr(lc_buf, find)) continue;

			/* Hack -- stop searching */
			find = NULL;

			/* Dump the line */
			Term_putstr(0, i+2, -1, TERM_WHITE, buf);

			/* Hilite "shower" */
			if (shower[0])
			{
				cptr str = lc_buf;

				/* Display matches */
				while ((str = strstr(str, shower)) != NULL)
				{
					int len = strlen(shower);

					/* Display the match */
					Term_putstr(str-lc_buf, i+2, len, TERM_YELLOW, &buf[str-lc_buf]);

					/* Advance */
					str += len;
				}
			}

			/* Count the printed lines */
			i++;
		}

		/* Hack -- failed search */
		if (find)
		{
			bell("Search string not found!");
			line = back;
			find = NULL;
			continue;
		}


		/* Show a general "title" */
		prt(format("[%s %s, %s, Line %d/%d]", VERSION_NAME,
		           VERSION_STRING, caption, line, size), 0, 0);


		/* Prompt -- menu screen */
		if (menu)
		{
			/* Wait for it */
			prt("[Press a Number, or ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- small files */
		else if (size <= hgt - 4)
		{
			/* Wait for it */
			prt("[Press ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- large files */
		else
		{
			/* Wait for it */
			prt("[Press Space to advance, or ESC to exit.]", hgt - 1, 0);
		}

		/* Get a keypress */
		ch = inkey();

		/* Return to last screen */
		if (ch == '?') break;

		/* Toggle case sensitive on/off */
		if (ch == '!')
		{
			case_sensitive = !case_sensitive;
		}

		/* Try showing */
		if (ch == '&')
		{
			/* Get "shower" */
			prt("Show: ", hgt - 1, 0);
			(void)askfor_aux(shower, sizeof(shower));

			/* Make the "shower" lowercase */
			if (!case_sensitive) string_lower(shower);
		}

		/* Try finding */
		if (ch == '/')
		{
			/* Get "finder" */
			prt("Find: ", hgt - 1, 0);
			if (askfor_aux(finder, sizeof(finder)))
			{
				/* Find it */
				find = finder;
				back = line;
				line = line + 1;

				/* Make the "finder" lowercase */
				if (!case_sensitive) string_lower(finder);

				/* Show it */
				my_strcpy(shower, finder, sizeof(shower));
			}
		}

		/* Go to a specific line */
		if (ch == '#')
		{
			char tmp[80];
			prt("Goto Line: ", hgt - 1, 0);
			strcpy(tmp, "0");
			if (askfor_aux(tmp, sizeof(tmp)))
			{
				line = atoi(tmp);
			}
		}

		/* Go to a specific file */
		if (ch == '%')
		{
			char ftmp[80];
			prt("Goto File: ", hgt - 1, 0);
			strcpy(ftmp, "help.hlp");
			if (askfor_aux(ftmp, sizeof(ftmp)))
			{
				if (!show_file(ftmp, NULL, 0, mode)) ch = ESCAPE;
			}
		}

		/* Back up one line */
		if (ch == '=')
		{
			line = line - 1;
			if (line < 0) line = 0;
		}

		/* Back up one half page */
		if (ch == '_')
		{
			line = line - ((hgt - 4) / 2);
			if (line < 0) line = 0;
		}

		/* Back up one full page */
		if (ch == '-')
		{
			line = line - (hgt - 4);
			if (line < 0) line = 0;
		}

		/* Advance one line */
		if ((ch == '\n') || (ch == '\r'))
		{
			line = line + 1;
		}

		/* Advance one half page */
		if (ch == '+')
		{
			line = line + ((hgt - 4) / 2);
			if (line < 0) line = 0;
		}

		/* Advance one full page */
		if (ch == ' ')
		{
			line = line + (hgt - 4);
		}

		/* Recurse on numbers */
		if (menu && isdigit((unsigned char)ch) && hook[D2I(ch)][0])
		{
			/* Recurse on that file */
			if (!show_file(hook[D2I(ch)], NULL, 0, mode)) ch = ESCAPE;
		}

		/* Exit on escape */
		if (ch == ESCAPE) break;
	}

	/* Close the file */
	my_fclose(fff);

	/* Done */
	return (ch != ESCAPE);
}
Esempio n. 10
0
char BEE::append_input(std::string& output, SDL_KeyboardEvent* k) {
	std::string s = "";
	switch (k->keysym.sym) {
		case SDLK_a:
		case SDLK_b:
		case SDLK_c:
		case SDLK_d:
		case SDLK_e:
		case SDLK_f:
		case SDLK_g:
		case SDLK_h:
		case SDLK_i:
		case SDLK_j:
		case SDLK_k:
		case SDLK_l:
		case SDLK_m:
		case SDLK_n:
		case SDLK_o:
		case SDLK_p:
		case SDLK_q:
		case SDLK_r:
		case SDLK_s:
		case SDLK_t:
		case SDLK_u:
		case SDLK_v:
		case SDLK_w:
		case SDLK_x:
		case SDLK_y:
		case SDLK_z: {
			s = SDL_GetKeyName(k->keysym.sym);

			bool should_capitalize = false;
			if (k->keysym.mod & KMOD_SHIFT) {
				should_capitalize = true;
			}
			if (k->keysym.mod & KMOD_CAPS) {
				should_capitalize = !should_capitalize;
			}
			if (!should_capitalize) {
				s = string_lower(s);
			}

			break;
		}

		case SDLK_BACKSLASH: {
			s = "\\";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "|";
			}
			break;
		}
		case SDLK_BACKSPACE: {
			if (!output.empty()) {
				output.pop_back();
			}
			break;
		}
		case SDLK_BACKQUOTE: {
			s = "`";
			if (k->keysym.mod & KMOD_SHIFT) {
				s = "~";
			}
			break;
		}
		case SDLK_SPACE: {
			s = " ";
			break;
		}
		case SDLK_TAB: {
			s = "	";
			break;
		}

		case SDLK_KP_DIVIDE: {
			s = "/";
			break;
		}
		case SDLK_KP_MULTIPLY: {
			s = "*";
			break;
		}
		case SDLK_KP_MINUS: {
			s = "-";
			break;
		}
		case SDLK_KP_PLUS: {
			s = "+";
			break;
		}
	}

	if (s.empty()) {
		switch(k->keysym.sym) {
			case SDLK_0: {
				s = "0";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = ")";
				}
				break;
			}
			case SDLK_1: {
				s = "1";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "!";
				}
				break;
			}
			case SDLK_2: {
				s = "2";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "@";
				}
				break;
			}
			case SDLK_3: {
				s = "3";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "#";
				}
				break;
			}
			case SDLK_4: {
				s = "4";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "$";
				}
				break;
			}
			case SDLK_5: {
				s = "5";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "%";
				}
				break;
			}
			case SDLK_6: {
				s = "6";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "^";
				}
				break;
			}
			case SDLK_7: {
				s = "7";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "&";
				}
				break;
			}
			case SDLK_8: {
				s = "8";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "*";
				}
				break;
			}
			case SDLK_9: {
				s = "9";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "(";
				}
				break;
			}
			case SDLK_QUOTE: {
				s = "'";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "\"";
				}
				break;
			}
			case SDLK_COMMA: {
				s = ",";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "<";
				}
				break;
			}
			case SDLK_EQUALS: {
				s = "=";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "+";
				}
				break;
			}
			case SDLK_LEFTBRACKET: {
				s = "[";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "{";
				}
				break;
			}
			case SDLK_MINUS: {
				s = "-";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "_";
				}
				break;
			}
			case SDLK_PERIOD: {
				s = ".";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = ">";
				}
				break;
			}
			case SDLK_RIGHTBRACKET: {
				s = "]";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "}";
				}
				break;
			}
			case SDLK_SEMICOLON: {
				s = ";";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = ":";
				}
				break;
			}
			case SDLK_SLASH: {
				s = "/";
				if (k->keysym.mod & KMOD_SHIFT) {
					s = "?";
				}
				break;
			}
		}
	}

	if ((s.empty())&&(k->keysym.mod & KMOD_NUM)) {
		switch(k->keysym.sym) {
			case SDLK_KP_0:
			case SDLK_KP_00:
			case SDLK_KP_000: {
				s = "0";
				break;
			}
			case SDLK_KP_1: {
				s = "1";
				break;
			}
			case SDLK_KP_2: {
				s = "2";
				break;
			}
			case SDLK_KP_3: {
				s = "3";
				break;
			}
			case SDLK_KP_4: {
				s = "4";
				break;
			}
			case SDLK_KP_5: {
				s = "5";
				break;
			}
			case SDLK_KP_6: {
				s = "6";
				break;
			}
			case SDLK_KP_7: {
				s = "7";
				break;
			}
			case SDLK_KP_8: {
				s = "8";
				break;
			}
			case SDLK_KP_9: {
				s = "9";
				break;
			}
			case SDLK_KP_PERIOD: {
				s = ".";
				break;
			}
		}
	}

	output += s;
	return s.c_str()[0];
}