Skip to content

Commit 967e44b

Browse files
authored
OMSString (#981)
1 parent 767ff79 commit 967e44b

File tree

7 files changed

+118
-32
lines changed

7 files changed

+118
-32
lines changed

src/OMSimulatorLib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ list(APPEND OMSIMULATORLIB_SOURCES MATWriter.cpp)
5353
list(APPEND OMSIMULATORLIB_SOURCES Model.cpp)
5454
list(APPEND OMSIMULATORLIB_SOURCES OMSFileSystem.cpp)
5555
list(APPEND OMSIMULATORLIB_SOURCES OMSimulator.cpp)
56+
list(APPEND OMSIMULATORLIB_SOURCES OMSString.cpp)
5657
list(APPEND OMSIMULATORLIB_SOURCES ResultReader.cpp)
5758
list(APPEND OMSIMULATORLIB_SOURCES ResultWriter.cpp)
5859
list(APPEND OMSIMULATORLIB_SOURCES Scope.cpp)

src/OMSimulatorLib/ComRef.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@
3030
*/
3131

3232
#include "ComRef.h"
33+
34+
#include "OMSString.h"
35+
3336
#include <assert.h>
3437
#include <RegEx.h>
3538

3639
const oms_regex re_ident("^[a-zA-Z][a-zA-Z0-9_]*$");
3740

3841
oms::ComRef::ComRef()
3942
{
40-
cref = new char[1];
41-
cref[0] = '\0';
43+
cref = allocateAndCopyString(NULL);
4244
}
4345

4446
oms::ComRef::ComRef(const std::string& path)
4547
{
46-
cref = new char[path.size() + 1];
47-
strcpy(cref, path.c_str());
48+
cref = allocateAndCopyString(path);
4849
}
4950

5051
oms::ComRef::ComRef(const char* path)
5152
{
5253
assert(path);
53-
cref = new char[strlen(path) + 1];
54-
strcpy(cref, path);
54+
cref = allocateAndCopyString(path);
5555
}
5656

5757
oms::ComRef::~ComRef()
@@ -61,8 +61,7 @@ oms::ComRef::~ComRef()
6161

6262
oms::ComRef::ComRef(const oms::ComRef& copy)
6363
{
64-
cref = new char[strlen(copy.cref) + 1];
65-
strcpy(cref, copy.cref);
64+
cref = allocateAndCopyString(copy.cref);
6665
}
6766

6867
oms::ComRef& oms::ComRef::operator=(const oms::ComRef& copy)
@@ -72,8 +71,7 @@ oms::ComRef& oms::ComRef::operator=(const oms::ComRef& copy)
7271
return *this;
7372

7473
delete[] cref;
75-
cref = new char[strlen(copy.cref) + 1];
76-
strcpy(cref, copy.cref);
74+
cref = allocateAndCopyString(copy.cref);
7775

7876
return *this;
7977
}
@@ -204,6 +202,11 @@ oms::ComRef oms::ComRef::pop_front()
204202
return front;
205203
}
206204

205+
size_t oms::ComRef::size() const
206+
{
207+
return strlen(cref);
208+
}
209+
207210
bool oms::operator==(const oms::ComRef& lhs, const oms::ComRef& rhs)
208211
{
209212
return (0 == strcmp(lhs.c_str(), rhs.c_str()));

src/OMSimulatorLib/ComRef.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#define _OMS_COM_REF_H_
3434

3535
#include <string>
36-
#include <cstring>
3736

3837
namespace oms
3938
{
@@ -74,7 +73,7 @@ namespace oms
7473
bool hasSuffix(const std::string& suffix) const; ///< returns true if the cref has a suffix that matches the argument
7574

7675
const char* c_str() const {return cref;}
77-
size_t size() {return strlen(cref);}
76+
size_t size() const;
7877
operator std::string() const {return std::string(cref);}
7978

8079
private:

src/OMSimulatorLib/FMUInfo.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#include "FMUInfo.h"
3333

3434
#include "Logging.h"
35+
#include "OMSString.h"
3536

36-
#include <cstring>
3737

3838
oms::FMUInfo::FMUInfo(const std::string& path)
3939
{
@@ -47,7 +47,7 @@ oms::FMUInfo::FMUInfo(const std::string& path)
4747
this->guid = nullptr;
4848
this->license = nullptr;
4949
this->modelName = nullptr;
50-
this->path = new char[path.size()+1]; strcpy(this->path, path.c_str());
50+
this->path = allocateAndCopyString(path);
5151
this->version = nullptr;
5252
this->canBeInstantiatedOnlyOncePerProcess = false;
5353
this->canGetAndSetFMUstate = false;
@@ -75,24 +75,6 @@ oms::FMUInfo::~FMUInfo()
7575
if (this->version) delete[] this->version;
7676
}
7777

78-
char* allocateAndCopyString(const char* source)
79-
{
80-
char* target;
81-
82-
if (source)
83-
{
84-
target = new char[strlen(source) + 1];
85-
strcpy(target, source);
86-
}
87-
else
88-
{
89-
target = new char[1];
90-
target[0] = '\0';
91-
}
92-
93-
return target;
94-
}
95-
9678
void oms::FMUInfo::update(fmi_version_enu_t version, fmi2_import_t* fmu)
9779
{
9880
fmi2_fmu_kind_enu_t fmuKind = fmi2_import_get_fmu_kind(fmu);

src/OMSimulatorLib/OMSFileSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#ifndef _OMS_FILESYSTEM_H_
2+
#define _OMS_FILESYSTEM_H_
23

34
#if !defined(WITHOUT_FS) && defined(__has_include)
45

src/OMSimulatorLib/OMSString.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
#include "OMSString.h"
33+
34+
#include <cstring>
35+
36+
char* oms::allocateAndCopyString(const char* source)
37+
{
38+
char* target;
39+
40+
if (source)
41+
{
42+
target = new char[strlen(source) + 1];
43+
strcpy(target, source);
44+
}
45+
else
46+
{
47+
target = new char[1];
48+
target[0] = '\0';
49+
}
50+
51+
return target;
52+
}
53+
54+
char* oms::allocateAndCopyString(const std::string& source)
55+
{
56+
return allocateAndCopyString(source.c_str());
57+
}

src/OMSimulatorLib/OMSString.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
#ifndef _OMS_STRING_H_
33+
#define _OMS_STRING_H_
34+
35+
#include <string>
36+
37+
namespace oms
38+
{
39+
char* allocateAndCopyString(const char* source);
40+
char* allocateAndCopyString(const std::string& source);
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)