How to connect boost / network / uri - c

Please tell me how to connect in the project
I needed a library for parsing with a URL. Found such a solution: https://cpp-netlib.org/0.10.1/in_depth/uri.html. Downloaded cpp-netlib-0.9.4. But I don’t know how to connect with my project
#include <iostream>
#include <boost/network/uri.hpp>
#include <boost/network/uri/uri_io.hpp>
using namespace boost::network;
int main() {
uri::uri url;
url << uri::scheme("http")
<< uri::host("www.github.com")
<< uri::path("/cpp-netlib");
std::cout << url << std::endl;
return 0;
}

Related

knowing end of message in protocol with msgpack

How do you manage end of a message in a protocol ? I use msgpack-c and the only solution I found is to send the header before the payload (separately).
Send the header to client :
// header
{
"message_type": "hello",
"payload_size": 10
}
The client received the header, unpack it, and allocate a buffer of "payload_size", receive data from stream, and if the buffer is complete the message is finish.
I want to send header and body succinctly
{
"header": { "message_type":"hello", "payload_size": 10},
"payload": {...} // can come in multiple frame
}
My problem is that I don't know if it's possible to partially unpack the header for knowing the size before receiving the full message (splitted if > 4096kb due to libevent restriction)
How would you do that ? I am open to all solutions.
C++
Using unpack() function
You can use offset parameter of unpack() function.
See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_unpacker#client-controls-a-buffer
Here is a code example:
#include <iostream>
#incluee <msgpack.hpp>
int main() {
msgpack::sbuffer buf;
msgpack::pack(buf, std::make_tuple("first message", 123, 56.78));
msgpack::pack(buf, std::make_tuple("second message", 42));
std::size_t off = 0; // cursor of buf
{
// unpack() function starts parse from off (0)
auto oh = msgpack::unpack(buf.data(), buf.size(), off);
// off is updated to 25. 25 is MessagePack formatted byte size
// of ["first message",123,56.78]
// (I use JSON notation but actual format is MessagePack)
std::cout << "off:" << off << std::endl;
std::cout << *oh << std::endl;
}
{
// unpack() function starts parse from off (25)
auto oh = msgpack::unpack(buf.data(), buf.size(), off);
// off is updated to 42.
// 42 - 25 = 17. 17 is MessagePack formatted byte size
// of ["second message",42]
// (I use JSON notation but actual format is MessagePack)
std::cout << "off:" << off << std::endl;
std::cout << *oh << std::endl;
}
}
Output is
off:25
["first message",123,56.78]
off:42
["second message",42]
msgpack-c unpack() manage the position of buffer internally.
You don't need to pass payload_size.
In addition you can mix non-msgpack format data in the buffer.
+--------------------+-----------------------------+--------------------+
| MessagePackBytes1 | Any format user knows size | MessagePackBytes2 |
+--------------------+-----------------------------+--------------------+
Let's say user knows the data structure that contains MessgePackBytes1(unknown size), any format data (known size), and MessgePackBytes1(unknown size).
#include <iostream>
#incluee <msgpack.hpp>
int main() {
msgpack::sbuffer buf;
msgpack::pack(buf, std::make_tuple("first message", 123, 56.78));
std::string non_mp = "non mp format data";
buf.write(non_mp.data(), non_mp.size());
msgpack::pack(buf, std::make_tuple("second message", 42));
std::size_t off = 0; // cursor of buf
{
auto oh = msgpack::unpack(buf.data(), buf.size(), off);
std::cout << "off:" << off << std::endl;
std::cout << *oh << std::endl;
}
{
std::string extracted{buf.data() + off, non_mp.size()};
std::cout << extracted << std::endl;
off += non_mp.size();
}
{
auto oh = msgpack::unpack(buf.data(), buf.size(), off);
std::cout << "off:" << off << std::endl;
std::cout << *oh << std::endl;
}
}
Output is
off:25
["first message",123,56.78]
non mp format data
off:60
["second message",42]
Using unpacker
It is a little advanced but it might fit streaming usecases.
https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_unpacker#msgpack-controls-a-buffer
Here is an example that unpacking MessagePack from continuous and scattered receiving message.
https://github.com/msgpack/msgpack-c/blob/700167995927f0348fb08ae2579440c1bc135480/example/boost/asio_send_recv.cpp#L41-L64
C
C version is basically similar to C++.
Using unpack() function
C version has the similar unpack function.
Here is the prototype:
msgpack_unpack_return
msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off);
You can pass off as offset similar to C++ version. C doesn't have reference so you need to pass the address of off using &off.
See https://github.com/msgpack/msgpack-c/wiki/v2_0_c_overview#using-unpack-function
If you want to know individual variable length field size such as stirng, you can access size member variable of unpacked object.
For example:
typedef struct {
uint32_t size;
struct msgpack_object* ptr;
} msgpack_object_array;
typedef struct {
uint32_t size;
const char* ptr;
} msgpack_object_str;
typedef struct {
uint32_t size;
const char* ptr;
} msgpack_object_bin;
typedef struct {
int8_t type;
uint32_t size;
const char* ptr;
} msgpack_object_ext;
See https://github.com/msgpack/msgpack-c/wiki/v2_0_c_overview#object
Using unpacker
See https://github.com/msgpack/msgpack-c/wiki/v2_0_c_overview#using-unpacker

having issues with simple for loop program. the similar code is having completely different results

