uchar.c

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Kevin McBride                                   *
00003  *   kevin@planetsaphire.com                                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #define     LIBTEKLTI_EXPORT  (0)
00022 
00023 #include "teklti.h"
00024 
00025 #ifndef     USE_386_ASM
00026 
00027 #include <string.h>
00028 #include <signal.h>
00029 
00030 #include <stdio.h>
00031 
00032 
00033 #ifdef      DEBUG_USE_FERROR
00034 static const char uchardup_srcnull[] = "POTENTIAL SEGFAULT in uchardup: src is NULL!\n";
00035 static const char char2uchar_srcnull[] = "POTENTIAL SEGFAULT in char2uchar: src is NULL!\n";
00036 static const char ucharempty_uchrtofreenull[] = "POTENTIAL SEGFAULT in ucharfree: uchrtofree is NULL!\n";
00037 #endif      /* DEBUG_USE_FERROR */
00038 
00039 
00040 TEKLTI_DECLSPEC uchar_t * uchardup ( const uchar_t * src )
00041 {
00042       /* Set up our retval pointer. */
00043       uchar_t * retval;
00044 
00045 #ifndef     NO_SIZE_T_CHECKS
00046       if ( src == NULL )
00047       {
00048 #ifdef      DEBUG_USE_FERROR
00049             fputs(char2uchar_srcnull, stderr);
00050 #endif      /* DEBUG_USE_FERROR */
00051             return NULL;
00052       }
00053 #endif      /* not NO_SIZE_T_CHECKS */
00054 
00055       retval = (uchar_t *)malloc(sizeof(uchar_t));
00056       if ( retval == NULL )
00057             return NULL;
00058 
00059       retval->uchar_t_asciilen = src->uchar_t_asciilen;
00060 
00061       retval->uchar_t_ascii = (char *)malloc(
00062             sizeof(char) * retval->uchar_t_asciilen
00063       );
00064       if ( retval->uchar_t_ascii == NULL )
00065       {
00066             free(retval);
00067 
00068             return NULL;
00069       }
00070 
00071       (void)strncpy(
00072             retval->uchar_t_ascii,
00073             src->uchar_t_ascii,
00074             retval->uchar_t_asciilen
00075       );
00076 
00077       return retval;
00078 }
00079 
00080 
00081 TEKLTI_DECLSPEC uchar_t * char2ucharlen ( const char * src, size_t len )
00082 {
00083       uchar_t * retval;
00084 
00085 #ifndef     NO_SIZE_T_CHECKS
00086       if ( src == NULL )
00087       {
00088 #ifdef      DEBUG_USE_FERROR
00089             fputs(char2uchar_srcnull, stderr);
00090 #endif      /* DEBUG_USE_FERROR */
00091             return NULL;
00092       }
00093 #endif      /* not NO_SIZE_T_CHECKS */
00094 
00095       retval = (uchar_t *)malloc(sizeof(uchar_t));
00096       if ( retval == NULL )
00097             return NULL;
00098 
00099       retval->uchar_t_asciilen = len + 1;
00100       
00101       retval->uchar_t_ascii = (char *)malloc(
00102             sizeof(char) * retval->uchar_t_asciilen
00103       );
00104       if ( retval->uchar_t_ascii == NULL )
00105       {
00106             free(retval);
00107 
00108             return NULL;
00109       }
00110 
00111       (void)strncpy(retval->uchar_t_ascii, src, retval->uchar_t_asciilen);
00112 
00113       return retval;
00114 }
00115 
00116 
00117 TEKLTI_DECLSPEC uchar_t * char2uchar ( const char * src )
00118 {
00119       /* Set up our retval pointer. */
00120       uchar_t * retval;
00121 
00122 #ifndef     NO_SIZE_T_CHECKS
00123       if ( src == NULL )
00124       {
00125 #ifdef      DEBUG_USE_FERROR
00126             fputs(char2uchar_srcnull, stderr);
00127 #endif      /* DEBUG_USE_FERROR */
00128             return NULL;
00129       }
00130 #endif      /* not NO_SIZE_T_CHECKS */
00131 
00132 
00133       retval = (uchar_t *)malloc(sizeof(uchar_t));
00134       if ( retval == NULL )
00135             return NULL;
00136 
00137       retval->uchar_t_asciilen = strlen(src) + 1;
00138       
00139       retval->uchar_t_ascii = (char *)malloc(
00140             sizeof(char) * retval->uchar_t_asciilen
00141       );
00142       if ( retval->uchar_t_ascii == NULL )
00143       {
00144             free(retval);
00145 
00146             return NULL;
00147       }
00148 
00149       (void)strncpy(retval->uchar_t_ascii, src, retval->uchar_t_asciilen);
00150 
00151       return retval;
00152 }
00153 
00154 
00155 TEKLTI_DECLSPEC void ucharempty ( uchar_t * uchrtofree )
00156 {
00157       /* Perform all pointer checks as necessary. */
00158 #ifndef     NO_SIZE_T_CHECKS
00159       if ( uchrtofree == NULL )
00160       {
00161 #ifdef      DEBUG_USE_FERROR
00162             fputs(ucharempty_uchrtofreenull, stderr);
00163 #endif      /* DEBUG_USE_FERROR */
00164             raise(1);
00165             return;
00166       }
00167 #endif      /* not NO_SIZE_T_CHECKS */
00168 
00169       /* Free the contained string first, and enforce privacy as necessary. */
00170 #ifdef      TEKLTI_ENFORCE_PRIVACY
00171       free(
00172             memset(
00173                   uchrtofree->uchar_t_ascii,
00174                   '\0',
00175                   sizeof(char) * uchrtofree->uchar_t_asciilen
00176             )
00177       );
00178 #else /* not USE_386_ASM_ENFORCE_PRIVACY */
00179       free(uchrtofree->uchar_t_ascii);
00180 #endif      /* not USE_386_ASM_ENFORCE_PRIVACY */
00181 
00182       free(uchrtofree);
00183 }
00184 
00185 #endif      /* not USE_386_ASM */
00186 
00187 
SourceForge.net Logo  Technical Library Template Interface Project Page