HPDF_SetCompressionMode() not working in Libharu - pdf

I am generating Pdf files using LibHaru libraries. My code is following
#include <iostream>
#include "hpdf.h"
using namespace std;
void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
{
}
int main()
{
cout<<"Compression"<<endl;
HPDF_Doc pdf = HPDF_New(error_handler, NULL);
if (!pdf)
return 0;
HPDF_STATUS Status = HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);
return 0;
}
PROBLEM: I debugged the code and found that HPDF_SetCompressionMode() returns 4129, which is the error code for Invalid value set when invoking HPDF_SetCommpressionMode(). .

If you step into the code, you will see you are getting the error because the ZLIB compression library was not compiled into your copy of HaruPDF.
First: comment out this line in ..\win32\include\hpdf_config.h:
/* zlib is not available */
//#define LIBHPDF_HAVE_NOZLIB
Second: find, download and unzip the ZLIB code. You can obtain the source from the following Website:
http://www.zlib.net/
Third: tell HaruPDF where it can find the ZLIB code, and recompile HaruPDF.
You should now be able to use compression.
Ain't Open Source grand?

Related

Boost asio crashes

I have a program using cpprestsdk for http querying and websocketpp for subscribing a data stream. The program will crash immediately(it says Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)). But if I comment either of the http querying or subcribing data stream, the program won't crash.
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include "json.hpp"
#include <iostream>
#include <ctime>
#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <vector>
#include <string>
using std::string;
using namespace web;
using std::cout, std::endl;
using std::vector;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_stream_data(websocketpp::connection_hdl hdl, message_ptr msg) {
}
class OrderBook {
public:
void initialize() {
web::http::client::http_client_config cfg;
std::string uri = string("https://fapi.binance.com/fapi/v1/depth?symbol=btcusdt&limit=1000");
web::http::client::http_client client(U(uri), cfg);
web::http::http_request request(web::http::methods::GET);
request.headers().add("Content-Type", "application/x-www-form-urlencoded");
web::http::http_response response = client.request(request).get();
}
int start_stream() {
client c;
std::string uri = string("wss://fstream.binance.com/ws/btcusdt#depth#100ms");
try {
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.init_asio();
c.set_message_handler(bind(on_stream_data, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
c.connect(con);
c.run();
} catch (websocketpp::exception const &e) {
std::cout << e.what() << std::endl;
}
}
};
int main(int argc, char *argv[]) {
OrderBook ob;
ob.initialize(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
std::this_thread::sleep_for(std::chrono::milliseconds(10000000));
ob.start_stream(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
}
When I run this program in Clion debug mode, Clion show that the crash comes from function in /opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/ssl/detail/impl/engine.ipp
int engine::do_connect(void*, std::size_t)
{
return ::SSL_connect(ssl_);
}
It says Exception: EXC_BAD_ACCESS (code=1, address=0xf000000000)
What's wrong with it? is it because I run two pieces of code using boost::asio, and something shouldn't be initialized twice?
I can compile this and run it fine.
My best bet is that you might be mixing versions, particularly boost versions. A common mode of failure is caused when ODR violations lead to Undefined Behaviour.
Note that these header-only libraries depend on a number of boost libraries that are not header-only (e.g. Boost System, Thread and/or Chrono). You need to compile against the same version as the libraries you link.
If you use distribution packaged versions of any library (cpprestsdk, websocketpp or whatever json library that is you're using) then you'd be safest also using the distribution packaged version of Boost.
I'd personally simplify the situation by just using Boost (Beast for HTTP/websocket, Json for, you guessed it).
Running it all on a test Ubuntu 18.04 the OS Boost 1.65 version, the start_stream sequence triggers this informative error:
[2022-05-22 13:42:11] [fatal] Required tls_init handler not present.
could not create connection because: Connection creation attempt failed
While being UBSAN/ASAN clean. Perhaps that error helps you, once you figure out the configuration problems that made your program crash.

Jaeger Tracing not working with yaml-cpp version 0.6.x

I am trying to instrument my program with jaeger-tracing (c++).
I was able to view my traces when I compliled the program with yaml-cpp version 0.5.3, but when I changed my yaml-cpp version to 0.6.x, I am unable to view my traces.
Dont know why its happening.
JaegerProgram Source code;
#include <iostream>
#include<memory>
#include <yaml-cpp/yaml.h>
#include<jaegertracing/Tracer.h>
#include<chrono>
using namespace std;
// using namespace literals::chrono_literals;
void init(const char *FilePath)
{
auto yaml = YAML::LoadFile(FilePath);
auto config = jaegertracing::Config::parse(yaml);
auto tracer=jaegertracing::Tracer::make(
"example-service-2",
config,
jaegertracing::logging::consoleLogger()
);
opentracing::Tracer::InitGlobal(
static_pointer_cast<opentracing::Tracer>(tracer)
);
}
void ChildSpan(const unique_ptr<opentracing::Span>& parentSpan){
this_thread::sleep_for(chrono::milliseconds(2));
auto childSpan = opentracing::Tracer::Global()->StartSpan("Span2",{opentracing::ChildOf(&parentSpan->context())});
}
void FollowsSpan(const unique_ptr<opentracing::Span>& followFromspan){
this_thread::sleep_for(chrono::milliseconds(3));
auto followSpan = opentracing::Tracer::Global()->StartSpan("Span3",{opentracing::FollowsFrom(&followFromspan->context())});
}
void ParentSpan(){
auto span = opentracing::Tracer::Global()->StartSpan("Span1");
ChildSpan(span);
FollowsSpan(span);
this_thread::sleep_for(chrono::milliseconds(4));
}
int main()
{
init("./config.yaml");
ParentSpan();
cout<<"Hello World"<<endl;
opentracing::Tracer::Global()->Close();
return 0;
}
compiling command - g++ -std=c++1z test.cpp -L /usr/local/lib/libyaml-cpp.a -ljaegertracing -lyaml-cpp
Yaml file
disabled: false
reporter:
logSpans: true
sampler:
type: const
param: 1
OS : ubuntu 18.04
jaegerTracing : master branch version
UPDATE
after little digging I found some fact, When I parse the above mention config file try to print the result I get the same value as written in config file, but when I parse same file using yaml-cpp-0.6.x the sampler.type is showing 'remote' and sampler.param to be '0.001' and when I manually change this change these values to be same as in config.yaml it has started showing traces. The error is present in parsing the yaml file as I could clearly see different values is loaded as configuration.

How to enumerate folder contents in Oct 2018

Trying to translate to cppwinrt the StorageFolder method GetFilesAsync I'm unable to get past compiler link errors. Here is a very simple routine to test the concept:
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Foundation.Collections.h"
IAsyncAction TestClass::LoadFiles()
{
StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation();
StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(hstring(L"Assets"));
auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery);
}
The problem seems to lie in the return type for GetFilesAsync. I've tried various types for that, e.g. IVectorView, but nothing seems to work. Does anyone know of a code example showing how this enumeration might be accomplished in C++/winrt?
[UPDATE] Returning to this project with SDK 10.0.17666 and VS 15.9.0 Preview 3 I find that the solution adopted earlier from these answers no longer works. This time I will be sure to include the full error to see if anyone has ideas. For simplicity I'll use just the simple code provided by IInspectable, altered only to make it a class member in my ResourceManager class:
#include "winrt/Windows.ApplicationModel.h"
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Streams.h"
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Storage.Search.h"
#include "winrt/Windows.UI.Core.h"
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Search;
IAsyncAction ResourceManager::LoadActivities()
{
StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation();
StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(L"Activities");
auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery);
}
The call to GetFilesAsync now produces the following link error:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: struct winrt::Windows::Foundation::IAsyncOperation > __thiscall winrt::impl::consume_Windows_Storage_Search_IStorageFolderQueryOperations::GetFilesAsync(enum winrt::Windows::Storage::Search::CommonFileQuery const &)const " (?GetFilesAsync#?$consume_Windows_Storage_Search_IStorageFolderQueryOperations#UStorageFolder#Storage#Windows#winrt###impl#winrt##QBE?AU?$IAsyncOperation#U?$IVectorView#UStorageFile#Storage#Windows#winrt###Collections#Foundation#Windows#winrt###Foundation#Windows#3#ABW4CommonFileQuery#Search#Storage#63##Z) referenced in function "public: struct winrt::Windows::Foundation::IAsyncAction __thiscall AppEngine::ResourceManager::LoadActivities$_ResumeCoro$2(void)" (?LoadActivities$_ResumeCoro$2#ResourceManager#AppEngine##QAE?AUIAsyncAction#Foundation#Windows#winrt##XZ)
(followed by the path to the object file)
I have to admit I find that error message hard to decipher. Perhaps someone else here will have an idea? Must be something that changed in recent system updates.
For what it's worth, the following standalone code builds just fine. So you're probably either missing a #include or a link library, but it's impossible to tell when you don't share important information like what actual error(s) you're seeing.
#pragma comment(lib, "WindowsApp")
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Search.h>
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Search;
IAsyncAction LoadFiles()
{
StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation();
StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(L"Assets");
auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery);
}
int main()
{
LoadFiles().get();
}

Where can I find libdecodeqr.lib?

I am creating a QR code scanner program in C++ using the library called libdecodeqr.
I downloaded all the files from https://github.com/josephholsten/libdecodeqr and I modified one of the test programs. However, I keep getting the error "unresolved external symbol". It's because I have not linked the libraries.
But I can't find libdecodeqr.lib anywhere! It's not included in the GitHub package and it's nowhere online!
In the instructions on the GitHub page, it says to add decodeqr.h, qrtypes.h, qrerror.h and libdecodeqr.lib to my environment. Other than the .lib file, I have added all those files to my environment.
Where can I find libdecodeqr.lib?
Also, here is the sample program I modified and am testing:
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/decodeqr.h"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/qrerror.h"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/qrtypes.h"
#include "bitstream.h"
using namespace cv;
int main(int argc, char *argv[])
{
namedWindow("src", 1);
//
// load image
//
Mat src_old = imread("qrTest.png", 1);
IplImage* src = new IplImage(src_old);
imshow("src", src_old);
//
// show version info
//
printf("libdecodeqr version %s\n", qr_decoder_version());
//
// initialize
//
QrDecoderHandle decoder = qr_decoder_open();
//
// do decode using default parameter
//
short stat = qr_decoder_decode_image(decoder, src);
printf("STATUS=%04x\n", stat);
//
// get QR code header
//
QrCodeHeader header;
if (qr_decoder_get_header(decoder, &header)){
//
// get QR code text
// To null terminate, a buffer size is larger than body size.
//
char *buf = new char[header.byte_size + 1];
qr_decoder_get_body(decoder, (unsigned char *)buf, header.byte_size + 1);
printf("%s\n", buf);
}
//
// finalize
//
qr_decoder_close(decoder);
puts("");
puts("Hit any key to end.");
cvWaitKey(0);
destroyAllWindows();
cvReleaseImage(&src);
return(0);
}
You are downloading the source code from GitHub, which means that you also need to create a new VisualStudio C++ project to compile your own libdecodeqr.lib (or dll). This will require you also install OpenCV in your computer.

How to make a loadable dll to use tcl code functionality by any program

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.
Here is my simple tcl code :
/* Filename : simple_addition.tcl */
#!/usr/bin/env tclsh8.5
proc add_two_nos { } {
set a 10
set b 20
set c [expr { $a + $b } ]
puts " c is $c ......."
}
And here is my c wrapper function which uses the above tcl addition functionality :
#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;
printf("inside main function \n");
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));
/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
fprintf(stderr, "ERROR in script: %s\n", result);
exit(1);
}
/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
printf("%s\n", result);
}
/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
}
This c wrapper is working fine for me and gives correct results.
Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.
In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.
VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Tcl_FindExecutable(NULL);
interp = Tcl_CreateInterp();
}
case DLL_THREAD_ATTACH:
break ;
case DLL_THREAD_DETACH:
break ;
case DLL_PROCESS_DETACH:
{
Tcl_DeleteInterp(interp);
break;
}
}
return TRUE;
}
and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.