Bilag M: Koden til hjlpeprogrammet

#include 
#include 
#include 
#include 


#define FALSE 0
#define TRUE 1

long antal = 4815; //number of objects in database

/* This function searches string source, until string s1 is found. It
then searches for string s2. What is in between is copied into string dest.
If s2 is not found, all that is after s1 is copied. */

void grab_string (char *source, char *dest, char *s1, char *s2)
{
	char *source_index, *source_index2, *dest_index, *s1_index, *s2_index;                   
	int found1, found2, end_of_string;
	
	source_index = source;
	dest_index = dest;
	found1 = FALSE;
	end_of_string = FALSE;
	
	//find s1
	if (*s1 == '\0') found1 = TRUE;
	while (!found1 && !end_of_string)
	{
		while ((*source_index != *s1) && (*source_index != '\0')) source_index++;
		if (*source_index == '\0') 
				end_of_string = TRUE;
			else
			{
				source_index2 = source_index;
				s1_index = s1;
				while (*source_index2 == *s1_index)
				{
					source_index2++;
					s1_index++;
				}
				if (*s1_index == '\0') 
				{
					found1 = TRUE;
					source_index = source_index2;
				}
				else source_index++;
				
			}
	}        

	//find s2                              
	found2 = FALSE;
	while ((*source_index != '\0') && (!found2))
		{
			if (*source_index == *s2)
			{
				source_index2 = source_index;
				s2_index = s2;
				while ((*source_index2 == *s2_index) && (*source_index2 != '\0'))
				{
					source_index2++;
					s2_index++;
				}
				if (*s2_index == '\0') 
				{
					found2 = TRUE;
					source_index = source_index2;
				}
				else 
					*dest = *source_index;
					
				
			}
			else
				*dest = *source_index;
			source_index++;
			if (!found2) dest++;
		}
	*dest = '\0'; 
}

/* Returns the last character of string, not counting line-feeds. */
char last (char *string) 
{          
	char c;
	                      
	if ((*string == '\0') || (*string == '\n')) return '\0';
	do 
	{
		c = *string;
		string++;
	}
	while ( (*string != '\0') && (*string != '\n'));
	return c;
}

/* Removes the last character of string, thus making it one character
shorter. */

void remove_last (char *string)
{
	if (*string != '\0')
	{
		while (*string != '\0') string++;
		string--;
		*string = '\0';
	}
}

/* Adds string2 to string1. */

void add (char *string1, char *string2) 
{
	while ( (*string1 != '\0') && (*string1 != '\n')) string1++;
	while ( (*string2 != '\0') && (*string2 != '\n')) *string1++ = *string2++;
	*(string1) = '\0';
}

/* Removes all preceding spaces from string. */

char* remove_spaces (char *string) 
{                 
	while (*string == ' ') string++;
	return string;                                 
}

/* Writes string to file, making sure that n characters are written. */

void fputs_all (FILE *file, char *string, int n)
{
	for (int i=0; i 96) && (ch < 123))
		return ch-32;
	else
		return ch;
}

/* The function greater_than compares the two strings first and second.
It returns TRUE if first is greater than second, otherwise FALSE. If one
of the strings is '', that string is automatically considered the greatest.
An empty string is considered the smallest. */

int greater_than (char *first, char *second) // first > second ?
{
	if ( *second == '') return FALSE;
	if ( *first == '') return TRUE;
	if ( *first == '\0') return FALSE;
	if ( *second == '\0') return TRUE;
	while ((c2c(*first) == c2c(*second)) && (*first != '\0')) // equal and none \0
	{
		first++;
		second++;
	}
	if (c2c(*first) > c2c(*second))
		return TRUE;
	else
		return FALSE;
}

/* The function greater_than_or_equal_to compares the two
strings first and second. It returns TRUE if first is greater
than or equal to the second, otherwise FALSE. If one of the
strings is '', that string is automatically considered the
greatest. An empty string is considered the smallest. */

int greater_than_or_equal_to (char *first, char *second) // first >= second ?
{
	if ( *second == '') return FALSE;
	if ( *first == '') return TRUE;
	if ( *first == '\0') return FALSE;
	if ( *second == '\0') return TRUE;
	while ((c2c(*first) == c2c(*second)) && (*first != '\0')) // equal and none \0
	{
		first++;
		second++;
	}
	if ((*first == '\0') && (*second == '\0')) return TRUE;
	if (c2c(*first) > c2c(*second))
		return TRUE;
	else
		return FALSE;
}

/* Copies string source into string dest. */

void string_copy (char *dest, char *source)
{
while ((*dest++ = *source++) != '\0');
}

/* The function create_index creates a sorted index file for one variable.
It performs a linear search to find the position of each record, thereby making
a number of passes on the database file equal to the number of records in the
database. A time consuming process, but requires almost no memory. */

void create_index (char *input, char *output, int att_size, int offset)
{
	FILE *in, *out;
	in  = fopen( input, "r+b" );
	out = fopen( output, "w+b" );

	char prev_string[1000]=""; // 1000 = size of largest attribute
	char cur_string[1000]="";
	char smallest_string[1000]="";
	char position[15] ="";

	long sorted=0;
	long pos =0;
	long k;
	int tag;

	printf("Sortering pbegyndt.\n");
	while (sorted < antal)
	{
		if (att_size == 10) //variable = size; sort by number
			strcpy(smallest_string, "20000"); //largest book
		else
		{
			smallest_string[0] = ''; smallest_string[1] = '\0'; // last string of all
		}
		pos = 0;
		for ( k=0; k= atoi(prev_string)) 
						&& (atoi(smallest_string) > atoi(cur_string)) )
					{
						string_copy (smallest_string, cur_string);
						pos = k;
					}
			}
			else
				if ( !tag && greater_than_or_equal_to (cur_string, prev_string) 
						&& greater_than (smallest_string, cur_string) )
					{
						string_copy (smallest_string, cur_string);
						pos = k;
					}
		}
		//write value and position to file
		fputs_all (out, smallest_string, att_size);
		_ltoa (pos, position, 10);
		fputs_all (out, position, 15);
		fseek (in, 1742+pos*1743, SEEK_SET); // set tag in database file
		fputc ('+', in);
		string_copy (prev_string, smallest_string);
		sorted++;
		printf("Sorteret: %d\n",sorted);

	}
	for (k=0; k