Exemple #1
0
int main() 
{
	ifstream infile( "alice_emma" );

	istream_iterator<string> ifile( infile );
	istream_iterator<string> eos;

	vector< string > text;
	copy( ifile, eos, inserter( text, text.begin() ));

	string filt_elems( "\",.?;:" );
	filter_string( text.begin(), text.end(), filt_elems );

	vector<string>::iterator iter;

	sort( text.begin(), text.end() );
	iter = unique( text.begin(), text.end() );
	text.erase( iter, text.end() );
	
	ofstream outfile( "alice_emma_sort" );

	iter = text.begin();
	for ( int line_cnt = 1; iter != text.end(); ++iter, ++line_cnt ) 
	{
		outfile << *iter << " ";
		if ( ! ( line_cnt % 8 ))
	     		outfile << '\n';
	}
}
Exemple #2
0
int main(int argc, char *argv[])
{
	FILE *in = stdin;
	char buf[0x4000]; /* limited to 16K character word length */
	cp_avltree *wordlist;
	cp_avltree *freqlist;
	int count = 0;
	int one = 1;
	int *wc;

	if (argc > 1) 
	{
		in = fopen(argv[1], "r");
		if (in == NULL)
		{
			fprintf(stderr, "can\'t open %s\n", argv[1]);
			exit(1);
		}
	}
	
	/* wordlist keys are distinct */
	wordlist = 
		cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | 
									COLLECTION_MODE_COPY | 
									COLLECTION_MODE_DEEP, 
									(cp_compare_fn) strcmp, 
									(cp_copy_fn) strdup, free, 
									(cp_copy_fn) intdup, free);
	
	while ((fscanf(in, "%s", buf)) != EOF)
	{
		filter_string(buf, PUNCT);
		to_lowercase(buf);
		wc = cp_avltree_get(wordlist, buf);
		if (wc) 
			(*wc)++;
		else
			cp_avltree_insert(wordlist, buf, &one);
		count++;
	}

	fclose(in);

	/* frequency list entry keys are not distinct */
	freqlist = 	
		cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | 
									COLLECTION_MODE_MULTIPLE_VALUES,
									(cp_compare_fn) freqcmp, 
									NULL, NULL, NULL, NULL);
	
	cp_avltree_callback(wordlist, add_freq, freqlist);
	cp_avltree_callback(freqlist, print_word_freq, &one);
	printf("%d words, %d distinct entries\n", count, cp_avltree_count(wordlist));

	cp_avltree_destroy(freqlist);
	cp_avltree_destroy(wordlist);

	return 0;
}
Exemple #3
0
int main() {
    std::string str;

    while (std::getline(std::cin, str) && str != "DONE") {
        str = filter_string(str);
        bool palindrome = true;
        for (auto i = 0u; i < str.size()/2 && palindrome; i++)
            palindrome &= (str[i] == str[str.size() - i - 1]);

        std::cout << (palindrome ? "You won't be eaten!\n" : "Uh oh..\n");
    }

    return 0;
}
Exemple #4
0
/*
 * Pretty print single event.
 */
