#include "insertion_sort_types.h"

/**************************************************************************/

/* check command line for correct number of files */
void check_command_line (int argc, char *argv[], char file[])
{
        if (argc != 2) {
                printf("Error: number of arguments in command line.\n");
                printf("Enter name of unsorted file: ");
                scanf("%s", file);
        } else
                strcpy (file, argv[1]);
}

/************************************************************************/

FILE *openfile (char filename[])
{
        FILE *infile;

        if ((infile = fopen(filename, "r")) == NULL) {
                printf("Error opening file %s.\n", filename);
                exit(1);
        }

        return(infile);
}

/************************************************************************/

void initialize_list (int list[MAX])
{
	int a;
	
	for(a=0; a<MAX; a++)
		list[a] = -1;
	
	return;
}

/************************************************************************/

void sort_file (int key, int sorted_list[MAX], int a)
{
	int b = 0, c = 0;
	
	if (sorted_list[b] == -1) {
		sorted_list[b] = key;
		return;
	}
	
	/* find location */
	while (sorted_list[c] < key && sorted_list[c] != -1)
		c++;
	b = c;
	
	while (sorted_list[a - 1] > key) {
		sorted_list[a] = sorted_list[a - 1];
		a--;
		if (a > 0)
			;
		else
			break;
	}
	
	sorted_list[b] = key;
		
	return;
}

/************************************************************************/

void print_list (int sorted_list[MAX], int how_long)
{
	int a;

	printf("The sorted list is:\n");
	
	for(a=0; a<how_long; a++) {
		printf("%d\n", sorted_list[a]);
	}

	return;
}

