/*
 * Faculty of Applied Science and Engineering, University of Toronto
 * CSC181: Introduction to Computer Programming, Fall 2000
 *
 * Assignment: 3
 * File: utility.h
 * Author: Ray Ortigas (rayo@dgp.toronto.edu)
 * Contains: Utility function definitions.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "utility.h"

/* Returns a string whose contents are in the C string s. */
StringPtr stringInit(const char* s) {
	StringPtr result;
	assert(s != NULL);

	result = malloc(sizeof(String));
	assert(result != NULL);

	result->contents = malloc(strlen(s)+1);
	assert(result->contents != NULL);

	strcpy(result->contents, s);
	return result;
}

/* Compares two strings s1 and s2 lexicographically, returning 0 if s1
   and s2 are equal, < 0 if s1 < s2 or > 0 if s1 > s2. */
int stringCompare(void* s1, void* s2) {
	assert(s1 != NULL && s2 != NULL);
	return strcmp(((StringPtr)s1)->contents, ((StringPtr)s2)->contents);
}

/* Returns a C string with contents equivalent to s. */
char* stringToCString(const StringPtr s) {
	char* result = malloc(strlen(s->contents)+1);
	assert(result != NULL);

	strcpy(result, s->contents);
	return result;
}
