Skip to content

Commit

Permalink
Merge branch 'more-snprintf'
Browse files Browse the repository at this point in the history
More snprintf-related fixes.

See #1162.
  • Loading branch information
vadz committed Aug 20, 2024
2 parents 55436b5 + 5a300fe commit 8963695
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 31 deletions.
11 changes: 11 additions & 0 deletions include/private/soci-mktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ mktime_from_ymdhms(tm& t,
// Throws if the string in buf couldn't be parsed as a date or a time string.
SOCI_DECL void parse_std_tm(char const *buf, std::tm &t);

// Reverse function for formatting datetime values.
//
// The string returned in the buffer uses YYYY-MM-DD HH:MM:SS format.
inline int format_std_tm(std::tm const& t, char* buf, std::size_t len)
{
return snprintf(buf, len,
"%d-%02d-%02d %02d:%02d:%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
}

} // namespace details

} // namespace soci
Expand Down
12 changes: 7 additions & 5 deletions src/backends/mysql/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "soci/soci-platform.h"
#include "soci-dtocstr.h"
#include "soci-exchange-cast.h"
#include "soci-mktime.h"
#include "soci/blob.h"
// std
#include <ciso646>
Expand Down Expand Up @@ -153,11 +154,12 @@ void mysql_standard_use_type_backend::pre_use(indicator const *ind)
std::size_t const bufSize = 80;
buf_ = new char[bufSize];

std::tm const& t = exchange_type_cast<x_stdtm>(data_);
snprintf(buf_, bufSize,
"\'%d-%02d-%02d %02d:%02d:%02d\'",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
int n = 0;
buf_[n++] = '\'';
n += format_std_tm(exchange_type_cast<x_stdtm>(data_),
buf_ + n, bufSize - n - 1);
buf_[n++] = '\'';
buf_[n] = '\0';
}
break;
case x_blob:
Expand Down
9 changes: 6 additions & 3 deletions src/backends/mysql/vector-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "common.h"
#include "soci/soci-platform.h"
#include "soci-dtocstr.h"
#include "soci-mktime.h"
// std
#include <ciso646>
#include <cstddef>
Expand Down Expand Up @@ -207,9 +208,11 @@ void mysql_vector_use_type_backend::pre_use(indicator const *ind)
std::size_t const bufSize = 80;
buf = new char[bufSize];

snprintf(buf, bufSize, "\'%d-%02d-%02d %02d:%02d:%02d\'",
v[i].tm_year + 1900, v[i].tm_mon + 1, v[i].tm_mday,
v[i].tm_hour, v[i].tm_min, v[i].tm_sec);
int n = 0;
buf[n++] = '\'';
n += format_std_tm(v[i], buf + n, bufSize - n - 1);
buf[n++] = '\'';
buf[n] = '\0';
}
break;

Expand Down
1 change: 0 additions & 1 deletion src/backends/oracle/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#ifdef _MSC_VER
#pragma warning(disable:4355)
#define snprintf _snprintf
#endif

using namespace soci;
Expand Down
1 change: 0 additions & 1 deletion src/backends/oracle/vector-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#ifdef _MSC_VER
#pragma warning(disable:4355)
#define snprintf _snprintf
#endif

using namespace soci;
Expand Down
6 changes: 2 additions & 4 deletions src/backends/postgresql/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "soci/soci-platform.h"
#include "soci-dtocstr.h"
#include "soci-exchange-cast.h"
#include "soci-mktime.h"
#include <libpq/libpq-fs.h> // libpq
#include <cctype>
#include <cstdint>
Expand Down Expand Up @@ -142,10 +143,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind)
std::size_t const bufSize = 80;
buf_ = new char[bufSize];

std::tm const& t = exchange_type_cast<x_stdtm>(data_);
snprintf(buf_, bufSize, "%d-%02d-%02d %02d:%02d:%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
format_std_tm(exchange_type_cast<x_stdtm>(data_), buf_, bufSize);
}
break;
case x_rowid:
Expand Down
5 changes: 2 additions & 3 deletions src/backends/postgresql/vector-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "soci/soci-platform.h"
#include "soci/postgresql/soci-postgresql.h"
#include "soci-dtocstr.h"
#include "soci-mktime.h"
#include "common.h"
#include "soci/type-wrappers.h"
#include <libpq/libpq-fs.h> // libpq
Expand Down Expand Up @@ -218,9 +219,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind)
std::size_t const bufSize = 80;
buf = new char[bufSize];

snprintf(buf, bufSize, "%d-%02d-%02d %02d:%02d:%02d",
v[i].tm_year + 1900, v[i].tm_mon + 1, v[i].tm_mday,
v[i].tm_hour, v[i].tm_min, v[i].tm_sec);
format_std_tm(v[i], buf, bufSize);
}
break;
case x_xmltype:
Expand Down
8 changes: 2 additions & 6 deletions src/backends/sqlite3/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "soci/blob.h"
#include "soci-dtocstr.h"
#include "soci-exchange-cast.h"
#include "soci-mktime.h"
// std
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -176,12 +177,7 @@ void sqlite3_standard_use_type_backend::pre_use(indicator const * ind)
std::tm &t = exchange_type_cast<x_stdtm>(data_);

col.buffer_.data_ = new char[bufSize];
col.buffer_.size_
= snprintf(
col.buffer_.data_, bufSize, "%d-%02d-%02d %02d:%02d:%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec
);
col.buffer_.size_ = format_std_tm(t, col.buffer_.data_, bufSize);
break;
}

Expand Down
7 changes: 2 additions & 5 deletions src/backends/sqlite3/vector-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "soci/soci-platform.h"
#include "soci/sqlite3/soci-sqlite3.h"
#include "soci-dtocstr.h"
#include "soci-mktime.h"
#include "common.h"
// std
#include <cstdio>
Expand Down Expand Up @@ -176,11 +177,7 @@ void sqlite3_vector_use_type_backend::pre_use(indicator const * ind)
col.type_ = dt_date;
col.dataType_ = db_date;
col.buffer_.data_ = new char[bufSize];
col.buffer_.size_
= snprintf(col.buffer_.data_, bufSize, "%d-%02d-%02d %02d:%02d:%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec
);
col.buffer_.size_ = format_std_tm(tm, col.buffer_.data_, bufSize);
break;
}

Expand Down
5 changes: 2 additions & 3 deletions src/core/use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "soci/use-type.h"
#include "soci/statement.h"
#include "soci-exchange-cast.h"
#include "soci-mktime.h"

#include <cstdio>

Expand Down Expand Up @@ -98,9 +99,7 @@ void standard_use_type::dump_value(std::ostream& os) const
std::tm const& t = exchange_type_cast<x_stdtm>(data_);

char buf[80];
snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
format_std_tm(t, buf, sizeof(buf));

os << buf;
}
Expand Down

0 comments on commit 8963695

Please sign in to comment.