int main(int argc, char* argv[]) {
	char buffer[MAX_LEN+1];
	int default_lines = 50;
	int position = 0;
	int current_len;
    char** lines;
    int fold = 0;

    lines = malloc(sizeof(char*) * default_lines);
    while (--argc > 0) {
    	if (strcmp(argv[argc], "-f") == 0) {
    		fold = 1;
    		break;
    	}
    }
    print_heading(fold);
    while (!feof(stdin) && fgets(buffer, MAX_LEN, stdin)) {
    	current_len = strlen(buffer)+1;
    	lines[position] = malloc(sizeof(char) * current_len);
    	memcpy(lines[position], buffer, sizeof(char) * current_len);
    	position++;
        if (position == default_lines) {
            lines = realloc(lines, default_lines += 2);
        }
    }
    quicksort_str(lines, 0, position-1, fold);
    for (int i=0; i<position; i++) {
        printf("%d >> %s", i, lines[i]);
    	free(lines[i]);
    }
    free(lines);
	return EXIT_SUCCESS;
}
void Logger::init(std::ostream* out, Timing::Timer* timer)
{
   // log all messages unless application won't change it
   set_global_message_level(trivial);

   // names for message levels
   m_lvl_names[trivial]    = "Trivial:";
   m_lvl_names[minor]      = "Minor:  ";
   m_lvl_names[major]      = "Major:  ";
   m_lvl_names[critical]   = "Critical:";

   static Timing::No_timer dummy;
   m_timer = &dummy;

   // init output stream
   if (out)
   {
      m_out = out;
      print_heading(*m_out);
      get_stream(0, major) << "Logger output stream set to " << typeid(*out).name() << ", object is " << m_out;
   }
   else
   {
      // fallback
      m_out = &std::clog;
      print_heading(*m_out);
      get_stream(0, major) << "Logger output stream set to std::clog";
   }


   // init timer
   if (timer)
   {
      m_timer = timer;
      get_stream(0, major) << "Logger timer set to " << typeid(*timer).name() << ", object is " << m_timer;
   }
   else
   {
      // fallback: using No_timer
      get_stream(0, critical) << "!!! No timer provided; use " << typeid(*m_timer).name() << " as fallback !!!";
      get_stream(0, critical) << "!!!   Messages would not be timestamped !!!";
   }
}
Exemple #3
0
static void handle_main_input(struct regedit *regedit, int c)
{
	switch (c) {
	case '\t':
		regedit->tree_input = !regedit->tree_input;
		print_heading(regedit);
		break;
	default:
		if (regedit->tree_input) {
			handle_tree_input(regedit, c);
		} else {
			handle_value_input(regedit, c);
		}
	}
}
Exemple #4
0
int regedit_getch(void)
{
	int c;

	SMB_ASSERT(regedit_main);

	c = getch();
	if (c == KEY_RESIZE) {
		tree_view_resize(regedit_main->keys, KEY_HEIGHT, KEY_WIDTH,
				 KEY_START_Y, KEY_START_X);
		value_list_resize(regedit_main->vl, VAL_HEIGHT, VAL_WIDTH,
				  VAL_START_Y, VAL_START_X);
		print_heading(regedit_main);
		show_path(regedit_main);
	}

	return c;
}
int main(int argc, char* argv[]) {
    int directory[MAX_FIELDS] = {0, 0};
    char buffer[MAX_LEN+1];
    int default_lines = 50;
    int fold[MAX_FIELDS] = {0, 0};
    int position = 0;
    int current_len;
    char** lines;

    lines = malloc(sizeof(char*) * default_lines);
    for (int i = 1; i<argc && i<3; i++) {
        if (argv[i][0] == '-') {
            int j = 1;
            while (argv[i][j]) {
                if (argv[i][j] == 'f') {
                    fold[i-1] = 1;
                } else if (argv[i][j] == 'd') {
                    directory[i-1] = 1;
                }
                j++;
            }
        } else {
            printf("Error: option %s not recognized.\n", argv[argc]);
        }
    }
    print_heading(fold, directory);
    while (!feof(stdin) && fgets(buffer, MAX_LEN, stdin)) {
        current_len = strlen(buffer)+1;
        lines[position] = malloc(sizeof(char) * current_len);
        memcpy(lines[position], buffer, sizeof(char) * current_len);
        position++;
        if (position == default_lines) {
            lines = realloc(lines, default_lines += 2);
        }
    }
    quicksort_str(lines, 0, position-1, fold, directory);
    for (int i=0; i<position; i++) {
        printf("%d >> %s", i, lines[i]);
        free(lines[i]);
    }
    free(lines);
    return EXIT_SUCCESS;
}
int main(void) {
    int capacity = DEFAULT_NUM_WORDS;
    char** words = malloc(sizeof(char*) * capacity);
    int* frequencies = malloc(sizeof(int) * capacity);
    int i, found, pos = 0;
    char word[MAXWORD+1];

    print_heading();
    while (strict_getword(word, MAXWORD) != EOF) {
        if (strlen(word) <= 0 || !isalnum(word[0])) 
            continue;
        found = 0;
        for (i = 0; i < pos; i++) {
            if (strcmp(word, words[i]) == 0) {
                frequencies[i]++;
                found = 1;
                break;
            }
        }
        if (!found) {
            frequencies[pos] = 1;
            words[pos] = malloc(sizeof(char) * (strlen(word)+1));
            strcpy(words[pos++], word);
//            printf("added new word %s with frequency %d\n", words[pos-1], frequencies[pos-1]);
        }
        if (pos == capacity) {
//            printf("doubling capacity to %d\n", capacity);
            capacity *= 2;
            words = realloc(words, capacity * sizeof(char*));
            frequencies = realloc(frequencies, capacity * sizeof(int));
        }
    }

    mergesort(words, frequencies, 0, pos-1);
    printf("\nWords in order of frequency are:\n");
    for (i = 0; i < pos; i++)
        printf("%d %s\n", frequencies[i], words[i]);
    for (i = 0; i < pos; i++)
        free(words[i]);
    free(words);
    free(frequencies);
    return EXIT_SUCCESS;
}
Exemple #7
0
int main() {
    print_heading(2, "a game that guess your number correctly.");

    return 0;
}
/**
 * This is the function called from wiz-debug.c.
 */
