GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

GATUtil.c

Go to the documentation of this file.
00001 /** @file GATUtil.c
00002  *  Source file for the different utility functions used throughout the GAT 
00003  *  engine.
00004  *
00005  *  @date Mon Oct 20 2003
00006  *
00007  *  @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATUtil.c,v 1.6 2004/04/05 11:49:44 hartmutkaiser Exp $
00008  *
00009  *  Copyright (C) Hartmut Kaiser
00010  *  This file is part of the GAT Engine.
00011  *  Contributed by Hartmut Kaiser <hartmutkaiser [at] t-online [dot] de>.
00012  *
00013  *  Use, modification and distribution is subject to the Gridlab Software
00014  *  License. (See accompanying file GLlicense.txt or copy at
00015  *  http://www.gridlab.org/GLlicense.txt)
00016  */
00017  
00018 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATUtil.c,v 1.6 2004/04/05 11:49:44 hartmutkaiser Exp $";
00019 
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 #include "GATUtil.h"
00024 #include "GATErrors.h"
00025 
00026 /** char *GATUtil_strdup(char const *str)
00027  *  @brief Make a copy of a given string 
00028  *
00029  *  The function GATUtil_strdup allocates a new copy of the given string. The 
00030  *  return value should be free'd by the caller by means of the function 
00031  *  free().
00032  *  Since strdup isn't POSIX, we need to provide our own.
00033  *
00034  *  @param str The string to be copied.
00035  *
00036  *  @return Returns the allocated string copy.
00037  */
00038 char *
00039 GATUtil_strdup(char const *str)
00040 {
00041   size_t length = strlen(str) + 1;
00042   char *msg = (char *) malloc(length);
00043   if (NULL != msg)
00044   {
00045     strcpy(msg, str);
00046   }
00047   return msg;
00048 }
00049 
00050 /** void *GATUtil_memdup(void const *str, GATuint32 size)
00051  *  @brief Make a copy of a given memory buffer 
00052  *
00053  *  The function GATUtil_memdup allocates a new copy of the given memory 
00054  *  buffer. The return value should be free'd by the caller by means of the 
00055  *  function free().
00056  *
00057  *  @param buffer The memory buffer to be copied.
00058  *
00059  *  @return Returns the allocated memory buffer copy.
00060  */
00061 void *
00062 GATUtil_memdup(void const *buffer, GATuint32 size)
00063 {
00064   void *new_buffer = malloc(size);
00065   if (NULL != new_buffer)
00066   {
00067     memcpy(new_buffer, buffer, size);
00068   }
00069   return new_buffer;
00070 }
00071 
00072 /** GATUtil_appendstring
00073  *  @brief Append a string to a given string.
00074  *
00075  *  The function GATUtil_appendstring appends a string to a given string 
00076  *  assuming, that the given string was allocated by malloc.
00077  *
00078  *  @param str The pointer to the string pointer, which contains the string
00079  *        to which the to_append string should be appended. May be zero, in 
00080  *        wich case a new buffer is allocated and returned.
00081  *  @param to_append The string to append
00082  *
00083  *  @return An error code.
00084  */
00085 GATResult
00086 GATUtil_appendstring(char **str, char const *to_append)
00087 {
00088   GATBool new_string = (NULL != *str) ? GATFalse : GATTrue;
00089   unsigned int len = (NULL != *str) ? strlen(*str) + 1 : 0;
00090   char *new_str = (char *)realloc(*str, len + strlen(to_append) + 1);
00091   if (NULL == new_str)
00092   {
00093     return GAT_MEMORYFAILURE;
00094   }
00095   if (GATTrue == new_string)
00096   {
00097     new_str[0] = '\0';
00098   }
00099   strcat(new_str, to_append);
00100   *str = new_str;
00101   return GAT_SUCCESS;
00102 }
00103