summaryrefslogtreecommitdiffstats
path: root/fortran/src/H5f90kit.c
blob: 6e8c79322a9e12d21bd2f44fa074a44562d66bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****h* H5f90kit/H5f90kit
 * PURPOSE
 *  Routines from HDF4 to deal with C-FORTRAN issues:
 *
 *   HD5f2cstring   -- convert a Fortran string to a C string
 *   HD5packFstring -- convert a C string into a Fortran string
 *
 * COPYRIGHT
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 ******
*/

#include <ctype.h>
#include <stddef.h>
#include "H5f90.h"

/****if* H5f90kit/HDf2cstring
 * NAME
 *   HD5f2cstring -- convert a Fortran string to a C string
 *  char * HDf2cstring(fdesc, len)
 * INPUTS
 *   _fcd  fdesc;    IN: Fortran string descriptor
 *  int  len;       IN: length of Fortran string
 * RETURNS
 *  Pointer to the C string if success, else NULL
 * PURPOSE
 *  Chop off trailing blanks off of a Fortran string and
 *  move it into a newly allocated C string.  It is up
 *  to the user to free this string.
 * SOURCE
 */
char *
HD5f2cstring(_fcd fdesc, size_t len)
/******/
{
    char       *cstr;   /* C string to return */
    char       *str;    /* Pointer to FORTRAN string */
    int         i;      /* Local index variable */

    /* Search for the end of the string */
    str = _fcdtocp(fdesc);
    for(i = (int)len - 1; i >= 0 && HDisspace((int)str[i]) && str[i] == ' '; i--)
        /*EMPTY*/;

    /* Allocate C string */
    if(NULL == (cstr = (char *)HDmalloc((size_t)(i + 2))))
        return NULL;

    /* Copy text from FORTRAN to C string */
    HDmemcpy(cstr, str, (size_t)(i + 1));

    /* Terminate C string */
    cstr[i + 1] = '\0';

    return cstr;
}   /* HD5f2cstring */

/****if* H5f90kit/HD5packFstring
 * NAME
 *   HD5packFstring -- convert a C string into a Fortran string
 *  int HD5packFstring(src, dest, len)
 * INPUTS
 *  char * src;         IN:  source string
 *  int   len;          IN:  length of string
 * OUTPUTS
 *  char * dest;       OUT: destination
 * RETURNS
 *   SUCCEED / FAIL
 * PURPOSE
 *  given a NULL terminated C string 'src' convert it to
 *  a space padded Fortran string 'dest' of length 'len'
 *
 *  This is very similar to HDc2fstr except that function does
 *  it in place and this one copies.  We should probably only
 *  support one of these.
 * SOURCE
 */
void
HD5packFstring(char *src, char *dest, size_t dst_len)
/******/
{
    size_t src_len=HDstrlen(src);

    /* Copy over the string information, up to the length of the src */
    /* (Don't copy the NUL terminator from the C string to the FORTRAN string */
    HDmemcpy(dest,src,MIN(src_len,dst_len));

    /* Pad out any remaining space in the FORTRAN string with ' 's */
    if(src_len<dst_len)
        HDmemset(&dest[src_len],' ',dst_len-src_len);

}   /* HD5packFstring */