Example #1
0
File: tail.c Project: hmirza/ECS-30
int main(int argc, char* argv[]){
  
  FILE* input_file = NULL; //File pointer
  
  int lines = 0; // Initializes lines variable type int to 0
  
  int numlines = atoi(argv[2]); //Converts command line arguement to type integer
  
  lines = get_number_of_lines (input_file, argv); //Calls the get_number_of_lines function to get the number of lines in a file
  
  printlines(input_file, argv, lines, numlines); // Calls the printlines function to print the N number of lines
  
  return 0;
}
Example #2
0
int main(int argc, char* argv[]){
	
	int default_case = -1;
	int sort_key = -1;
	
	int num_lines_file = 0;

	FILE *fp;

	char *filename;
	char line[LINE_SIZE];

	struct line_helper *line_array;

	//printf("%d\n", argc);

	if(argc < 2 || argc > 3){
		fprintf(stderr, BAD_CMD_LINE_PARAMETERS);
		return FAILURE;
	}

	if(argc == 2){
		default_case = 1;
		filename = argv[1];
	}else{ 
		default_case = 0;
		filename = argv[2];
		sort_key = atoi(argv[1]) * -1;
		if(sort_key <= 0){
			fprintf(stderr, BAD_CMD_LINE_PARAMETERS);
			return FAILURE;
		}
		// printf("Sort key : %d\n", sort_key);
	}

	// printf("File name : %s\n", filename);

	fp = fopen(filename, READ_FLAG);
	if(fp == NULL){
		fprintf(stderr, "Error: Cannot open file %s\n",filename);
		return FAILURE;
	}

	num_lines_file = get_number_of_lines(fp);
	// printf("\nNumber of lines in file : %d\n", num_lines_file);
	
	line_array = (struct line_helper*) malloc( num_lines_file * sizeof(struct line_helper));
	if(line_array == NULL){
		fprintf(stderr, MALLOC_FAIL);
		return FAILURE;
	}

	int line_number = 0;

	fseek( fp, 0, SEEK_SET);
	while(fgets(line, LINE_SIZE, fp) != NULL){

		int line_length = 0;
		int eol_found = 0;

		// printf("Line read : %s", line);
		// printf("\n&&&&&&&& String length of line  : %lu\n", strlen(line));
		while(line_length < LINE_SIZE){
			line_length++;
			if(line[line_length] == '\n'){
				eol_found = 1;
				break;
			}
		}

		if(eol_found == 0){
			fprintf(stderr, LONG_LINE);
			return FAILURE;
		}

		//line_array[line_number].word = "";
		//printf("Line read : %s", line);
		strcpy(line_array[line_number].line,line);
		line_array[line_number].line_num = line_number;

		char* token = strtok (line," ");
		char *prev = NULL;

		if(default_case != 1){
			int token_num = 1;
			

			while (token != NULL){

			    // printf ("Token : %s\n",token);

			    if(token_num >= sort_key || token == NULL){
			    	break;
			    }

			    prev = strdup(token);
			    token = strtok (NULL, " ");
			    token_num++;
		  	}
	  	}

	  	if(token == NULL){
	  		token = strdup(prev);
	  	}

	  	int i = 0;
	  	while(token != NULL && token[i] != '\0'){
	  		if(token[i] == '\n'){
	  			token[i] = '\0';
	  			break;
	  		}
	  		i++;
	  	}

	  	strcpy(line_array[line_number].word,token);
	  	// printf("Word to be sorted : %s\n", line_array[line_number].word);

		line_number++;
	}

	// printf("\n*****Original lines : *****\n");
	// print_lines(line_array, num_lines_file);

	qsort(line_array, num_lines_file, sizeof(struct line_helper), compare);

	//printf("\n*****Sorted lines : *****\n");
	print_lines(line_array, num_lines_file);
	
	return SUCCESS;
}
Example #3
0
int main(int argc, char **argv)
{
    /* Prepares globally used data on program start_up */
    int number_of_lines = 0, number_of_matches = 0, number_of_rounds = 0, match_number = 0, user_input = 0;
    char **file_string, early[STR_LEN2], late[STR_LEN2], weekday[STR_LEN];
    match_s *match; /* Note: Array declared, but not allocated or initialized */
    team_s *team; /* Note: Array declared, but not allocated or initialized */

    get_number_of_lines(&number_of_lines);
    get_number_of_matches(number_of_lines, &number_of_matches);
    get_number_of_rounds(number_of_lines, &number_of_rounds);
    file_string = (char **)calloc(number_of_lines,sizeof(char *));
    for(match_number = 0; match_number < number_of_matches; match_number++) {
        file_string[match_number] = (char *)calloc(LINE_LENGTH,sizeof(char));
    }
    read_file(file_string);
    match = (match_s *)calloc(number_of_matches,sizeof(match_s));
    save_file_to_struct(match, file_string, number_of_lines);

    team = (team_s *)calloc(NUMBER_OF_TEAMS,sizeof(team_s));
    get_teams(team, match, number_of_matches);

    /* Run program with user interaction */

    if(argc == 2 && strcmp(argv[1], "--print") == 0) {
        do_all(match, team, number_of_matches, number_of_rounds);
    } else {
        printf("Please input a numner between 1 and 6 to run said program.\nInput 0 to exit the program:\n");
        printf("Input:>> ");
        scanf("%d",&user_input);

        while(user_input != 0) {

            switch (user_input) {
            case 1:
                printf("\nTask one:\n");
                printf("Print all matches that has 7 or more goals total:\n");
                task_one(match, number_of_matches);
                break;
            case 2:
                printf("\nTask two:\n");
                printf("Print the first round with most goals.\n");
                task_two(match, number_of_rounds);
                break;
            case 3:
                printf("\nTask three:\n");
                printf("Get the teams that win more mathces out than home:\n");
                task_three(team);
                break;
            case 4:
                printf("Task four:\n");
                printf("Print the team with fewest spectators when playing home.\n");
                task_four(team);
                break;
            case 5:
                printf("Task five:\n");
                printf("Print the matches in a specific time interval, and sort them by number of goals.\n");
                prompt_time(early, late, weekday);
                task_five(match, number_of_matches, early, late, weekday);
                break;
            case 6:
                printf("Task six:\n");
                printf("Print the result for the end of the season.\n");
                task_six(team, number_of_rounds);
                break;
            default:
                printf("Invalid user input!\n");
                break;
            }

            user_input = 0;

            printf("\nTo perform another task, input a number from 1 - 6.\nTo exit, input 0.\n");
            printf("Input:>> ");
            scanf("%d",&user_input);
        }
    }

    free(match);
    free(team);

    return 0;
}
Example #4
0
LRESULT CALLBACK view_context_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	static HWND grippy=0;
	int lines,dir,do_scroll=FALSE,update_scroll_pos=TRUE;
	static int divider_drag=FALSE,org_row_width=90,row_width=90,last_pos=0;
