/*
 * Copyright (c) 1998-2005 by Wayne C. Gramlich.
 * All rights reserved.
 */

/*
 * This module implements a fixed sized vector of strings.
 */

#ifndef ASSERT_H
#include "assert.h"
#endif

#ifndef MEMORY_H
#include "memory.h"
#endif

#ifndef STRING_H
#include "string.h"
#endif

#ifndef STHEADERS_H
#include "stheaders.h"
#endif

struct string_array_struct {
    unsigned size;		/* Number of strings in string array */
    String *strings;		/* The STIPPLE string values */
    Str *strs;			/* The temporary Unix (char *) values */
};

typedef struct string_array_struct *String_array, String_array_struct;

typedef void *System;

extern System system__standard(void);

extern module___object string_array__module__object;
extern type___reference string_array_type_ref;

static String_array_struct
  string_array___initial_value; /* Initialize to zeros */
String_array string_array___initial = &string_array___initial_value;

extern module___object string_array__module__object;
extern type___reference string_array_type_ref;

static object___object string_array_initial_object = {
    "",				/* Package name */
    "string_array",			/* Type name */
    "??",			/* Object name */
    (type___reference *)0,	/*XXX: Fix me */
    (int *)0,			/* Size of float object */
    0,				/* Static needs count */
    (need___entry *)0,		/*XXX: Fix me Static needs */
    0,				/* Parameter needs count */
    (need___entry *)0,		/* Parameter needs */
    (void **)&string_array___initial,	/* Object pointer */
    (instantiation___object *)0	/* Instantiation list */
};
static object___object *object_list[1] = {
    &string_array_initial_object,
};

/*
 * string_array__external__initialize()
 *	This routine will initialize the string_array module.
 */
void
string_array__external__initialize(void)
{
}

/*
 * This procedure will create and return a new {string_array} object
 * with {size} slots initialized to contain {initial_value}.
 */
String_array
string_array__create(
    String initial_value,
    unsigned size)
{
    unsigned bytes;
    unsigned index;
    Str *strs;
    String *strings;
    String_array string_array;
    System system;

    bytes = (sizeof(String) + 1) * size;
    strings = (String *)memory_allocate(bytes);
    strs = (Str *)memory_allocate(bytes);

    for (index = 0; index < size; index++) {
	strings[index] = initial_value;
	strs[index] = (Str)0;
    }
    strings[size] = (String)0;
    strs[size] = (Str)0;

    string_array = memory_alloc(String_array);
    string_array->size = size;
    string_array->strings = strings;
    string_array->strs = strs;
    return string_array;
}

/*
 * This procedure will return the {index}'th string from {string_array}.
 */
String
string_array__fetch1(
    String_array string_array,
    unsigned index)
{
    unsigned size;

    size = string_array->size;
    assert (index < size);
    return string_array->strings[index];
}

/*
 * This procedure will store {value} into the {index}'th slot of
 * {string_array}.
 */
void
string_array__store1(
    String_array string_array,
    unsigned index,
    String value)
{
    unsigned size;

    size = string_array->size;
    assert (index < size);
    string_array->strings[index] = value;
}

/*
 * This procedure will return the size of {string_array}.
 */
unsigned
string_array__size_get(
    String_array string_array)
{
    return string_array->size;
}

/*
 * This procedure will return a null terminated vector of Unix strings
 * that corresponds to the STIPPLE strings in {string_array}.  This
 * vector is quite temporary and is only suitable for passing in as
 * a value to the execve(2) system call.
 */
Str *
string_array__unix_strings_get(
    String_array string_array)
{
    unsigned index;
    unsigned size;
    Str *strs;
    String *strings;
    Str var;
    String variable;

    size = string_array->size;
    strs = string_array->strs;
    strings = string_array->strings;
    for (index = 0; index < size; index++) {
	variable = strings[index];
	var = string__unix_string(variable);
	strs[index] = var;
    }
    strs[size] = (Str)0;
    return strs;
}
