I am serializing a packet over XDR but i do not understand how to provide vector of string. I have here a small fully working serialization / deserialization for a std::vector of uint64_t. Here my code:
Serializer:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_LENGTH_ 100
int main(void)
{
XDR xdr;
xdrstdio_create(&xdr, stdout, XDR_ENCODE);
std::vector<uint64_t> ids; // vector i want to send
ids.push_back(1);
ids.push_back(2);
ids.push_back(3);
// serializing the vector
uint64_t *_ids = &ids[0];
uint32_t size = ids.size();
xdr_array(&xdr,(char**)(&_ids), &size, MAX_LENGTH_,sizeof(uint64_t),(xdrproc_t)xdr_u_long);
return 1;
}
Deserializer:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_LENGTH_ 100
int main(void)
{
XDR xdrs;
xdrstdio_create(&xdrs, stdin, XDR_DECODE);
uint64_t *ids_ = new uint64_t[MAX_LENGTH_];
uint32_t size;
bool status = xdr_array(&xdrs,(char**)(&ids_), &size, MAX_LENGTH_,
sizeof(uint64_t), (xdrproc_t)xdr_u_long);
std::vector<uint64_t> ids(ids_,ids_+size);
for(std::vector<uint64_t>::iterator it = ids.begin(); it != ids.end(); ++it)
{
std::cout << *it <<std::endl;
}
return 1;
}
The following code works... running ./serializer | ./deserializer i obtain 1 2 3. Now I do not know how to handle having to serialize std::vector<std::string>. A single string works well using xdr_string.
http://linux.die.net/man/3/xdr_array
Any help would be very much appreciated!
EDIT:
I have tried the following:
Serializer:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#include <algorithm>
#include <cstring>
#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50
char *convert(const std::string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}
int main(void)
{
XDR xdr;
xdrstdio_create(&xdr, stdout, XDR_ENCODE);
std::vector<std::string> messages; // vector i want to send
messages.push_back("this is");
messages.push_back("my string");
messages.push_back("vector test");
// transform the vector to c style
std::vector<char*> messagesCStyle;
std::transform(messages.begin(), messages.end(), std::back_inserter(messagesCStyle), convert);
// serializing the vector
char **_messages = &messagesCStyle[0];
uint32_t size = messages.size();
xdr_array(&xdr,(char**)(&_messages), &size, MAX_VECTOR_LENGTH_ * MAX_STRING_LENGTH_,sizeof(char),(xdrproc_t)xdr_string);
return 1;
}
Deserializer:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50
int main(void)
{
XDR xdrs;
xdrstdio_create(&xdrs, stdin, XDR_DECODE);
std::vector<char*> messagesCStyle_;
uint32_t size;
bool status = xdr_array(&xdrs,(char**)(&messagesCStyle_), &size, MAX_VECTOR_LENGTH_,
MAX_STRING_LENGTH_, (xdrproc_t)xdr_string);
for(std::vector<char*>::iterator it = messagesCStyle_.begin(); it != messagesCStyle_.end(); ++it)
{
std::cout << *it <<std::endl;
}
return 1;
}
I am pretty sure the code for the Serializer is not best but at least it seams to work. However the deserializer does not!! I think the problem is related to the fact that i do not know how much memory to allocate before calling the xdr_array. Any help?
I made it work:
Encoder:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#include <algorithm>
#include <cstring>
#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50
char *convert(const std::string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}
int main(void)
{
XDR xdr;
xdrstdio_create(&xdr, stdout, XDR_ENCODE);
std::vector<std::string> messages; // vector i want to send
messages.push_back("this is");
messages.push_back("my string");
messages.push_back("vector test");
messages.push_back("this is a relatively long string!!!");
// transform the vector to c style
std::vector<char*> messagesCStyle;
std::transform(messages.begin(), messages.end(),
std::back_inserter(messagesCStyle),
[](const std::string & s){
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
});
// serializing the vector
char **_messages = &messagesCStyle[0];
uint32_t size = messages.size();
xdr_array(&xdr,(char**)(&_messages), &size, MAX_VECTOR_LENGTH_ * MAX_STRING_LENGTH_,sizeof(char*),(xdrproc_t)xdr_string);
return 1;
}
Decoder:
#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50
int main(void)
{
XDR xdrs;
uint32_t size;
char** buffer = NULL;
xdrstdio_create(&xdrs, stdin, XDR_DECODE);
bool status = xdr_array(&xdrs, (char**) &buffer, &size, MAX_VECTOR_LENGTH_,
sizeof(char*), (xdrproc_t)xdr_string);
std::cout << "status: " << status << std::endl;
std::cout << "size: " << size << std::endl;
std::vector<std::string> stringMessages_(buffer, buffer + size);
for(std::vector<std::string>::iterator it = stringMessages_.begin(); it != stringMessages_.end(); ++it)
{
std::cout << *it <<std::endl;
}
for (int i = 0; i < size; i++) {
free(buffer[i]);
}
free(buffer);
return 1;
}
Related
So i have a program that records snippets of audio on a microphone array, it then timestamps the end time of the file. My problem is that the process of the program starting the recording takes a random amount of time to start and so the audio is a random length. I want a way to read the size of the file (kilobytes) then trim the file by a certain number of kilobytes so that the recordings are always the same length.
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include<signal.h>
#include<unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "zlib.h"
long long int rem;
long long int ms1;
long long int unixtime;
using namespace std;
const char* Filenames;
int main(void) {
int system(const char *command);
int x;
struct timeval tp1;
rem = 5;
while (rem != 0) {
gettimeofday(&tp1, NULL);
ms1 = tp1.tv_sec * 1000ll + tp1.tv_usec / 1000ll;
rem = ms1 % 10000;
}
for (x=0; x<3; x++){
pid_t pid=fork();
if (pid==0){
execl("/home/pi/odas/bin/odaslive", "/home/pi/odas/bin/odaslive", "-vc", "/home/pi/odas/config/odaslive/matrix_creator.cfg", (char *)NULL);
exit(127);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(15000));
kill(pid, SIGINT);
}
gettimeofday(&tp1, NULL);
unixtime = tp1.tv_sec + tp1.tv_usec / 1000000ll;
std::string name1 = "/home/pi/matrix_creator_explore/postfiltered/postfiltered1_";
std::string name2 = ".raw";
std::string result1;
result1 = name1 + std::to_string(unixtime) + name2;
const char *cstr = result1.c_str();
rename ("/home/pi/matrix_creator_explore/postfiltered.raw", cstr);
std::string name3 = "/home/pi/matrix_creator_explore/tracked/tracked1_";
std::string name4 = ".raw";
std::string result2;
result2 = name3 + std::to_string(unixtime) + name4;
const char *cstr1 = result2.c_str();
rename ("/home/pi/matrix_creator_explore/tracked.raw", cstr1);
struct stat buf;
stat( cstr,&buf);
printf ("\n %i \n", buf.st_size);
}
}
I have two programs and I want them to communicate together by msgrcv() && msgsnd(). I so have a master program which init the message queue and start the 2 others programs:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int qid = msgget(ftok(".",'u'), 0);
char* params[3];
params[1] = (char *)malloc(sizeof(char) * 9);
sprintf(params[1], "%d", qid);
params[2] = NULL;
printf("qid = %d and qid(str) = %s", qid, params[1]);
// return (0);
//spawning two child processes
pid_t cpid = fork();
if (cpid == 0) {
params[0] = (char*)"./sender";
execv(params[0], params);
exit(0);
}
cpid = fork();
if (cpid == 0) {
params[0] = (char*)"./receiver";
execv(params[0], params);
exit(0);
}
while (wait(NULL) != -1); // waiting for both children to terminate
msgctl(qid, IPC_RMID, NULL);
std::cout << "parent proc: " << getpid()
<< " now exits" << std::endl;
exit(0);
}
I also prepare the parameters and start the both following programs:
sender
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int ac, char **av) {
if (ac != 2)
return (-1);
// create my msgQ with key value from ftok()
// int qid = msgget(IPC_PRIVATE, IPC_EXCL|IPC_CREAT|0600);
int qid = atoi(av[1]);
// declare my message buffer
struct buf {
long mtype; // required
char greeting[50]; // mesg content
};
buf msg;
int size = sizeof(msg)-sizeof(long);
std::cout << "Welcome in the prog assignment 2! Type [exit] to stop the program." << std::endl;
bool exit = false;
while (!exit)
{
std::cout << getpid() << ": ";
std::cin.getline(msg.greeting, 50, '\n');
std::cout << msg.greeting << std::endl;
msg.mtype = 114; // only reading mesg with type mtype = 114
if (strcmp(msg.greeting, "exit") == 0)
exit = true;
msgsnd(qid, (struct msgbuf *)&msg, size, 0);
}
}
receiver
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
int main(int ac, char **av) {
int i = 0;
while (i < ac)
printf("AV: %s\n", av[i++]);
if (ac != 2)
return (-1);
// int qid = msgget(IPC_PRIVATE, IPC_EXCL|IPC_CREAT|0600);
int qid = atoi(av[1]);
// declare my message buffer
struct buf {
long mtype;
char greeting[50];
};
buf msg;
int size = sizeof(msg)-sizeof(long);
bool exit = false;
while (!exit)
{
msgrcv(qid, (struct msgbuf *)&msg, size, 114, 0);
if (strcmp(msg.greeting, "exit") == 0)
exit = true;
std::cout << getpid() << msg.greeting << std::endl;
}
std::cout << "get out" << std::endl;
}
It doesn't work and I'm not sure to understand why because, I'm creating the message queue, passing it as parameter, then I put it back as int and then use it. However, it just gives me an infinite loop of weird display, why?
ANy help is welcome.. Thank !
Hi I am new to usage of Poco , can you please help me to find a way to get the index/position during the writing into deflating stream so that I can truncate the invalid data and make sure my file contains only valid data.
#include <stdexcept>
#include <stdarg.h>
#include <map>
#include <iostream>
#include <cstring>
#include <fstream>
#include <Poco/DeflatingStream.h>
#include <stdio.h>
#include <limits>
#include <stdio.h>
#include <unistd.h>
using namespace std;
std::ofstream* ostr;
Poco::DeflatingOutputStream* ofstr;
string fileName="/home/lamb/Cpp/simple.gzip";
bool written = false;
// int lastsucessfulwrite;
compress(){
*ofstr << "\t<xyz>\n";
*ofstr << "\t</xyz>\n";
*ofstr << " who=\"";
*ofstr << "/>\n";
written = true;
/* "lastsucessfulwrite" How to store the index of ofstr , in case of normal files we use ftell but in DeflatingOutputStream how to get index so that I can erase it later based on this value */
}
timer(){
sleep(2);
// 2 second
written = false ;
}
close(){
ofstr->close();
delete ofstr;
ofstr = NULL;
ostr->close();
delete ostr;
ostr = NULL;
}
int main(){
ostr = new std::ofstream;
ostr->exceptions(std::ofstream::failbit|std::ofstream::badbit);
ostr->open(_fileName.c_str(), std::ios::binary | std::ios::app);
ofstr = new Poco::DeflatingOutputStream(*_ostr,
Poco::DeflatingStreamBuf::STREAM_GZIP);
ofstr->precision(std::numeric_limits<double>::digits10);
string data1 = "hello';
string data2 = "hello';
string data3 = "hello';
written = false ;
timer()//start
compress(data1);
if(written)
{
compress(data2);
}
if(written)
{
compress(data2);
}
if(written)
{
compress(data3);// timeup and time() is inovked and part of compress() is executed
}
// Now I would like to use lastsucessfulwrite as the key and truncate the paritally witten data3
// In case of normal file we use "truncate" system call
close();
}
You can use any standard C++ stream functions with Poco streams.
streampos pos = ofstr->tellp()
I want to make a program that reads the highest value from one file and stores it in another. I've read about ifstream and ofstream but how do I let the ofstream store the highest value from the instream in another file? Here is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct CsvWhitespace : ctype<char> {
static const mask* make_table() {
static vector<mask> v{classic_table(), classic_table() + table_size};
v[','] |= space; // comma will be classified as whitespace
return v.data();
}
CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
} csvWhitespace;
int main() {
string line;
ifstream myfile ("C:/Users/Username/Desktop/log.csv");
ofstream myfile2 ("C:/Users/Username/Desktop/log2.csv");
return 0;
}
auto v = vector<int>{};
myfile.imbue(locale{myfile.getloc(), &csvWhitespace});
copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
myfile2 << *max_element(begin(v), end(v));
}
Thanks in advance :)
You could just copy from the one file in the other, without having to worry about the format, by treating them in binary mode. Here is an example:
#include <stdio.h>
#include <string.h>
#define bufSize 1024
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
char buf[bufSize];
if (argc != 3)
{
fprintf(stderr,
"Usage: %s <soure-file> <target-file>\n", argv[0]);
return 1;
}
if ((ifp = fopen(argv[1], "rb")) == NULL)
{ /* Open source file. */
perror("fopen source-file");
return 1;
}
if ((ofp = fopen(argv[2], "wb")) == NULL)
{ /* Open target file. */
perror("fopen target-file");
return 1;
}
while (fgets(buf, sizeof(buf), ifp) != NULL)
{ /* While we don't reach the end of source. */
/* Read characters from source file to fill buffer. */
/* Write characters read to target file. */
fwrite(buf, sizeof(char), strlen(buf), ofp);
}
fclose(ifp);
fclose(ofp);
return 0;
}
which was given as an example in IP, source. You just need to specify the cmd arguments as the desired files.
You can do it like this. Live example using cin and cout rather than files.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct CsvWhitespace : ctype<char> {
static const mask* make_table() {
static vector<mask> v{classic_table(), classic_table() + table_size};
v[','] |= space; // comma will be classified as whitespace
return v.data();
}
CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
};
int main() {
string line;
ifstream myfile("log.csv");
ofstream myfile2("log2.csv");
auto v = vector<int>{};
myfile.imbue(locale{myfile.getloc(), new CsvWhitespace{}});
copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
myfile2 << *max_element(begin(v), end(v));
}
I want to create some modules for my program. I want to call a function and pass a vector as a parameter. The return value should also be a vector.
My code looks like this
main.cpp
//BlueSmart.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
using namespace std;
#pragma comment(lib, "Irprops.lib")
BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};
BLUETOOTH_RADIO_INFO m_bt_info = {
sizeof(BLUETOOTH_RADIO_INFO),
0,
};
BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL
};
BLUETOOTH_DEVICE_INFO m_device_info = {
sizeof(BLUETOOTH_DEVICE_INFO),
0,
};
HANDLE m_radio = NULL;
HBLUETOOTH_RADIO_FIND m_bt = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
int wmain(int argc, wchar_t **args) {
while(true) {
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
do {
localBluetoothDevices ();
m_search_params.hRadio = m_radio;
::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
vector<wstring> vec;
int m_device_id = 0;
do {
wostringstream tmp;
++m_device_id;
//Something like this <----------------------------------------
externBluetoothDevices (vec);
//Something like this <----------------------------------------
wprintf(L"********************************************************************** \n");
wprintf(L"\tDevice %d:\r\n", m_device_id);
wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
wprintf(L"====================================================================== \n");
for (int i = 0; i < 6; i++) {
tmp << hex << m_device_info.Address.rgBytes [i];
if (i < 5)
tmp << L':';
}
vec.push_back(tmp.str());
} while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
BluetoothFindDeviceClose(m_bt_dev);
//Sleep(10*1000*60);
Sleep(10000);
} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
BluetoothFindRadioClose(m_bt);
}
return 0;
}
//Lokal verfügbare bzw. angeschlossene Bluetooth-Devices
void localBluetoothDevices (){
int m_radio_id = 0;
m_radio_id++;
BluetoothGetRadioInfo(m_radio, &m_bt_info);
//Lokaler Bluetoothadapter
wprintf(L"====================================================================== \n");
wprintf(L"Local Device Nr. %d\n", m_radio_id);
wprintf(L"\tName: %s\r\n", m_bt_info.szName);
wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);
}
//Extern verfügbare bzw. Bluetooth-Devices
vector<wstring> externBluetoothDevices (vector<wstring> &vec){
return vec;
}
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <bthdef.h>
#include <BluetoothAPIs.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
#include <conio.h>
void localBluetoothDevices ();
vector<wstring> externBluetoothDevices (vector<wstring>);
It says that vector is not a known type. What am I doing wrong?
In stdafx.h replace
vector<wstring> externBluetoothDevices (vector<wstring>);
with
std::vector<std::wstring> externBluetoothDevices (std::vector<std::wstring>);
Basically the issue was although you put using namespace std; in your cpp file that doesn't count in your header file which is before the using declaration is seen.
Also note that your defintion in the cpp file is different. In the cpp file you have a reference
vector<wstring> externBluetoothDevices (vector<wstring>&);
Decide which you really want.
You should pass a pointer of a vector.