void stats_collect(void)
{
	static int simtype;
	static bool auto_flag;
	char buf[1024];

	/* Prompt the user for sim params */
	simtype = stats_prompt();

	/* Make sure the results are good! */
	if (!((simtype == 1) || (simtype == 2)))
		return; 

	/* Are we in diving or clearing mode */
	if (simtype == 2)
		clearing = TRUE;
	else
		clearing = FALSE;

	/* Open log file */
	path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "stats.log");
	stats_log = file_open(buf, MODE_WRITE, FTYPE_TEXT);

	/* Logging didn't work */
	if (!stats_log) {
		msg("Error - can't open stats.log for writing.");
		exit(1);
	}

	/* Turn on auto-more.  This will clear prompts for items
	 * that drop under the player, or that can't fit on the 
	 * floor due to too many items.  This is a very small amount
	 * of items, even on deeper levels, so it's not worth worrying
	 * too much about.
	 */
	 auto_flag = FALSE;
	 
	 if (!OPT(auto_more)) {
		/* Remember that we turned off auto_more */
		auto_flag = TRUE;

		/* Turn on auto-more */
		option_set(option_name(OPT_auto_more),TRUE);
	}

	/* Print heading for the file */
	print_heading();

	/* Make sure all stats are 0 */
	init_stat_vals();

	/* Select diving option */
	if (!clearing) diving_stats();

	/* Select clearing option */
	if (clearing) clearing_stats();

	/* Turn auto-more back off */
	if (auto_flag) option_set(option_name(OPT_auto_more), FALSE);

	/* Close log file */
	if (!file_close(stats_log)) {
		msg("Error - can't close randart.log file.");
		exit(1);
	}
}
Exemple #9
0
int main() {
    FILE* bFile;
    char *board = malloc(sizeof(char) * 128);
    int cars[26][3];
    char car = '\0'; // car to move
    char dir = '\0'; // direction of move
    int amount = -1; // number of spaces to move
    int temp = 0;

    print_heading(3, "The Traffic Game");

    bFile = fopen("board.txt", "r");
    if(bFile == NULL) {
        printf("Could not open file board.txt.\n");
        return -1;
    }

    scan_board(bFile, board, cars);

    fclose(bFile);

    printf("Welcome to the traffic game!\n\n");
    printf("Move the vehicles so that the Red car (RR) can\n");
    printf("exit the board to the right. Each move should be\n");
    printf("of the form:    CDN    where C is the vehicle to\n");
    printf("be moved,  D ids the direction (u for up, d for down,\n");
    printf("l for left or r for right), and N is the number of\n");
    printf("squares to move it. For example GR2 means move the\n");
    printf("G vehicle to the right 2 squares. Lower-case input\n");
    printf("such as   gr2   is also accepted.   Entre x to exit the\n");
    printf("program.\n");

    while(1) {
        print_board(board);

        do {
            get_input(&car, &dir, &amount);

            // printf("\n%c%c%d\n", car, dir, amount); //for testing

            if(car == 'X') {
                printf("Thanks for playing! Exiting...\n");
                free(board);
                return 0;
            }

            temp = valid_move(car, dir, amount, cars, board);

            if(! temp) {
                printf("Move not valid. try again.\n");
            }

        }while(! temp);

        if(make_move(car, dir, amount, cars, board)) {
            print_board(board);
            printf("\nCongradulations!! You Win!!!\n\n");
            printf("Thanks for playing! Exiting...\n");
            free(board);
            return 0;
        }
    }
}
Exemple #10
0
static void display_window(TALLOC_CTX *mem_ctx, struct registry_context *ctx)
{
	struct regedit *regedit;
	struct tree_node *root;
	bool colors;
	int key;

	initscr();

	cbreak();
	noecho();

	colors = has_colors();
	if (colors) {
		start_color();
		use_default_colors();
	}

	regedit = talloc_zero(mem_ctx, struct regedit);
	SMB_ASSERT(regedit != NULL);
	regedit_main = regedit;

	regedit->main_window = stdscr;
	keypad(regedit->main_window, TRUE);

	mvwprintw(regedit->main_window, 0, 0, "Path: ");
	regedit->path_label = newpad(1, PATH_WIDTH_MAX);
	SMB_ASSERT(regedit->path_label);
	wprintw(regedit->path_label, "/");
	show_path(regedit_main);

	root = load_hives(regedit, ctx);
	SMB_ASSERT(root != NULL);

	regedit->keys = tree_view_new(regedit, root, KEY_HEIGHT, KEY_WIDTH,
				      KEY_START_Y, KEY_START_X);
	SMB_ASSERT(regedit->keys != NULL);

	regedit->vl = value_list_new(regedit, VAL_HEIGHT, VAL_WIDTH,
				     VAL_START_Y, VAL_START_X);
	SMB_ASSERT(regedit->vl != NULL);

	regedit->tree_input = true;
	print_heading(regedit);

	tree_view_show(regedit->keys);
	menu_driver(regedit->keys->menu, REQ_FIRST_ITEM);
	load_values(regedit);
	value_list_show(regedit->vl);

	update_panels();
	doupdate();

	do {
		key = regedit_getch();

		handle_main_input(regedit, key);
		update_panels();
		doupdate();
	} while (key != 'q' || key == 'Q');

	endwin();
}
Exemple #11
0
int main( int argc, char ** argv )
{
    char path[_MAX_PATH];

    char buffer[ MAX_COMMAND_LINE + 2 ] = { (char)MAX_COMMAND_LINE };  /* Used with _cgets() - maximum number of characters in must be set in 1st byte */

    int    _argc;
    char * _argv[ MAX_ARGS ],
         * result,
         * fullpath;

    int archive_handle = -1;         /* last attached archive */

    int i, cmd;

    if( !ResInit( NULL )) 
        return( 1 );            /* ResInit failed */

    
    /* these would be called after parsing an .ini file or similar */

    IF_DEBUG( ResDbgLogOpen( "resource.log" ));

    print_heading();

    _getcwd( path, _MAX_PATH );
    printf( "PATH: %s\n", path );

    do {
        do {
#if( !RES_USE_FLAT_MODEL )
            printf( "\n%s> ", GLOBAL_CURRENT_PATH );
#else
            printf( "\nRoot > " );
#endif
    _getcwd( path, _MAX_PATH );
    printf( "[SD: %s]>", path );

            result = GETS( buffer );  /* Input a line of text */
            
        } while( !buffer[1] );


        if( !stricmp( result, "exit" ))
            break;

        _argc = parse_args( result, _argv );

        cmd = COMMAND_ERROR;

        for( i=0; i<COMMAND_COUNT; i++ ) {
            if( !stricmp( result, command[i] )) {
                cmd = i;
                break;
            }
        }

        if( cmd == COMMAND_EXIT && !_argc )
            break;

        if( _argc > 1 ) {
            if( !stricmp( _argv[1], "?" ) || !stricmp( _argv[1], "help" )) {
                show_help( cmd );
                continue;
            }
            else
                if( strchr( _argv[1], ASCII_BACKSLASH )) {
                    res_fullpath( path, _argv[1], _MAX_PATH );
                    fullpath = path;
                }
                else
                    fullpath = _argv[1];
        }

        switch( cmd ) 
        {
            case COMMAND_DIR:
#if( RES_USE_FLAT_MODEL )
                printf( "This function is only meaningful when using the hierarchical model.\n" );
                break;
#endif
                if( _argc > 1 )
                    cmd_dir( _argv[1] );
                else
                    cmd_dir( NULL );
                break;


            case COMMAND_ANALYZE:
            {
#if( !RES_USE_FLAT_MODEL )
                HASH_ENTRY * entry;

                if( _argc == 1 )
                    entry = hash_find( GLOBAL_CURRENT_PATH, GLOBAL_HASH_TABLE );
                else
                    entry = hash_find( fullpath, GLOBAL_HASH_TABLE );

                if( entry ) {
#if( RES_DEBUG_VERSION )
                    if( entry -> dir )
                        dbg_analyze_hash( (HASH_TABLE *)entry -> dir );
                    else
                        printf( "No directory table for this directory.\n" );
#endif /* RES_DEBUG_VERSION */
                }
                else
                    printf( "Directory not found.\n" );
#else
                printf( "This command only meaningful when using the hierarchical model!\n" );
#endif
                break;
            }

            case COMMAND_RUN:
                cmd_run( _argc, _argv );
                break;

            case COMMAND_CD:
                if( _argc > 1 ) {
                    if( !ResSetDirectory( fullpath ))
                        printf( "Error changing to directory %s\n", fullpath );
                }
                else
                    printf( "Current directory is: %s\n", GLOBAL_CURRENT_PATH );

                break;


            case COMMAND_ADD:
                if( _argc > 1 ) {
                    int test = FALSE,
                        flag = -1;

                    if( _argc > 2 )
                        flag = is_bool( _argv[2] );
                    
                    if( flag == -1 )
                        flag = TRUE;

                    if( !GLOBAL_SEARCH_INDEX )
                        test = ResCreatePath( fullpath, flag );
                    else
                        test = ResAddPath( fullpath, flag );

                    if( !test )
                        printf( "Error adding %s to search path\n", fullpath );
                }
                else
                    show_help(cmd);
                break;


            case COMMAND_STREAM:
            {
                FILE * fptr;
                char   c;
                int    test;

                if( _argc < 2 ) {
                    show_help(cmd);
                    break;
                }

                fptr = fopen( _argv[1], "r" );

                if( fptr ) {
                    while( (test = fscanf( fptr, "%c", &c )) != EOF )
                        printf( "%c", c );

                    printf( "\n\n\n\n ************** REWINDING ****************** \n\n\n\n\n\n\n" );

                    fseek( fptr, 0, SEEK_SET );

                    while( (test = fscanf( fptr, "%c", &c )) != EOF )
                        printf( "%c", c );

                    fclose( fptr );

                } else {
                    printf( "Error opening file %s\n", _argv[1] );
                }

                break;
            }


            case COMMAND_PATH:
            {
                int x=0;
                char b[_MAX_PATH];

                if( GLOBAL_PATH_LIST ) {

                    while( ResGetPath( x++, b ))
                        printf( "%s\n", b );
                }
                else
                    printf( "No path created.\n" );

                break;
            }

            case COMMAND_EXTRACT:
            {
                /* extracts the archive to the local directory with the same filename */

                if( _argc < 3 )
                    show_help(cmd);
                else              
                    ResExtractFile( _argv[1], _argv[2] );
                break;
            }            

            case COMMAND_READ:
                if( _argc >= 2 )
                    cmd_read( _argv[1] );
                else
                    show_help(cmd);
                break;


            case COMMAND_ATTACH:
            {
                char dst[_MAX_PATH];
                int  flag = -1;

                if( _argc < 2 ) {
                    show_help(cmd);
                    break;
                }

                if( _argc >= 3 )
                    flag = is_bool( _argv[ _argc - 1 ] );

                if( _argc >= 3 ) res_fullpath( dst, _argv[2], _MAX_PATH );

                if( _argc == 2 )
                    archive_handle = ResAttach( GLOBAL_CURRENT_PATH, fullpath, FALSE );
                else
                    if( _argc == 3 ) {
                        if( flag != -1 )
                            archive_handle = ResAttach( GLOBAL_CURRENT_PATH, fullpath, flag );
                        else
                            archive_handle = ResAttach( fullpath, dst, FALSE );
                    } else
                        if( _argc == 4 )
                            archive_handle = ResAttach( fullpath, dst, flag == -1 ? 0 : flag );
                
                if( archive_handle == -1 )
                    printf( "Error attaching zip file %s\n", fullpath );

                break;
            }

            case COMMAND_DUMP:
                ResDbgDump();
                MemDump();          printf("\n");
                MemFindLevels();    printf("\n");
                MemFindUsage();
                break;

            case COMMAND_DETACH:
                if( archive_handle != -1 )
                {
                    ResDetach( archive_handle );
                    archive_handle = -1;
                }
                else
                    printf( "No archives currently attached.\n" );
                break;

            case COMMAND_MAP:
                if( _argc < 2 )
                    show_help(cmd);
                else
                    cmd_map( _argv[1] );
                break;

            case COMMAND_FIND:
                if( _argc < 2 )
                    show_help(cmd);
                else
                    cmd_find( fullpath );
                break;

            case COMMAND_HELP:
                print_heading();
                show_help(cmd);
                break;

            case COMMAND_ERROR:
            default:
                printf( "Syntax error\n" );
                break;
        }

    } while( TRUE );
    
    ResExit();
    MemDump();
    _getcwd( path, _MAX_PATH );
    printf( "PATH: %s\n", path );

    return(0);
}
static void handle_main_input(struct regedit *regedit, int c)
{
	switch (c) {
	case 18: { /* CTRL-R */
		struct tree_node *root, *node;
		const char **path;

		node = tree_view_get_current_node(regedit->keys);
		path = tree_node_get_path(regedit, node);
		SMB_ASSERT(path != NULL);

		root = tree_node_new_root(regedit, regedit->registry_context);
		SMB_ASSERT(root != NULL);

		tree_view_set_root(regedit->keys, root);
		tree_view_set_path(regedit->keys, path);
		node = tree_view_get_current_node(regedit->keys);
		value_list_load(regedit->vl, node->key);
		tree_view_show(regedit->keys);
		value_list_show(regedit->vl);
		print_path(regedit, node);
		talloc_free(discard_const(path));
		break;
	}
	case 'f':
	case 'F':
	case '/': {
		int rv;
		struct regedit_search_opts *opts;
		struct tree_node *node;

		opts = &regedit->active_search;
		rv = dialog_search_input(regedit, opts);
		if (rv == DIALOG_OK) {
			SMB_ASSERT(opts->query != NULL);
			opts->match = find_substring_nocase;
			node = regedit->keys->root->child_head;
			if (opts->search_case) {
				opts->match = find_substring;
			}
			if (!opts->search_recursive) {
				node = tree_view_get_current_node(regedit->keys);
				node = tree_node_first(node);
			}
			regedit_search(regedit, node, NULL, SEARCH_NEXT);
		}
		break;
	}
	case 'x':
		regedit_search_repeat(regedit, SEARCH_NEXT);
		break;
	case 'X':
		regedit_search_repeat(regedit, SEARCH_PREV);
		break;
	case '\t':
		regedit->tree_input = !regedit->tree_input;
		print_heading(regedit);
		break;
	default:
		if (regedit->tree_input) {
			handle_tree_input(regedit, c);
		} else {
			handle_value_input(regedit, c);
		}
	}
}
static WERROR regedit_search(struct regedit *regedit, struct tree_node *node,
			     struct value_item *vitem, unsigned flags)
{
	struct regedit_search_opts *opts;
	struct tree_node *found;
	struct value_item *found_value;
	bool search_key, need_sync;
	char *save_value_name;
	WERROR rv;
	bool (*iterate)(struct tree_node **, bool, WERROR *);
	struct value_item *(*find_item)(struct value_list *,
				        struct value_item *,
				        const char *,
				        regedit_search_match_fn_t);

