Skip to content

Commit

Permalink
Added what should be a version of asprintf that works in visual studio
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15079 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 7, 2013
1 parent a89bed2 commit 99fa25e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 1 addition & 5 deletions SimulationRuntime/c/simulation/simulation_info_xml.c
Expand Up @@ -29,6 +29,7 @@
*/

#include "simulation_info_xml.h"
#include "omc_msvc.h" /* for asprintf */
#include <expat.h>
#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -81,12 +82,7 @@ static void XMLCALL endElement(void *userData, const char *name) {
return;
}
if (0==strcmp("nonlinear",name)) {
#if defined(__MINGW32__) || defined(_MSC_VER)
xml->equationInfo[curIndex].name = malloc(snprintf(NULL, 0, "Nonlinear function (residualFunc%d, size %d)", curIndex, xml->equationInfo[curIndex].numVar) + 1);
sprintf(&xml->equationInfo[curIndex].name, "Nonlinear function (residualFunc%d, size %d)", curIndex, xml->equationInfo[curIndex].numVar);
#else
asprintf(&xml->equationInfo[curIndex].name, "Nonlinear function (residualFunc%d, size %d)", curIndex, xml->equationInfo[curIndex].numVar);
#endif
xml->equationInfo[curIndex].profileBlockIndex = curProfileIndex;
((void**)userData)[2] = (void*) (curProfileIndex+1);
return;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@
SET(util_sources base_array.c boolean_array.c omc_error.c division.c index_spec.c
integer_array.c java_interface.c list.c memory_pool.c modelica_string.c
read_write.c read_matlab4.c real_array.c ringbuffer.c
rtclock.c string_array.c utility.c varinfo.c)
rtclock.c string_array.c utility.c varinfo.c omc_msvc.c)


SET(util_headers base_array.h boolean_array.h division.h omc_error.h index_spec.h integer_array.h
Expand Down
47 changes: 47 additions & 0 deletions SimulationRuntime/c/util/omc_msvc.c
@@ -0,0 +1,47 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include "omc_msvc.h"
#include <stdio.h>

#if defined(_MSC_VER)
int asprintf(char **strp, const char *fmt, ...) {
int len;
va_list ap;
va_start(ap, fmt);

len = vsnprintf(NULL, 0, fmt, ap);
*strp = malloc(len+1);
len = snprintf(strp, len+1, fmt, ap);

va_end(args);
return len;
}
#endif
Expand Up @@ -65,6 +65,8 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}};
#define snprintf sprintf_s
#endif

int asprintf(char **strp, const char *fmt, ...);

#endif

#endif

0 comments on commit 99fa25e

Please sign in to comment.