util/strdup.hh
changeset 351 e7d236750709
equal deleted inserted replaced
350:2c3c4dc34979 351:e7d236750709
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2011  Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 
       
    25 /*******************************************/
       
    26 /* String Duplication Utility Functions... */
       
    27 /*******************************************/
       
    28 
       
    29 
       
    30 
       
    31 #ifndef STRDUP__H
       
    32 #define STRDUP__H
       
    33 
       
    34 #include <stdlib.h>   /* required for malloc() */
       
    35 #include <string.h>   /* required for strcat() & strlen() */
       
    36 
       
    37 /*
       
    38  * Join two strings together. Allocate space with malloc(3).
       
    39  */
       
    40 static char *strdup2(const char *a, const char *b) {
       
    41   char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
       
    42 
       
    43   if (res == NULL) return NULL;
       
    44   return strcat(strcpy(res, a), b);
       
    45 }
       
    46 
       
    47 /*
       
    48  * Join three strings together. Allocate space with malloc(3).
       
    49  */
       
    50 static char *strdup3(const char *a, const char *b, const char *c) {
       
    51   char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
       
    52 
       
    53   if (res == NULL) return NULL;
       
    54   return strcat(strcat(strcpy(res, a), b), c);
       
    55 }
       
    56 
       
    57 
       
    58 #endif /* STRDUP__H */