static void print_events(struct lttng_event *event)
{
	switch (event->type) {
	case LTTNG_EVENT_TRACEPOINT:
	{
		if (event->loglevel != -1) {
			MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
				indent6,
				event->name,
				loglevel_string(event->loglevel),
				event->loglevel,
				enabled_string(event->enabled),
				filter_string(event->filter));
		} else {
			MSG("%s%s (type: tracepoint)%s%s",
				indent6,
				event->name,
				enabled_string(event->enabled),
				filter_string(event->filter));
		}
		break;
	}
	case LTTNG_EVENT_PROBE:
		MSG("%s%s (type: probe)%s%s", indent6,
				event->name, enabled_string(event->enabled),
				filter_string(event->filter));
		if (event->attr.probe.addr != 0) {
			MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
		} else {
			MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
			MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
		}
		break;
	case LTTNG_EVENT_FUNCTION:
	case LTTNG_EVENT_FUNCTION_ENTRY:
		MSG("%s%s (type: function)%s%s", indent6,
				event->name, enabled_string(event->enabled),
				filter_string(event->filter));
		MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
		break;
	case LTTNG_EVENT_SYSCALL:
		MSG("%ssyscalls (type: syscall)%s%s", indent6,
				enabled_string(event->enabled),
				filter_string(event->filter));
		break;
	case LTTNG_EVENT_NOOP:
		MSG("%s (type: noop)%s%s", indent6,
				enabled_string(event->enabled),
				filter_string(event->filter));
		break;
	case LTTNG_EVENT_ALL:
		/* We should never have "all" events in list. */
		assert(0);
		break;
	}
}
int
phone_utils_remove_filler_chars(char *number)
{
	return filter_string(number, filler_chars);
}
/*FIXME: handle failed memory allocations! */
char *
phone_utils_normalize_number_using_params(const char *_number, const char *param_country,
					const char *param_area)
{
	/*FIXME: add support for normalizing completely to what's wanted */
	char *number = strdup(_number);
	char *tmp;
	int found;
	char *pos;
	int len;
	const char *international, *national;
	int international_len, national_len;
	/* Optimize, already got those saved, get them */
	international = phone_utils_get_user_international_prefix();
	national = phone_utils_get_user_national_prefix();
	international_len = strlen(international);
	national_len = strlen(national);

	/* on error */
	if (!number) {
		return NULL;
	}
	
	remove_from_chrs(&number, trailing_delimiters, 0);
	len = filter_string(number, filler_chars);
	number = realloc(number, len + 1);
	
	len = remove_from_chrs_r(&number, possible_chars, 1);

	/* if empty, i.e some sort of code / ilegal number, return the number */
	if (len == 0) {
		free(number);
		return strdup(_number);
	}

	/* if normalized, replace the CC and AC to the new ones*/
	if (number[0] == '+') {
		/* Strip CC and AC and then replace them. */
		pos = number + 1;
		found = _phone_utils_get_prefix_of_number(&country_code, pos);
		if (found >= 0) {
			pos += country_code.fields[found].len;
			found = _phone_utils_get_prefix_of_number(&area_code, pos);
			if (found >= 0) {
				int total_len;
				int param_country_len = strlen(param_country);
				int param_area_len = strlen(param_area);
				pos += area_code.fields[found].len;
				len -= pos - number;
				tmp = number;
				/* malloc: +1 for '+' */ 
				total_len = 1 + param_country_len + param_area_len + len;
				number = malloc (total_len + 1);
				number[0] = '+';
				strncpy(&number[1], param_country, param_country_len);
				strncpy(&number[1 + param_country_len], param_area, param_area_len);
				strncpy(&number[1 + param_country_len + param_area_len], pos, len);
				number[total_len] = '\0';

				free(tmp);
			}
		}
	
	}
	/* step 1: normalize 00 to + */
	else if (international_len > 0 && !strncmp(number, international, international_len)) {
		tmp = number;

		number = strdup(&number[international_len - 1]);
		*number = '+';

		free(tmp);
	}
	/* step 2: normalize national prefix to +<CC>
	* if national = "" assume it's a match */
	else if (national_len >= 0 && !strncmp(number, national, national_len)) {
		int total_len;
		int param_country_len = strlen(param_country);
		tmp = number;
		/* malloc: +1 for '+' */ 
		total_len = 1 + param_country_len + len - national_len; 
		number = malloc(total_len + 1);
		number[0] = '+';
		strncpy(&number[1], param_country, param_country_len);
		strncpy(&number[1 + param_country_len], &tmp[national_len], len - national_len);
		number[total_len] = '\0';

		free(tmp);
	}
	/* by default, just try to add +<CC> to the start, better than not trying at all. */
	else {
		int total_len;
		int param_country_len = strlen(param_country);
		tmp = number;
		/* malloc: +1 for '+' */ 
		total_len = 1 + param_country_len + len;
		number = malloc (total_len + 1);
		number[0] = '+';
		strncpy(&number[1], param_country, param_country_len);
		strncpy(&number[1 + param_country_len], tmp, len);
		number[total_len] = '\0';

		free(tmp);
	}

	return number;
}
/**
 * 
 *  rct2: 0x006AADA3
 */