#ifdef _DEBUG
	if(FALSE)
//	if(message!=0x200&&message!=0x84&&message!=0x20&&message!=WM_ENTERIDLE)
//	if(msg!=WM_MOUSEFIRST&&msg!=WM_NCHITTEST&&msg!=WM_SETCURSOR&&msg!=WM_ENTERIDLE&&msg!=WM_DRAWITEM
//		&&msg!=WM_CTLCOLORBTN&&msg!=WM_CTLCOLOREDIT&&msg!=WM_CTLCOLORSCROLLBAR)
	if(msg!=WM_NCHITTEST&&msg!=WM_SETCURSOR&&msg!=WM_ENTERIDLE)
	{
		static DWORD tick=0;
		if((GetTickCount()-tick)>500)
			printf("--\n");
		printf("v");
		print_msg(msg,lparam,wparam);
		tick=GetTickCount();
	}
#endif	
	switch(msg)
	{
	case WM_INITDIALOG:
		grippy=create_grippy(hwnd);
		init_context_win_anchor(hwnd);
		get_ini_value("CONTEXT_WINDOW","row_width",&row_width);
		set_context_divider(hwnd,row_width);
		restore_context_rel_pos(hwnd);

		SendDlgItemMessage(hwnd,IDC_CONTEXT_SCROLLBAR,SBM_SETRANGE,0,10000);
		{
			int tabstop=21; //4 fixed chars
			SendDlgItemMessage(hwnd,IDC_CONTEXT,EM_SETTABSTOPS,1,&tabstop);
		}
		set_context_font(hwnd);
		open_file(&gfh);
		if(gfh==0){
			WCHAR str[MAX_PATH*2];
			_snwprintf(str,sizeof(str)/sizeof(WCHAR),L"cant open %s",fname);
			str[sizeof(str)/sizeof(WCHAR)-1]=0;
			MessageBoxW(hwnd,str,L"error",MB_OK);
			EndDialog(hwnd,0);
			return 0;
		}
		get_file_size(gfh,&fsize);
		_fseeki64(gfh,start_offset,SEEK_SET);
		set_scroll_pos(hwnd,IDC_CONTEXT_SCROLLBAR,gfh);
		SetFocus(GetDlgItem(hwnd,IDC_CONTEXT_SCROLLBAR));
		line_count=get_number_of_lines(hwnd,IDC_CONTEXT);
		fill_context(hwnd,IDC_CONTEXT,gfh);
		close_file(&gfh);
		last_pos=-1;
		orig_edit=SetWindowLong(GetDlgItem(hwnd,IDC_CONTEXT),GWL_WNDPROC,subclass_edit);
		orig_scroll=SetWindowLong(GetDlgItem(hwnd,IDC_CONTEXT_SCROLLBAR),GWL_WNDPROC,subclass_scroll);
		SetWindowTextW(hwnd,fname);
		return 0;
	case WM_DESTROY:
		save_context_rel_pos(hwnd);
		break;
	case WM_HELP:
		context_help(hwnd);
		return TRUE;
		break;
	case WM_SIZE:
		grippy_move(hwnd,grippy);
		set_context_divider(hwnd,row_width);
		line_count=get_number_of_lines(hwnd,IDC_CONTEXT);
		open_file(&gfh);
		fill_context(hwnd,IDC_CONTEXT,gfh);
		set_scroll_pos(hwnd,IDC_CONTEXT_SCROLLBAR,gfh);
		close_file(&gfh);
		break;
	case WM_RBUTTONDOWN:
	case WM_LBUTTONUP:
		ReleaseCapture();
		if(divider_drag){
			write_ini_value("CONTEXT_WINDOW","row_width",row_width);
			divider_drag=FALSE;
		}
		break;
	case WM_LBUTTONDOWN:
		SetCapture(hwnd);
		SetCursor(LoadCursor(NULL,IDC_SIZEWE));
		divider_drag=TRUE;
		org_row_width=row_width;
		break;
	case WM_MOUSEFIRST:
		{
			int x=LOWORD(lparam);
			SetCursor(LoadCursor(NULL,IDC_SIZEWE));
			if(divider_drag){
				RECT rect;
				GetClientRect(hwnd,&rect);
				if((rect.right-x)>25 && x>5){
					row_width=x;
					set_context_divider(hwnd,row_width);
				}
			}
		}
		break;
	case WM_MOUSEWHEEL:
		{
			short wheel=HIWORD(wparam);
			int flags=LOWORD(wparam);
			if(wheel>0){
				dir=-1;
				if(flags&MK_RBUTTON)
					lines=line_count-2;
				else
					lines=3+1;
			}
			else{
				dir=1;
				if(flags&MK_RBUTTON)
					lines=line_count-2-1;
				else
					lines=3;
			}
			do_scroll=TRUE;
		}
		break;
	case WM_VSCROLL:
		{
		int pos=HIWORD(wparam);
		switch(LOWORD(wparam)){
		case SB_TOP:
			if(GetKeyState(VK_CONTROL)&0x8000){
				last_offset=0;
				current_line=1;
			}
			else{
				last_offset=start_offset; //_fseeki64(f,start_offset,SEEK_SET);
				current_line=start_line;
			}
			lines=dir=0;
			do_scroll=TRUE;
			break;
		case SB_PAGEUP:
			dir=-1;
			lines=line_count-2;
			if(lines<=0)
				lines=1;
			do_scroll=TRUE;
			break;
		case SB_PAGEDOWN:
			dir=1;
			lines=line_count-2-1;
			if(lines<=0)
				lines=1;
			do_scroll=TRUE;
			break;
		case SB_LINEUP:
			dir=-1;
			lines=2;
			do_scroll=TRUE;
			break;
		case SB_LINEDOWN:
			dir=1;
			lines=1;
			do_scroll=TRUE;
			break;
		case SB_THUMBTRACK:
			//printf("pos=%i last_pos=%i scroll_pos=%i line_count=%i\n",HIWORD(wparam),last_pos,scroll_pos,line_count);
			if(pos<last_pos){
				dir=-1;
				lines=line_count/4;
				if(lines<=1)
					lines=2;
				do_scroll=TRUE;
			}
			else if(pos>last_pos){
				dir=1;
				lines=line_count/4;
				if(lines==0)
					lines=1;
				do_scroll=TRUE;
			}
			if(last_pos==-1)
				do_scroll=FALSE;
			last_pos=pos;
			update_scroll_pos=FALSE;
			break;
		case SB_THUMBPOSITION: //dragged and released
			dir=lines=0;
			do_scroll=TRUE;
			break;
		case SB_ENDSCROLL:
			last_pos=-1;
			break;
		}
		}
		break;
	case WM_COMMAND:
		switch(LOWORD(wparam)){
		case IDCANCEL:
			if(divider_drag){
				divider_drag=FALSE;
				ReleaseCapture();
				set_context_divider(hwnd,org_row_width);
				row_width=org_row_width;
				return 0;
			}
			if(gfh!=0)
				fclose(gfh);
			gfh=0;
			if(orig_edit!=0)SetWindowLong(GetDlgItem(hwnd,IDC_CONTEXT),GWL_WNDPROC,orig_edit);
			EndDialog(hwnd,0);
			return 0;
		}
		break;
	}
	if(do_scroll)
		do_scroll_proc(hwnd,lines,dir,update_scroll_pos);
	return 0;
}