
/** @file  psString.c
 *
 *  @brief Contains the definition of string utility functions
 *
 *  String utility functions defined shall provide basic string copying
 *  capabilities while using the preferred memory management utilities.
 *
 *  @author Eric Van Alst, MHPCC
 *
 *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2004-11-04 01:05:00 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 */

#include <stdlib.h>
#include <string.h>
#include "psString.h"
#include "psMemory.h"
#include "psError.h"

#include "psSysUtilsErrors.h"

char *psStringCopy(const char *str)
{
    // Allocate memory using psAlloc function
    // Copy input string to memory just allocated
    // Return the copy
    return strcpy(psAlloc(strlen(str) + 1), str);
}

char *psStringNCopy(const char *str, psS32 nChar)
{
    char *returnValue = NULL;

    // Check the number of characters to copy is non-negative
    if (nChar < 0) {
        // Log error message and return NULL
        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                PS_ERRORTEXT_psString_NCHAR_NEGATIVE,
                nChar);
        return NULL;
    }
    // Allocate memory using psAlloc function - nChar bytes
    // Copy input string to memory allocated up to nChar characters
    // Return the copy
    returnValue = strncpy(psAlloc((size_t) nChar + 1), str, (size_t) nChar);

    // Ensure the last byte is NULL character
    if (nChar > 0) {
        returnValue[nChar] = '\0';
    }
    // Return the string pointer
    return returnValue;
}