static void window_editor_object_selection_scrollpaint()
{
	int x, y, i, colour, colour2, numObjects, type;
	short scrollIndex;
	rct_object_entry *entry;
	rct_object_filters *filter;
	rct_window *w;
	rct_drawpixelinfo *dpi;
	uint8 *itemFlags;
	uint8 source;

	window_scrollpaint_get_registers(w, dpi, scrollIndex);

	colour = RCT2_ADDRESS(0x0141FC48, uint8)[w->colours[1] * 8];
	colour = (colour << 24) | (colour << 16) | (colour << 8) | colour;
	gfx_clear(dpi, colour);

	numObjects = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, sint32);
	entry = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
	itemFlags = RCT2_GLOBAL(0x009ADAEC, uint8*);
	y = 0;
	for (i = 0; i < numObjects; i++) {
		filter = get_object_filter(i);
		type = entry->flags & 0x0F;
		source = (entry->flags & 0xF0) >> 4;
		if (type == w->selected_tab && !(*itemFlags & 0x20) && filter_source(entry) && filter_string(entry) && filter_chunks(entry, filter)) {
			if (y + 12 >= dpi->y && y <= dpi->y + dpi->height) {
				// Draw checkbox
				if (!(RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TRACK_MANAGER) && !(*itemFlags & 0x20))
					gfx_fill_rect_inset(dpi, 2, y, 11, y + 10, w->colours[1], 0xE0);

				// Highlight background
				colour = 142;
				if (entry == (rct_object_entry*)w->var_494 && !(*itemFlags & 0x20)) {
					gfx_fill_rect(dpi, 0, y, w->width, y + 11, 0x2000031);
					colour = 14;
				}

				// Draw checkmark
				if (!(RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TRACK_MANAGER) && (*itemFlags & 1)) {
					x = 2;
					RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, sint16) = colour == 14 ? -2 : -1;
					colour2 = w->colours[1] & 0x7F;
					if (*itemFlags & 0x1C)
						colour2 |= 0x40;

					gfx_draw_string(dpi, (char*)0x009DED72, colour2, x, y);
				}

				// Draw text
				char *buffer = (char*)0x0141ED68;
				*buffer = colour;
				strcpy(buffer + 1, object_get_name(entry));
				if (RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TRACK_MANAGER) {
					while (*buffer != 0 && *buffer != 9)
						buffer++;

					*buffer = 0;
				}

				if (*itemFlags & 0x20) {
					colour = w->colours[1] & 0x7F;
					RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, sint16) = -1;
				} else {
					colour = 0;
					RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, sint16) = 224;
				}
				x = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TRACK_MANAGER ? 0 : 15;
				gfx_draw_string(dpi, (char*)0x0141ED68, colour, x, y);
			}
			y += 12;
		}

		entry = object_get_next(entry);
		itemFlags++;
	}
}
/**
 * Takes the y coordinate of the clicked on scroll list
 * and converts this into an object selection.
 * Returns the position in the list.
 * Object_selection_flags, installed_entry also populated
 *
 * rct2: 0x006AA703
 */
static int get_object_from_object_selection(uint8 object_type, int y, uint8 *object_selection_flags, rct_object_entry **installed_entry)
{
	rct_object_filters *filter;
	*installed_entry = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
	uint8* selection_flags = RCT2_GLOBAL(RCT2_ADDRESS_EDITOR_OBJECT_FLAGS_LIST, uint8*);
	uint8 source;
	int object_count = 0;
	for (int i = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); i > 0; --i){
		filter = get_object_filter(RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) - i);
		source = ((*installed_entry)->flags & 0xF0) >> 4;
		if (((*installed_entry)->flags & 0xF) == object_type && filter_source(*installed_entry) && filter_string(*installed_entry) && filter_chunks(*installed_entry, filter)){
			if (!(*selection_flags & 0x20)){
				y -= 12;
				*object_selection_flags = *selection_flags;
				if (y < 0)return object_count;
				object_count++;
			}
		}

		*installed_entry = object_get_next(*installed_entry);
		selection_flags++;
	}
	return -1;
}