/* 
 * Vmisc.c - 'C' file for miscellaneous support functions.
 * 
 * Author:   L. Van Warren
 * Date:    June 1985
 * Copyright (c) 1985-1995 L. Van Warren * All Rights Reserved
 */
#include <std.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ComplexMath.h"
#include "Math2.h"
#include "Vmisc.h"


/*****************************************************************
 * TAG( entab )
 * 
 * Output indentLevel number of tabs to a file.
 * 
 * Inputs:
 *  A file and an indentation level.
 * Outputs:
 *  indentLevel number of tabs..
 */

void entab(FILE *file, int indentLevel)
{
    int i;
    
    for(i = 0; i < indentLevel; i++)
    {
        fprintf(file, "  ");
    }
}

/*****************************************************************
 * TAG( error )
 * 
 * Error routine.
 * 
 * Inputs:
 *  A pointer to a message string.
 * Outputs:
 *  A message to the file descriptor stderr.
 */

void error(char *message)
{
    fprintf(stderr, "%s\n", message);
}

/* ******* */
/* Strings */
/* ******* */

String32
asnString32(name_ptr)
char *name_ptr;
{
    int length;
    int i;
    String32 name32;
    
    name32.c[21] = '\0'; /* No matter what. */

    length = MIN(31, strlen(name_ptr));

    strncpy(name32.c, name_ptr, length);

    for(i = length; i < 32; i++)
    {
		name32.c[i] = '\0';
    }

    return(name32);
}

static int next_name = 1;

String32
genString32(void)
{
    String32 name32;
    
    sprintf(name32.c, "%d", next_name++);

    return(name32);
}

int
hashString32(name, hashsize)
String32 name;
int hashsize;
{
    int i=0, count=0;

    while ( name.c[i] != '\0' && i < 32)
    {
        count += name.c[i++] * i;
    }

    return (count % hashsize);
}

int
isSameString32(a, b)
String32 a, b;
{
    int return_val;

    if(strncmp(a.c, b.c, 32) == 0)
    {
        return_val = 1;
    }
    else
    {
        return_val = 0;
    }

    return(return_val);
}

String32 fscanString32(FILE* f)
{
    String32 ret ;

    fscanf (f, "%s", ret.c) ;

    return ret ;
}

void fprintString32 (FILE* f, String32 inst)
{
    fprintf (f, "%s", inst.c) ;
    NEWLINE(f);
}

String32 freadString32(FILE* f)
{
    String32 ret ;

    fread (&ret, sizeof(ret), 1, f) ;
    return ret ;
}

void fwriteString32 (FILE* f, String32 inst)
{
    fwrite (&inst, sizeof(inst), 1, f) ;
}

void clearLineBuf(char* buf, int size)
{
    int i;

    for(i = 0; i < size; i++)
    {
        buf[i] = '\0';
    }
}

int isLineBlank(char* buf, int size)
{
    int i;
    int result;

    /* ******************************************* */
    /* while you see white space, keep going,      */
    /* if you see a newline return 1 else return 0 */
    /* ******************************************* */

    for(i = 0; i < size; i++)
    {
        switch(buf[i])
        {
            case ' ':
            case '\t':
            break;

            case '\n':
                result = 1;
            break;

            default:
                result = 0;
            break;
        }
    }
    
    return(result);
}

isWhiteSpace(char* buf, int size)
{
    int i;
    int result;

    /* ******************************************* */
    /* while you see white space, keep going,      */
    /* if you see a newline return 1 else return 0 */
    /* ******************************************* */

    for(i = 0; i < size; i++)
    {
        switch(buf[i])
        {
            case ' ':
            case '\t':
            break;

            case '\n':
				result=1;
            break;

            default:
                result=0;
            break;
        }
    }
    
    return(result);
}   
