correctly process strings with DOS-style CR-LF in lammps_commands_string()
This commit is contained in:
@ -495,11 +495,15 @@ void lammps_commands_string(void *handle, const char *str)
|
||||
{
|
||||
LAMMPS *lmp = (LAMMPS *) handle;
|
||||
|
||||
// make copy of str so can strtok() it
|
||||
// copy str and convert from CR-LF (DOS-style) to LF (Unix style) line
|
||||
int n = strlen(str);
|
||||
char *ptr, *copy = new char[n+1];
|
||||
|
||||
int n = strlen(str) + 1;
|
||||
char *copy = new char[n];
|
||||
strcpy(copy,str);
|
||||
for (ptr = copy; *str != '\0'; ++str) {
|
||||
if ((str[0] == '\r') && (str[1] == '\n')) continue;
|
||||
*ptr++ = *str;
|
||||
}
|
||||
*ptr = '\0';
|
||||
|
||||
BEGIN_CAPTURE
|
||||
{
|
||||
@ -507,8 +511,9 @@ void lammps_commands_string(void *handle, const char *str)
|
||||
lmp->error->all(FLERR,"Library error: issuing LAMMPS command during run");
|
||||
}
|
||||
|
||||
char *ptr = copy;
|
||||
for (int i=0; i < n-1; ++i) {
|
||||
n = strlen(copy);
|
||||
ptr = copy;
|
||||
for (int i=0; i < n; ++i) {
|
||||
|
||||
// handle continuation character as last character in line or string
|
||||
if ((copy[i] == '&') && (copy[i+1] == '\n'))
|
||||
|
||||
@ -118,4 +118,24 @@ TEST_F(LibraryCommands, from_string)
|
||||
lammps_commands_string(lmp, cmds.c_str());
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
EXPECT_EQ(lammps_get_natoms(lmp), 2);
|
||||
|
||||
// repeat test with DOS/Windows style CR-LF line endings
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
lammps_command(lmp, "clear");
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
cmds.clear();
|
||||
for (unsigned int i = 0; i < sizeof(demo_input) / sizeof(char *); ++i) {
|
||||
cmds += demo_input[i];
|
||||
cmds += "\r\n";
|
||||
}
|
||||
for (unsigned int i = 0; i < sizeof(cont_input) / sizeof(char *); ++i) {
|
||||
cmds += cont_input[i];
|
||||
cmds += "\r\n";
|
||||
}
|
||||
EXPECT_EQ(lammps_get_natoms(lmp), 0);
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
lammps_commands_string(lmp, cmds.c_str());
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
EXPECT_EQ(lammps_get_natoms(lmp), 2);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user