I am a beginner programmer. i have just started learning c++. I am currently trying to find project which will help me in practicing different types of loop and command statements. in my recent project of creating a program to find factorial for a given number using for loop i am having problems which might be due to code differences. Can you guys help me find the differences in each code and why the outputs of each program are different ?
this is a code i wrote myself :
#include <iostream>
using namespace std;
int main (){
int i,B;
// B=1;
for (i=0; i < 10; i++);
{
//B*=i;
cout << i;
//cout << B << endl;
}
}
the code above gives output of only 10
this is a code i copied
#include <iostream>
using namespace std;
int main(){
int a;
for (int a=1; a <=10;a++)
{
cout << a << endl ;
}
}
this gives output of all the numbers upto 10.
i cant see any reason why the first code would give me an output of 10 as it is "<10" and not "<=10

CERN ROOT exporting data to plain text

So I have tried and tried to follow similar questions asked like this one, but to no success.
It's really simple - I have some .root files and can see the histograms in ROOT but want to export the data as a .txt or similar to be able to analyse it in other programs.
Here is working example. Reads in a root file with three branches, named TS, ns, and nserr.
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include <fstream>
using namespace std;
void dumpTreeTotxt(){
TFile *f=new TFile("TS0.root"); // opens the root file
TTree *tr=(TTree*)f->Get("tree"); // creates the TTree object
tr->Scan(); // prints the content on the screen
float a,b,c; // create variables of the same type as the branches you want to access
tr->SetBranchAddress("TS",&a); // for all the TTree branches you need this
tr->SetBranchAddress("ns",&b);
tr->SetBranchAddress("nserr",&c);
ofstream myfile;
myfile.open ("example.txt");
myfile << "TS ns nserr\n";
for (int i=0;i<tr->GetEntries();i++){
// loop over the tree
tr->GetEntry(i);
cout << a << " " << b << " "<< c << endl; //print to the screen
myfile << a << " " << b << " "<< c<<"\n"; //write to file
}
myfile.close();
}

QtCreator undefined reference using FTD2XX C++

So I've been playing with this one for a week now and I figured it was time to call in some backup. I am currently developing some software to communicate with some boards I am developing. The boards have ft230 chips from FTDI, so I am using their FTD2XX library to interface with them. This is where the issue starts.
For the longest time I have been getting "Undefined Reference" errors for some functions and not others. So I think the linker is correct, it just isn't working for all the functions. My environment is QtCreator 3.3.0 based on QT 5.4.0. The compiler is MinGW 4.9.1. The operating system is Windows 8.1. I have downloaded the latest drivers for 8.1 from ftdichips.com. Below is the function giving me the issue:
#include <windows.h>
#include "ftdiDriver/ftdi.h"
#include <ftd2xx.h>
#include <QDebug>
void ftdiConnect(void){
// Variables for FTDI communicaiton
FT_STATUS ftStatus;
FT_DEVICE_LIST_INFO_NODE *devInfo;
DWORD numDevs;
// create the device information list
ftStatus = FT_CreateDeviceInfoList(&numDevs);
if (ftStatus == FT_OK) {
qDebug() << "Number of devices is: " << numDevs;
}
if (numDevs > 0) {
// allocate storage for list based on numDevs
devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);
// get the device information list
ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
if (ftStatus == FT_OK) {
for (int i = 0; i < numDevs; i++) {
qDebug() << "Dev: " << i;
qDebug() << " Flags: " << devInfo[i].Flags;
qDebug() << " Type: " << devInfo[i].Type;
qDebug() << " ID: " << devInfo[i].ID;
qDebug() << " LocId: " << devInfo[i].LocId;
qDebug() << " SerialNumber: " << devInfo[i].SerialNumber;
qDebug() << " Description: " << devInfo[i].Description;
qDebug() << " ftHandle: " << devInfo[i].ftHandle;
}
}
}
}
I've searched a couple forums. It seems that most people are able to include the .lib file in their project file and things seem to work. I have tried every combination of this, and I have the same issue. Since some functions for (FT_ListDevices for example), I don't think that's the issue. It would not make sense for some functions to work and not others. I've called FTDIchips and they told me they would get back to me when they have had time to look at the issue, but had no idea of anything to try in the mean time.
If you think it would be helpful, I could include the project text in this question as well. I just didn't want it to become cluttered.
Linker's "undefined references" mean (i) either you didn't provide those functions referenced (no .lib file), or (ii) you provide wrong stuff (say, 32-bit instead of 64-bit), or (iii) you asked for wrong function (wrong function name or wrong modifiers).
Anyway, it's completely your fault. You should at last read something about linking executables with external libraries and dll's, and start using your head. It's a shame to ask tech support, just because you are too lazy to read some linker's guide.

Looking for determine file type from the magic number

It is possible to determine the file type from the magic number of file?
If I've understood, the magic number can have different size, maybe a reference dictionary or something like a library could help me?
it is possible to determine the file type from the magic number of file
yes you can ,because each file format has different magic number.
for example FFD8 for .jpg files
See here Magic Numbers in Files
The file command on Linux does precisely that. Study its internals to see how it identifies files using their magic-numbers(signature bytes). The complete source-code is available at darwinsys.com/file.
The following 2 lists are the most comprehensive ones with file-types and their magic-numbers:
- File Signature Table
- Linux Magic Numbers
JmimeMagic is a java library for such
Use libmagic (apt-get install libmagic-dev on Ubuntu systems).
Example below uses the default magic database to query the file passed on the command line. (Essentially an implementation of the file command. See man libmagic for more details/functions.
#include <iostream>
#include <magic.h>
#include <cassert>
int main(int argc, char **argv) {
if (argc == 1) {
std::cerr << "Usage " << argv[0] << " [filename]" << std::endl;
return -1;
}
const char * fname = argv[1];
magic_t cookie = magic_open(0);
assert (cookie !=nullptr);
int rc = magic_load(cookie, nullptr);
assert(rc == 0);
auto f= magic_file(cookie, fname);
if (f ==nullptr) {
std::cerr << magic_error(cookie) << std::endl;
} else {
std::cout << fname << ' ' << f << std::endl;
}
}

Resources