1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * INTERFACE: Fortran Rappture Common Functions Source |
---|
4 | * |
---|
5 | * Fortran functions common to all interfaces. |
---|
6 | * |
---|
7 | * ====================================================================== |
---|
8 | * AUTHOR: Derrick Kearney, Purdue University |
---|
9 | * Copyright (c) 2004-2005 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | #include "RpFortranCommon.h" |
---|
16 | |
---|
17 | char* null_terminate(char* inStr, int len) { |
---|
18 | int retVal = 0; |
---|
19 | char* newStr = NULL; |
---|
20 | char* current = NULL; |
---|
21 | |
---|
22 | if (inStr && (len > 0) ) { |
---|
23 | |
---|
24 | current = inStr+len-1; |
---|
25 | |
---|
26 | while ((len > 0) && (isspace(*(current)))) { |
---|
27 | // dont strip off newlines |
---|
28 | |
---|
29 | if ( (*(current) == '\f') |
---|
30 | || (*(current) == '\n') |
---|
31 | || (*(current) == '\r') |
---|
32 | || (*(current) == '\t') |
---|
33 | || (*(current) == '\v') ) |
---|
34 | { |
---|
35 | break; |
---|
36 | } |
---|
37 | |
---|
38 | if (--len) { |
---|
39 | current--; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | newStr = (char*) calloc(len+1,(sizeof(char))); |
---|
44 | strncpy(newStr,inStr,len); |
---|
45 | *(newStr+len) = '\0'; |
---|
46 | |
---|
47 | retVal++; |
---|
48 | } |
---|
49 | |
---|
50 | return newStr; |
---|
51 | } |
---|
52 | |
---|
53 | std::string null_terminate_str(const char* inStr, int len) |
---|
54 | { |
---|
55 | int retVal = 0; |
---|
56 | std::string newStr = ""; |
---|
57 | const char* current = NULL; |
---|
58 | |
---|
59 | if (inStr) { |
---|
60 | |
---|
61 | current = inStr+len-1; |
---|
62 | |
---|
63 | while ((len > 0) && (isspace(*(current)))) { |
---|
64 | // dont strip off newlines |
---|
65 | |
---|
66 | if ( (*(current) == '\f') |
---|
67 | || (*(current) == '\n') |
---|
68 | || (*(current) == '\r') |
---|
69 | || (*(current) == '\t') |
---|
70 | || (*(current) == '\v') ) |
---|
71 | { |
---|
72 | break; |
---|
73 | } |
---|
74 | |
---|
75 | if (--len) { |
---|
76 | current--; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | newStr = std::string(inStr,len); |
---|
81 | |
---|
82 | retVal++; |
---|
83 | } |
---|
84 | |
---|
85 | // return retVal; |
---|
86 | |
---|
87 | return newStr; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void fortranify(const char* inBuff, char* retText, int retTextLen) { |
---|
92 | |
---|
93 | int inBuffLen = 0; |
---|
94 | int i = 0; |
---|
95 | |
---|
96 | if (inBuff && retText && (retTextLen > 0)) { |
---|
97 | inBuffLen = strlen(inBuff); |
---|
98 | |
---|
99 | strncpy(retText, inBuff, retTextLen); |
---|
100 | |
---|
101 | // fortran-ify the string |
---|
102 | if (inBuffLen < retTextLen) { |
---|
103 | for (i = inBuffLen; i < retTextLen; i++) { |
---|
104 | retText[i] = ' '; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|