|
| 1 | +// Methods for Functional Mock-up Unit Export of EnergyPlus. |
| 2 | + |
| 3 | +/////////////////////////////////////////////////////// |
| 4 | +/// \file util.c |
| 5 | +/// |
| 6 | +/// \brief utility functions |
| 7 | +/// |
| 8 | +/// \author Wangda Zuo, Thierry S. Nouidui, Michael Wetter |
| 9 | +/// Simulation Research Group, |
| 10 | +/// LBNL, |
| 11 | +/// WZuo@lbl.gov |
| 12 | +/// |
| 13 | +/// \date 2011-11-02 |
| 14 | +/// |
| 15 | +/// |
| 16 | +/// This file provides utility functions for fmus |
| 17 | +/// |
| 18 | +/// Copyright (c) 2012, The Regents of the University of California, |
| 19 | +/// through Lawrence Berkeley National Laboratory |
| 20 | +/// subject to receipt of any required approvals from the U.S. Dept. of Energy. |
| 21 | +/// All rights reserved. |
| 22 | +/////////////////////////////////////////////////////// |
| 23 | + |
| 24 | +#include <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | +#include <sys/stat.h> |
| 28 | +#include "util.h" |
| 29 | + |
| 30 | +int debug; // Control for debug information |
| 31 | + |
| 32 | +/////////////////////////////////////////////////////////////////////////////// |
| 33 | +/// Translate the double variable to string variable. |
| 34 | +/// |
| 35 | +///\param buffer String variable. |
| 36 | +///\param r Double variable. |
| 37 | +/////////////////////////////////////////////////////////////////////////////// |
| 38 | +void doubleToCommaString(char* buffer, double r){ |
| 39 | + char* comma; |
| 40 | + sprintf(buffer, "%.16g", r); |
| 41 | + comma = strchr(buffer, '.'); |
| 42 | + if (comma) *comma = ','; |
| 43 | +} |
| 44 | + |
| 45 | +////////////////////////////////////////////////////////////////////////// |
| 46 | +/// Delete the temporary folder |
| 47 | +/// |
| 48 | +///\param tmpPat The path of the temporary folder |
| 49 | +///\return 0 if no error occurred |
| 50 | +///////////////////////////////////////////////////////////////////////// |
| 51 | +int deleteTmpDir(char* tmpPat){ |
| 52 | + char* cmd; |
| 53 | + struct stat st; |
| 54 | + |
| 55 | + // Ceck if the folder present |
| 56 | + if(stat(tmpPat,&st) != 0){ |
| 57 | + printfError("Folder \"%s\" is not existing.\n", tmpPat); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + cmd = (char *)calloc(sizeof(char), strlen(tmpPat) +18); |
| 62 | + if (cmd == NULL){ |
| 63 | + printfError("Fail to allocate memory for cmd.\n", tmpPat); |
| 64 | + return -1; |
| 65 | + } |
| 66 | + |
| 67 | + if (WINDOWS) |
| 68 | + sprintf(cmd, "rd %s /S/Q", tmpPat); // Command in windows |
| 69 | + else |
| 70 | + sprintf(cmd, "rm -r %s", tmpPat); // Command in Linux |
| 71 | + |
| 72 | + |
| 73 | + printf("Generated cmd: \"%s\".\n", cmd); |
| 74 | + if ( system(cmd) != 0 ){ |
| 75 | + printError("Fail to delete the temporary files"); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + printDebug("Deleted temporary files"); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +////////////////////////////////////////////////////////////////////////////// |
| 83 | +/// Get temporary path |
| 84 | +/// |
| 85 | +///\param nam Name to be used for temporary path |
| 86 | +///\param length Number of characters to be copied from \c nam. |
| 87 | +///\return Point of tmpPat if there is no error occurred. |
| 88 | +///////////////////////////////////////////////////////////////////////////// |
| 89 | +char *getTmpPath(const char *nam, int length) |
| 90 | +{ |
| 91 | + char *tmpPat; |
| 92 | + |
| 93 | + tmpPat = (char *)calloc(sizeof(char), length+2); |
| 94 | + |
| 95 | + // Define the temporary folder |
| 96 | + if(strncpy(tmpPat, nam, length) == NULL){ |
| 97 | + printError("Fail to allocate memory for temp dir.\n"); |
| 98 | + return NULL; |
| 99 | + } |
| 100 | + if(WINDOWS) strcat(tmpPat, "\\"); |
| 101 | + else strcat(tmpPat, "/"); |
| 102 | + |
| 103 | + return tmpPat; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +////////////////////////////////////////////////////////////////////////////// |
| 109 | +/// Set the mode in debug so that the debug information will be printed |
| 110 | +/// |
| 111 | +////////////////////////////////////////////////////////////////////////////// |
| 112 | +void setDebug(int value) |
| 113 | +{ |
| 114 | + debug = 1; |
| 115 | +} |
| 116 | + |
| 117 | +////////////////////////////////////////////////////////////////////////////// |
| 118 | +/// Print debug message |
| 119 | +/// |
| 120 | +///\param msg Message to be printed for debugging |
| 121 | +////////////////////////////////////////////////////////////////////////////// |
| 122 | +void printDebug(const char* msg){ |
| 123 | + if (debug == 1) |
| 124 | + { |
| 125 | + fprintf(stdout, "Debug: "); |
| 126 | + fprintf(stdout, "%s\n", msg); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +//////////////////////////////////////////////////////////////////////////////// |
| 131 | +///// Print formatted debug message |
| 132 | +///// |
| 133 | +/////\param str1 Message to be printed for debugging |
| 134 | +/////\param str2 String variable to be printed for debugging |
| 135 | +//////////////////////////////////////////////////////////////////////////////// |
| 136 | +//void printfDebug(const char* str1, const char* str2){ |
| 137 | +// if (debug == 1) |
| 138 | +// { |
| 139 | +// fprintf(stdout, "Debug: "); |
| 140 | +// fprintf(stdout, str1, str2); |
| 141 | +// } |
| 142 | +//} |
| 143 | + |
| 144 | +////////////////////////////////////////////////////////////////////////////// |
| 145 | +/// Print formatted debug message with Integer |
| 146 | +/// |
| 147 | +///\param str1 Message to be printed for debugging |
| 148 | +///\param integer Integer variable to be printed for debugging |
| 149 | +////////////////////////////////////////////////////////////////////////////// |
| 150 | +void printfIntDebug(const char* str1, const int integer){ |
| 151 | + if (debug == 1) |
| 152 | + { |
| 153 | + fprintf(stdout, "Debug: "); |
| 154 | + fprintf(stdout, str1, integer); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +////////////////////////////////////////////////////////////////////////////// |
| 159 | +/// Print error message |
| 160 | +/// |
| 161 | +///\param msg Error message to be printed |
| 162 | +////////////////////////////////////////////////////////////////////////////// |
| 163 | +void printError(const char* msg){ |
| 164 | + fprintf(stderr, "*** Error: "); |
| 165 | + fprintf(stderr, "%s\n", msg); |
| 166 | +} |
| 167 | + |
| 168 | +////////////////////////////////////////////////////////////////////////////// |
| 169 | +/// Print formatted error message |
| 170 | +/// |
| 171 | +///\param str1 Error message to be printed |
| 172 | +///\param str2 String variable to be printed |
| 173 | +////////////////////////////////////////////////////////////////////////////// |
| 174 | +void printfError(const char* str1, const char* str2){ |
| 175 | + fprintf(stderr, "*** Error: "); |
| 176 | + fprintf(stderr, str1, str2); |
| 177 | +} |
| 178 | + |
| 179 | +/* |
| 180 | +
|
| 181 | +*********************************************************************************** |
| 182 | +Copyright Notice |
| 183 | +---------------- |
| 184 | +
|
| 185 | +Functional Mock-up Unit Export of EnergyPlus ©2013, The Regents of |
| 186 | +the University of California, through Lawrence Berkeley National |
| 187 | +Laboratory (subject to receipt of any required approvals from |
| 188 | +the U.S. Department of Energy). All rights reserved. |
| 189 | + |
| 190 | +If you have questions about your rights to use or distribute this software, |
| 191 | +please contact Berkeley Lab's Technology Transfer Department at |
| 192 | +TTD@lbl.gov.referring to "Functional Mock-up Unit Export |
| 193 | +of EnergyPlus (LBNL Ref 2013-088)". |
| 194 | + |
| 195 | +NOTICE: This software was produced by The Regents of the |
| 196 | +University of California under Contract No. DE-AC02-05CH11231 |
| 197 | +with the Department of Energy. |
| 198 | +For 5 years from November 1, 2012, the Government is granted for itself |
| 199 | +and others acting on its behalf a nonexclusive, paid-up, irrevocable |
| 200 | +worldwide license in this data to reproduce, prepare derivative works, |
| 201 | +and perform publicly and display publicly, by or on behalf of the Government. |
| 202 | +There is provision for the possible extension of the term of this license. |
| 203 | +Subsequent to that period or any extension granted, the Government is granted |
| 204 | +for itself and others acting on its behalf a nonexclusive, paid-up, irrevocable |
| 205 | +worldwide license in this data to reproduce, prepare derivative works, |
| 206 | +distribute copies to the public, perform publicly and display publicly, |
| 207 | +and to permit others to do so. The specific term of the license can be identified |
| 208 | +by inquiry made to Lawrence Berkeley National Laboratory or DOE. Neither |
| 209 | +the United States nor the United States Department of Energy, nor any of their employees, |
| 210 | +makes any warranty, express or implied, or assumes any legal liability or responsibility |
| 211 | +for the accuracy, completeness, or usefulness of any data, apparatus, product, |
| 212 | +or process disclosed, or represents that its use would not infringe privately owned rights. |
| 213 | + |
| 214 | + |
| 215 | +Copyright (c) 2013, The Regents of the University of California, Department |
| 216 | +of Energy contract-operators of the Lawrence Berkeley National Laboratory. |
| 217 | +All rights reserved. |
| 218 | + |
| 219 | +1. Redistribution and use in source and binary forms, with or without modification, |
| 220 | +are permitted provided that the following conditions are met: |
| 221 | + |
| 222 | +(1) Redistributions of source code must retain the copyright notice, this list |
| 223 | +of conditions and the following disclaimer. |
| 224 | + |
| 225 | +(2) Redistributions in binary form must reproduce the copyright notice, this list |
| 226 | +of conditions and the following disclaimer in the documentation and/or other |
| 227 | +materials provided with the distribution. |
| 228 | + |
| 229 | +(3) Neither the name of the University of California, Lawrence Berkeley |
| 230 | +National Laboratory, U.S. Dept. of Energy nor the names of its contributors |
| 231 | +may be used to endorse or promote products derived from this software without |
| 232 | +specific prior written permission. |
| 233 | + |
| 234 | +2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 235 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 236 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 237 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 238 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 239 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 240 | +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 241 | +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 242 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 243 | +POSSIBILITY OF SUCH DAMAGE. |
| 244 | + |
| 245 | +3. You are under no obligation whatsoever to provide any bug fixes, patches, |
| 246 | +or upgrades to the features, functionality or performance of the source code |
| 247 | +("Enhancements") to anyone; however, if you choose to make your Enhancements |
| 248 | +available either publicly, or directly to Lawrence Berkeley National Laboratory, |
| 249 | +without imposing a separate written license agreement for such Enhancements, |
| 250 | +then you hereby grant the following license: a non-exclusive, royalty-free |
| 251 | +perpetual license to install, use, modify, prepare derivative works, incorporate |
| 252 | +into other computer software, distribute, and sublicense such enhancements or |
| 253 | +derivative works thereof, in binary and source code form. |
| 254 | + |
| 255 | +NOTE: This license corresponds to the "revised BSD" or "3-clause BSD" |
| 256 | +License and includes the following modification: Paragraph 3. has been added. |
| 257 | +
|
| 258 | +
|
| 259 | +*********************************************************************************** |
| 260 | +*/ |
0 commit comments