Compare commits
14 Commits
v2.0.7
...
0799513ea2
Author | SHA1 | Date | |
---|---|---|---|
0799513ea2 | |||
effeee105c | |||
ed5b366341 | |||
9a520ddd9c | |||
2c43087d15 | |||
60ac1365a5 | |||
57027e234e | |||
887d5db635 | |||
ecbae3f79b | |||
b54979aa74 | |||
724f3d0bb9 | |||
06c5710412 | |||
b45fae576f | |||
55f093156d |
@@ -233,3 +233,6 @@ can be found in the `python/examples` folder.
|
||||
- https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun
|
||||
- https://github.com/pypa/cibuildwheel/blob/main/examples/github-deploy.yml
|
||||
- https://cibuildwheel.readthedocs.io/en/stable/deliver-to-pypi/
|
||||
- https://www.gnu.org/software/libiconv/
|
||||
- https://vcpkg.io/en/packages.html
|
||||
- https://vcpkg.io/en/getting-started
|
||||
|
0
lib/half.hpp → lib/3rdparty/half.hpp
vendored
0
lib/half.hpp → lib/3rdparty/half.hpp
vendored
@@ -9,6 +9,12 @@
|
||||
#include <math.h>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <time.h>
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <iconv.h>
|
||||
#elif defined(__WIN32__) || defined(_WIN32)
|
||||
#define timegm _mkgmtime
|
||||
#endif
|
||||
|
||||
//---------------------------------------------------------------------------//
|
||||
|
||||
@@ -142,6 +148,87 @@ namespace imc
|
||||
return sumstr;
|
||||
}
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
// convert encoding of any descriptions, channel-names, units etc.
|
||||
class iconverter
|
||||
{
|
||||
std::string in_enc_, out_enc_;
|
||||
iconv_t cd_;
|
||||
size_t out_buffer_size_;
|
||||
|
||||
public:
|
||||
|
||||
iconverter(std::string in_enc, std::string out_enc, size_t out_buffer_size = 1024) :
|
||||
in_enc_(in_enc), out_enc_(out_enc), out_buffer_size_(out_buffer_size)
|
||||
{
|
||||
// allocate descriptor for character set conversion
|
||||
// (https://man7.org/linux/man-pages/man3/iconv_open.3.html)
|
||||
cd_ = iconv_open(out_enc.c_str(), in_enc.c_str());
|
||||
|
||||
if ( (iconv_t)-1 == cd_ )
|
||||
{
|
||||
if ( errno == EINVAL )
|
||||
{
|
||||
std::string errmsg = std::string("The encoding conversion from ") + in_enc
|
||||
+ std::string(" to ") + out_enc + std::string(" is not supported by the implementation.");
|
||||
throw std::runtime_error(errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void convert(std::string &astring)
|
||||
{
|
||||
if ( astring.empty() ) return;
|
||||
|
||||
std::vector<char> in_buffer(astring.begin(),astring.end());
|
||||
char *inbuf = &in_buffer[0];
|
||||
size_t inbytes = in_buffer.size();
|
||||
|
||||
std::vector<char> out_buffer(out_buffer_size_);
|
||||
char *outbuf = &out_buffer[0];
|
||||
size_t outbytes = out_buffer.size();
|
||||
|
||||
// perform character set conversion
|
||||
// ( - https://man7.org/linux/man-pages/man3/iconv.3.html
|
||||
// - https://www.ibm.com/docs/en/zos/2.2.0?topic=functions-iconv-code-conversion )
|
||||
while ( inbytes > 0 )
|
||||
{
|
||||
size_t res = iconv(cd_,&inbuf,&inbytes,&outbuf,&outbytes);
|
||||
|
||||
if ( (size_t)-1 == res )
|
||||
{
|
||||
std::string errmsg;
|
||||
if ( errno == EILSEQ )
|
||||
{
|
||||
errmsg = std::string("An invalid multibyte sequence is encountered in the input.");
|
||||
throw std::runtime_error(errmsg);
|
||||
}
|
||||
else if ( errno == EINVAL )
|
||||
{
|
||||
errmsg = std::string("An incomplete multibyte sequence is encountered in the input")
|
||||
+ std::string(" and the input byte sequence terminates after it.");
|
||||
}
|
||||
else if ( errno == E2BIG )
|
||||
{
|
||||
errmsg = std::string("The output buffer has no more room for the next converted character.");
|
||||
}
|
||||
throw std::runtime_error(errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
std::string outstring(out_buffer.begin(),out_buffer.end()-outbytes);
|
||||
astring = outstring;
|
||||
}
|
||||
};
|
||||
#elif defined(__WIN32__) || defined(_WIN32)
|
||||
class iconverter
|
||||
{
|
||||
public:
|
||||
iconverter(std::string in_enc, std::string out_enc, size_t out_buffer_size = 1024) {}
|
||||
void convert(std::string &astring) {}
|
||||
};
|
||||
#endif
|
||||
|
||||
// channel
|
||||
struct channel
|
||||
{
|
||||
@@ -301,9 +388,12 @@ namespace imc
|
||||
double secs_int;
|
||||
trigger_time_frac_secs_ = modf((double)secs,&secs_int);
|
||||
tms.tm_sec = (int)secs_int;
|
||||
//tms.tm_isdst = -1;
|
||||
|
||||
// generate std::chrono::system_clock::time_point type
|
||||
std::time_t ts = std::mktime(&tms);
|
||||
// ( - https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html
|
||||
// - https://man7.org/linux/man-pages/man3/tzset.3.html )
|
||||
std::time_t ts = timegm(&tms); //std::mktime(&tms);
|
||||
trigger_time_ = std::chrono::system_clock::from_time_t(ts);
|
||||
}
|
||||
|
||||
@@ -313,6 +403,9 @@ namespace imc
|
||||
// calculate absolute trigger-time
|
||||
absolute_trigger_time_ = trigger_time_ + std::chrono::seconds(addtime_);
|
||||
// + std::chrono::nanoseconds((long int)(trigger_time_frac_secs_*1.e9));
|
||||
|
||||
// convert any non-UTF-8 codepage to UTF-8
|
||||
convert_encoding();
|
||||
}
|
||||
|
||||
// convert buffer to actual datatype
|
||||
@@ -400,6 +493,40 @@ namespace imc
|
||||
}
|
||||
}
|
||||
|
||||
// convert any description, units etc. to UTF-8 (by default)
|
||||
void convert_encoding()
|
||||
{
|
||||
// actual input codepage
|
||||
std::string cpn;
|
||||
|
||||
if ( !codepage_.empty() )
|
||||
{
|
||||
// construct iconv-compatible name for respective codepage
|
||||
cpn = std::string("CP") + codepage_;
|
||||
}
|
||||
else {
|
||||
// assume codepage 1252 by default
|
||||
cpn = std::string("CP1252");
|
||||
}
|
||||
|
||||
// set up converter
|
||||
std::string utf = std::string("UTF-8");
|
||||
iconverter conv(cpn,utf);
|
||||
|
||||
conv.convert(name_);
|
||||
conv.convert(comment_);
|
||||
conv.convert(origin_);
|
||||
conv.convert(origin_comment_);
|
||||
conv.convert(text_);
|
||||
conv.convert(language_code_);
|
||||
conv.convert(yname_);
|
||||
conv.convert(yunit_);
|
||||
conv.convert(xname_);
|
||||
conv.convert(xunit_);
|
||||
conv.convert(group_name_);
|
||||
conv.convert(group_comment_);
|
||||
}
|
||||
|
||||
// get info string
|
||||
std::string get_info(int width = 20)
|
||||
{
|
||||
@@ -413,8 +540,8 @@ namespace imc
|
||||
<<std::setw(width)<<std::left<<"comment:"<<comment_<<"\n"
|
||||
<<std::setw(width)<<std::left<<"origin:"<<origin_<<"\n"
|
||||
<<std::setw(width)<<std::left<<"description:"<<text_<<"\n"
|
||||
<<std::setw(width)<<std::left<<"trigger-time-nt:"<<std::put_time(std::localtime(&tt),"%FT%T")<<"\n"
|
||||
<<std::setw(width)<<std::left<<"trigger-time:"<<std::put_time(std::localtime(&att),"%FT%T")<<"\n"
|
||||
<<std::setw(width)<<std::left<<"trigger-time-nt:"<<std::put_time(std::gmtime(&tt),"%FT%T")<<"\n"
|
||||
<<std::setw(width)<<std::left<<"trigger-time:"<<std::put_time(std::gmtime(&att),"%FT%T")<<"\n"
|
||||
<<std::setw(width)<<std::left<<"language-code:"<<language_code_<<"\n"
|
||||
<<std::setw(width)<<std::left<<"codepage:"<<codepage_<<"\n"
|
||||
<<std::setw(width)<<std::left<<"yname:"<<yname_<<"\n"
|
||||
@@ -451,8 +578,8 @@ namespace imc
|
||||
<<"\",\"comment\":\""<<comment_
|
||||
<<"\",\"origin\":\""<<origin_
|
||||
<<"\",\"description\":\""<<text_
|
||||
<<"\",\"trigger-time-nt\":\""<<std::put_time(std::localtime(&tt),"%FT%T")
|
||||
<<"\",\"trigger-time\":\""<<std::put_time(std::localtime(&att),"%FT%T")
|
||||
<<"\",\"trigger-time-nt\":\""<<std::put_time(std::gmtime(&tt),"%FT%T")
|
||||
<<"\",\"trigger-time\":\""<<std::put_time(std::gmtime(&att),"%FT%T")
|
||||
<<"\",\"language-code\":\""<<language_code_
|
||||
<<"\",\"codepage\":\""<<codepage_
|
||||
<<"\",\"yname\":\""<<prepjsonstr(yname_)
|
||||
|
9
makefile
9
makefile
@@ -10,8 +10,11 @@ SRC = src/
|
||||
LIB = lib/
|
||||
PYT = python/
|
||||
|
||||
# list headers
|
||||
# list headers and include directories
|
||||
HPP = $(wildcard $(LIB)/*.hpp)
|
||||
IPP = $(shell find $(LIB) -type f -name '*.hpp')
|
||||
KIB = $(shell find $(LIB) -type d)
|
||||
MIB = $(foreach dir,$(KIB),-I $(dir))
|
||||
|
||||
# choose compiler and its options
|
||||
CC = g++ -std=c++17
|
||||
@@ -36,12 +39,12 @@ $(EXE): check-tags $(GVSN) main.o
|
||||
$(CC) $(OPT) main.o -o $@
|
||||
|
||||
# build main.cpp and include git version/commit tag
|
||||
main.o: src/main.cpp $(HPP)
|
||||
main.o: src/main.cpp $(IPP)
|
||||
@cp $< $<.cpp
|
||||
@sed -i 's/TAGSTRING/$(GTAG)/g' $<.cpp
|
||||
@sed -i 's/HASHSTRING/$(GHSH)/g' $<.cpp
|
||||
@sed -i 's/TIMESTAMPSTRING/$(TMS)/g' $<.cpp
|
||||
$(CC) -c $(OPT) -I $(LIB) $<.cpp -o $@
|
||||
$(CC) -c $(OPT) $(MIB) $<.cpp -o $@
|
||||
@rm $<.cpp
|
||||
|
||||
install: $(EXE)
|
||||
|
@@ -5,6 +5,16 @@ from IMCtermite cimport cppimctermite
|
||||
|
||||
import json as jn
|
||||
import decimal
|
||||
import platform
|
||||
|
||||
# auxiliary function for codepage conversion
|
||||
def get_codepage(chn) :
|
||||
if platform == 'Windows' :
|
||||
chndec = jn.loads(chn.decode(errors="ignore"))
|
||||
chncdp = chndec["codepage"]
|
||||
return 'utf-8' if chncdp is None else chncdp
|
||||
else :
|
||||
return 'utf-8'
|
||||
|
||||
cdef class imctermite:
|
||||
|
||||
@@ -22,7 +32,7 @@ cdef class imctermite:
|
||||
# get JSON list of channels
|
||||
def get_channels(self, bool include_data):
|
||||
chnlst = self.cppimc.get_channels(True,include_data)
|
||||
chnlstjn = [jn.loads(chn.decode(errors="ignore")) for chn in chnlst]
|
||||
chnlstjn = [jn.loads(chn.decode(get_codepage(chn),errors="ignore")) for chn in chnlst]
|
||||
return chnlstjn
|
||||
|
||||
# print single channel/all channels
|
||||
|
@@ -2,3 +2,4 @@ include lib/*.hpp
|
||||
include *.cpp
|
||||
include *.pyx
|
||||
include *.pxd
|
||||
include VERSION
|
||||
|
@@ -1 +1 @@
|
||||
2.0.7
|
||||
2.0.16
|
||||
|
Reference in New Issue
Block a user