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