	opts = &regedit->active_search;

	if (!opts->query || !opts->match) {
		return WERR_OK;
	}

	SMB_ASSERT(opts->search_key || opts->search_value);

	rv = WERR_OK;
	found = NULL;
	found_value = NULL;
	save_value_name = NULL;
	search_key = opts->search_key;
	need_sync = false;
	iterate = tree_node_next;
	find_item = value_list_find_next_item;

	if (flags & SEARCH_PREV) {
		iterate = tree_node_prev;
		find_item = value_list_find_prev_item;
	}

	if (opts->search_value) {
		struct value_item *it;

		it = value_list_get_current_item(regedit->vl);
		if (it) {
			save_value_name = talloc_strdup(regedit,
							it->value_name);
			if (save_value_name == NULL) {
				return WERR_NOMEM;
			}
		}

		if (vitem) {
			search_key = false;
		}
	}

	if (!vitem && (flags & SEARCH_REPEAT)) {
		if (opts->search_value) {
			search_key = false;
		} else if (!iterate(&node, opts->search_recursive, &rv)) {
			beep();
			return rv;
		}
	}

	do {
		if (search_key) {
			SMB_ASSERT(opts->search_key == true);
			if (opts->match(node->name, opts->query)) {
				found = node;
			} else if (opts->search_value) {
				search_key = false;
			}
		}
		if (!search_key) {
			SMB_ASSERT(opts->search_value == true);
			if (!vitem) {
				rv = value_list_load_quick(regedit->vl,
							   node->key);
				if (!W_ERROR_IS_OK(rv)) {
					goto out;
				}
				need_sync = true;
			}
			found_value = find_item(regedit->vl, vitem, opts->query,
						opts->match);
			if (found_value) {
				found = node;
			} else {
				vitem = NULL;
				search_key = opts->search_key;
			}
		}
	} while (!found && iterate(&node, opts->search_recursive, &rv));

	if (!W_ERROR_IS_OK(rv)) {
		goto out;
	}

	if (found) {
		/* Put the cursor on the node that was found */
		if (!tree_view_is_node_visible(regedit->keys, found)) {
			tree_view_update(regedit->keys,
					 tree_node_first(found));
			print_path(regedit, found);
		}
		tree_view_set_current_node(regedit->keys, found);
		if (found_value) {
			if (need_sync) {
				value_list_sync(regedit->vl);
			}
			value_list_set_current_item(regedit->vl, found_value);
			regedit->tree_input = false;
		} else {
			load_values(regedit);
			regedit->tree_input = true;
		}
		tree_view_show(regedit->keys);
		value_list_show(regedit->vl);
		print_heading(regedit);
	} else {
		if (need_sync) {
			load_values(regedit);
			value_list_set_current_item_by_name(regedit->vl,
							    save_value_name);
		}
		beep();
	}

out:
	talloc_free(save_value_name);

	return rv;
}