~ubuntu-branches/ubuntu/utopic/adios/utopic

« back to all changes in this revision

Viewing changes to src/futils.c

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2013-12-09 15:21:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20131209152131-jtd4fpmdv3xnunnm
Tags: 1.5.0-1
* New upstream.
* Standards-Version: 3.9.5
* Include latest config.{sub,guess} 
* New watch file.
* Create libadios-bin for binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * ADIOS is freely available under the terms of the BSD license described
3
 
 * in the COPYING file in the top level directory of this source distribution.
4
 
 *
5
 
 * Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.
6
 
 */
7
 
 
8
 
#include <stdlib.h>
9
 
#include <string.h>
10
 
#include "futils.h"
11
 
#include "bp_utils.h" // error()
12
 
#include "adios_error.h" // err_no_memory
13
 
 
14
 
#ifdef DMALLOC
15
 
#include "dmalloc.h"
16
 
#endif
17
 
 
18
 
static int called_from_fortran = 0; // set to 1 when called from Fortran API
19
 
void futils_called_from_fortran_set(void) {called_from_fortran = 1;}
20
 
void futils_called_from_fortran_unset(void) {called_from_fortran = 0;}
21
 
int futils_is_called_from_fortran(void) {return called_from_fortran;}
22
 
 
23
 
/** Copy a C string into a Fortran CHARACTER array */
24
 
void futils_cstr_to_fstr(const char *cs, char *fs, int flen) 
25
 
{
26
 
    int clen = strlen(cs);
27
 
    if (clen > flen)
28
 
        clen = flen;
29
 
    strncpy(fs, cs, clen);           /* does not copy the '\0' */
30
 
    memset(fs+clen, ' ', flen-clen); /* right pad with spaces the CHARACTER array */
31
 
}
32
 
 
33
 
/** Trim a Fortran string and allocate a C string and copy content to it and add '\0' 
34
 
 *  Need to free() the string later.
35
 
 */
36
 
char * futils_fstr_to_cstr(const char * fs, int flen)
37
 
{
38
 
    char *cs;
39
 
    int clen = flen;
40
 
    while (clen > 0 && fs[clen-1] == ' ')
41
 
        clen--;
42
 
    cs = (char*) malloc ((size_t) (clen + 1));
43
 
    if (cs == NULL) {
44
 
        adios_error (err_no_memory, "ERROR: Cannot allocate %d bytes for a C string in ADIOS API", clen+1);
45
 
        return NULL;
46
 
    }
47
 
    strncpy (cs, fs, clen);
48
 
    cs[clen] = '\0';
49
 
    return cs;
50
 
}
51