code snippet:
std::function<void(const void *& buf, int& size)> f = [data2, read_buf,key_name](const void *& buf, int& size) {
auto ctx = data2->GetContext();
void *reply = NULL;
auto status = redisGetReply(ctx, &reply);
if (status == REDIS_OK) {
redisReply *r = (redisReply *) reply;
char *data_buf = 0;
int data_size = 0;
if (GetData(r, data_buf, data_size, 3, 2)) {
if (read_buf->capacity() < data_size) {
read_buf->resize(data_size * 2);
}
memcpy(&(*read_buf)[0], data_buf, data_size);
buf = (const void *) &(*read_buf)[0];
size = data_size;
}
}
else {
std::cerr << "[redis-io]: subscribe get message returns status: " << status << ", Error Code : " << ctx->err << ", Error Details : " << ctx->errstr << ", Channel Info : " << key_name << std::endl;
throw runtime_error("[redis-io] subscribe get message returns error.");
}
if (reply != NULL)
freeReplyObject(reply);
};
the problem is when redisGetReply(ctx, &reply) returns error and enter the else branch, the printed out error message string of ctx->errstr is messy code, which is unreadable as follows:
messy code image
How to fix the issue to show error message normally? Great appreciate.
Related
I'm trying to create an HTTP server using C++ 98.
The issue is that, every time I launch my server, I get the response, sending again the request from the same browser tab, and the browser keeps loading.
In my kqueue, it seems like the block for checking the read event is not being executed on the second time.
This is my code:
void webserv::Server::lunch(){
this->kq.create_event(this->sock.getSocket(), EVFILT_READ);
while(1)
this->_lunch_worker();
}
void webserv::Server::_lunch_worker(void)
{
int n_ev;
int accept_sock;
int address_size;
char buff[1000];
webserv::Header header;
// register the events in ev_list
std::cout << GREEN << "---- WAITING FOR CONNECTION ----" << RESET << std::endl;
n_ev = this->kq.get_event();
for (int i = 0; i < n_ev; i++)
{
if (this->kq.get_fd(i) < 0)
continue;
if (this->kq.get_fd(i) == this->sock.getSocket())
{
std::cout << "--- RECEIVED NEW CONNECTION ---" << std::endl;
address_size = sizeof(this->sock.getAddress());
accept_sock = accept(
this->sock.getSocket(),
(struct sockaddr*)&this->sock.getAddress(),
(socklen_t *)&address_size
);
this->sock.test_error(accept_sock);
this->kq.create_event(accept_sock, EVFILT_READ);
int flags;
if ((flags = fcntl(accept_sock, F_GETFL, 0)) < 0) {
perror("fcntl");
close(accept_sock);
close(this->sock.getSocket());
}
if (fcntl(accept_sock, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl");
close(accept_sock);
close(this->sock.getSocket());
}
this->kq.create_event(accept_sock, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
}
else if (this->kq.is_read_available(i))
{
int bytes_read;
std::cout << "START: is_read_available" << std::endl;
if ((bytes_read = recv(this->kq.get_fd(i), buff, 999, 0)) > 0)
{
}
}
else if (this->kq.is_write_available(i))
{
std::string hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!";
if (send(this->kq.get_fd(i), hello.c_str(), hello.length(), 0) != 0)
{
std::cout << "TEST2" << std::endl;
this->kq.set_event(this->kq.get_fd(i), EVFILT_WRITE, EV_DELETE);
this->kq.get_event_list()[i].ident = -1;
close(this->kq.get_fd(i));
}
std::cout << "END: is_write_available" << std::endl;
}
}
}
And this is the kqueue class:
/************************ CONSTRUCTORS/DESTRUCTOR ************************/
webserv::Kqueue::Kqueue()
{
this->_kq = kqueue();
std::cout << "KQUEUE CREATED" << std::endl;
this->test_error(this->_kq, "Creating Kqueue :");
this->_n_ev = 0;
}
webserv::Kqueue::~Kqueue()
{
close(this->_kq);
}
/************************ MEMBER FUNCTIONS ************************/
void webserv::Kqueue::set_event(int fd, int filter, int flags, void *udata)
{
EV_SET(&this->_ev_set, fd, filter, flags, 0, 0, udata);
}
void webserv::Kqueue::add_event(void)
{
int ret;
ret = kevent(this->_kq, &this->_ev_set, 1, NULL, 0, NULL);
this->test_error(ret, "Kqueue/add_even functions");
}
int webserv::Kqueue::get_event(void)
{
this->_n_ev = kevent(this->_kq, NULL, 0, this->_ev_list, __EV_LIST_SIZE__, NULL);
this->test_error(this->_n_ev, "Kqueue/get_event function:");
return (this->_n_ev);
}
void webserv::Kqueue::create_event(int fd, int filter, int flags, void *udata)
{
this->set_event(fd, filter, flags, udata);
this->add_event();
}
bool webserv::Kqueue::isEOF(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/isEOF function:");
return (this->_ev_list[index].flags & EV_EOF);
}
bool webserv::Kqueue::is_read_available(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/is_read_available function:");
return (this->_ev_list[index].filter == EVFILT_READ);
}
bool webserv::Kqueue::is_write_available(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/is_write_available function:");
return (this->_ev_list[index].filter == EVFILT_WRITE);
}
void webserv::Kqueue::test_error(int fd, const std::string &str)
{
if (fd < 0)
{
std::cerr << RED << str << " ";
perror("The following error occured: ");
std::cerr << RESET;
exit(EXIT_FAILURE);
}
}
/************************ GETTERS/SETTERS ************************/
struct kevent *webserv::Kqueue::get_event_list()
{
return (this->_ev_list);
}
int webserv::Kqueue::get_fd(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/get_ev_list function:");
return (this->_ev_list[index].ident);
}
void webserv::Kqueue::set_kqueue(int fd)
{
this->_kq = fd;
}
I'have started to work on ns3 and i try to extract from code manet-routing-compare.cc which is intalled by default with NS3.26 in examples/routing.I want to extract 4 parameters :number of sent packet, reception time of packet ,send time and packet size in order to calculate throughput and delay end to end.I have tried but i don't find a solution and every time we have error. Please, help me i really need this parametrs and thank you very much.
#include <fstream>
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/aodv-module.h"
#include "ns3/olsr-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/dsr-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
using namespace dsr;
NS_LOG_COMPONENT_DEFINE ("manet-routing-compare");
class RoutingExperiment
{
public:
RoutingExperiment ();
void Run (int nSinks, double txp, std::string CSVfileName);
//static void SetMACParam (ns3::NetDeviceContainer & devices,
// int slotDistance);
std::string CommandSetup (int argc, char **argv);
private:
Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node);
void ReceivePacket (Ptr<Socket> socket);
void CheckThroughput ();
uint32_t port;
uint32_t bytesTotal;
uint32_t packetsReceived;
std::string m_CSVfileName;
int m_nSinks;
std::string m_protocolName;
double m_txp;
bool m_traceMobility;
uint32_t m_protocol;
};
RoutingExperiment::RoutingExperiment ()
: port (9),
bytesTotal (0),
packetsReceived (0),
m_CSVfileName ("manet-routing.output.csv"),
m_traceMobility (false),
m_protocol (2) // AODV
{
}
static inline std::string
PrintReceivedPacket (Ptr<Socket> socket, Ptr<Packet> packet, Address senderAddress)
{
std::ostringstream oss;
oss << Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId ();
if (InetSocketAddress::IsMatchingType (senderAddress))
{
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
oss << " received one packet from " << addr.GetIpv4 ();
}
else
{
oss << " received one packet!";
}
return oss.str ();
}
void
RoutingExperiment::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
}
void
RoutingExperiment::CheckThroughput ()
{
double kbs = (bytesTotal * 8.0) / 1000;
bytesTotal = 0;
std::ofstream out (m_CSVfileName.c_str (), std::ios::app);
out << (Simulator::Now ()).GetSeconds () << ","
<< kbs << ","
<< packetsReceived << ","
<< m_nSinks << ","
<< m_protocolName << ","
<< m_txp << ""
<< std::endl;
out.close ();
packetsReceived = 0;
Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this);
}
Ptr<Socket>
RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr<Node> node)
{
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> sink = Socket::CreateSocket (node, tid);
InetSocketAddress local = InetSocketAddress (addr, port);
sink->Bind (local);
sink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket, this));
return sink;
}
std::string
RoutingExperiment::CommandSetup (int argc, char **argv)
{
CommandLine cmd;
cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName);
cmd.AddValue ("traceMobility", "Enable mobility tracing", m_traceMobility);
cmd.AddValue ("protocol", "1=OLSR;2=AODV;3=DSDV;4=DSR", m_protocol);
cmd.Parse (argc, argv);
return m_CSVfileName;
}
int
main (int argc, char *argv[])
{
RoutingExperiment experiment;
std::string CSVfileName = experiment.CommandSetup (argc,argv);
//blank out the last output file and write the column headers
std::ofstream out (CSVfileName.c_str ());
out << "SimulationSecond," <<
"ReceiveRate," <<
"PacketsReceived," <<
"NumberOfSinks," <<
"RoutingProtocol," <<
"TransmissionPower" <<
std::endl;
out.close ();
int nSinks = 10;
double txp = 7.5;
experiment.Run (nSinks, txp, CSVfileName);
}
void
RoutingExperiment::Run (int nSinks, double txp, std::string CSVfileName)
{
Packet::EnablePrinting ();
m_nSinks = nSinks;
m_txp = txp;
m_CSVfileName = CSVfileName;
int nWifis = 50;
double TotalTime = 200.0;
std::string rate ("2048bps");
std::string phyMode ("DsssRate11Mbps");
std::string tr_name ("manet-routing-compare");
int nodeSpeed = 20; //in m/s
int nodePause = 0; //in s
m_protocolName = "protocol";
Config::SetDefault ("ns3::OnOffApplication::PacketSize",StringValue ("64"));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate));
//Set Non-unicastMode rate to unicast mode
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue (phyMode));
NodeContainer adhocNodes;
adhocNodes.Create (nWifis);
// setting up wifi phy and channel using helpers
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
wifiPhy.SetChannel (wifiChannel.Create ());
// Add a mac and disable rate control
WifiMacHelper wifiMac;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode));
wifiPhy.Set ("TxPowerStart",DoubleValue (txp));
wifiPhy.Set ("TxPowerEnd", DoubleValue (txp));
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
MobilityHelper mobilityAdhoc;
int64_t streamIndex = 0; // used to get consistent mobility across scenarios
ObjectFactory pos;
pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"));
pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));
Ptr<PositionAllocator> taPositionAlloc = pos.Create ()->GetObject<PositionAllocator> ();
streamIndex += taPositionAlloc->AssignStreams (streamIndex);
std::stringstream ssSpeed;
ssSpeed << "ns3::UniformRandomVariable[Min=0.0|Max=" << nodeSpeed << "]";
std::stringstream ssPause;
ssPause << "ns3::ConstantRandomVariable[Constant=" << nodePause << "]";
mobilityAdhoc.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
"Speed", StringValue (ssSpeed.str ()),
"Pause", StringValue (ssPause.str ()),
"PositionAllocator", PointerValue (taPositionAlloc));
mobilityAdhoc.SetPositionAllocator (taPositionAlloc);
mobilityAdhoc.Install (adhocNodes);
streamIndex += mobilityAdhoc.AssignStreams (adhocNodes, streamIndex);
AodvHelper aodv;
OlsrHelper olsr;
DsdvHelper dsdv;
DsrHelper dsr;
DsrMainHelper dsrMain;
Ipv4ListRoutingHelper list;
InternetStackHelper internet;
switch (m_protocol)
{
case 1:
list.Add (olsr, 100);
m_protocolName = "OLSR";
break;
case 2:
list.Add (aodv, 100);
m_protocolName = "AODV";
break;
case 3:
list.Add (dsdv, 100);
m_protocolName = "DSDV";
break;
case 4:
m_protocolName = "DSR";
break;
default:
NS_FATAL_ERROR ("No such protocol:" << m_protocol);
}
if (m_protocol < 4)
{
internet.SetRoutingHelper (list);
internet.Install (adhocNodes);
}
else if (m_protocol == 4)
{
internet.Install (adhocNodes);
dsrMain.Install (dsr, adhocNodes);
}
NS_LOG_INFO ("assigning ip address");
Ipv4AddressHelper addressAdhoc;
addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer adhocInterfaces;
adhocInterfaces = addressAdhoc.Assign (adhocDevices);
OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ());
onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
for (int i = 0; i < nSinks; i++)
{
Ptr<Socket> sink = SetupPacketReceive (adhocInterfaces.GetAddress (i), adhocNodes.Get (i));
AddressValue remoteAddress (InetSocketAddress (adhocInterfaces.GetAddress (i), port));
onoff1.SetAttribute ("Remote", remoteAddress);
Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
ApplicationContainer temp = onoff1.Install (adhocNodes.Get (i + nSinks));
temp.Start (Seconds (var->GetValue (100.0,101.0)));
temp.Stop (Seconds (TotalTime));
}
std::stringstream ss;
ss << nWifis;
std::string nodes = ss.str ();
std::stringstream ss2;
ss2 << nodeSpeed;
std::string sNodeSpeed = ss2.str ();
std::stringstream ss3;
ss3 << nodePause;
std::string sNodePause = ss3.str ();
std::stringstream ss4;
ss4 << rate;
std::string sRate = ss4.str ();
//NS_LOG_INFO ("Configure Tracing.");
//tr_name = tr_name + "_" + m_protocolName +"_" + nodes + "nodes_" + sNodeSpeed + "speed_" + sNodePause + "pause_" + sRate + "rate";
//AsciiTraceHelper ascii;
//Ptr<OutputStreamWrapper> osw = ascii.CreateFileStream ( (tr_name + ".tr").c_str());
//wifiPhy.EnableAsciiAll (osw);
AsciiTraceHelper ascii;
MobilityHelper::EnableAsciiAll (ascii.CreateFileStream (tr_name + ".mob"));
//Ptr<FlowMonitor> flowmon;
//FlowMonitorHelper flowmonHelper;
//flowmon = flowmonHelper.InstallAll ();
NS_LOG_INFO ("Run Simulation.");
CheckThroughput ();
Simulator::Stop (Seconds (TotalTime));
Simulator::Run ();
//flowmon->SerializeToXmlFile ((tr_name + ".flowmon").c_str(), false, false);
Simulator::Destroy ();
}
I am gonna answer it in two parts,
First the parameters which you can get without making much changes, which are reception time and packet size:
PrintReceivedPacket function is called when a packet is received at the receiver, you have access to received packet at this point. The Simulator::Now () gives you the reception time of the packet at sink and packet->GetSize () gives you the size of packet received.
Second the parameters of send time and number of packet sent. You will have to add a sequence to steps to get the trace to
Tx of OnOffApplication
Get Application from ApplicationContainer temp using Get (0)
Cast Application to OnOffApplication using dynamic cast
Connect to the Tx of this pointer to a function and in this function increase a global counter for number of packet sent and log the current time for sent time
Do let me know if any query
I am writing a server/client program using linux sockets. I want that both server and client can read and write at the same time with threads.
(The full code I followed can be downloaded at http://www.praxisbuch.net/embedded-linux/downloads.html).
When I start() my thread it throws a infinite loop of 'Bad file descriptor'.
My client.cpp is as follows:
class receiveThread : public Thread {
public:
receiveThread(RawMessage& rawMsg) : data_control(rawMsg) {}
virtual void run() {
cout << "starting recveive thread ..." << endl;
while(1) {
int recvBytes = bt_socket.recieveMessage(&temp_data_control);
try {
cout << "Received new Target Position: " << temp_data_control.data.value1 << endl;
data_control = temp_data_control;
}
catch (SocketException) {
cout << "Received exception " << endl;
sleep(1);
}
}
}
~receiveThread() {
bt_socket.disconnect();
}
private:
BtSocket bt_socket;
RawMessage temp_data_control;
RawMessage& data_control;
};
int main() {
BtSocket BtClient(PORTNUM);
// connect to the server.
BtClient.connectToServer();
RawMessage raw_mes;
receiveThread recv_thread(raw_mes);
recv_thread.start(); //the error occurs
while (1)
{
//do something
}
return 0;
}
thread.cpp is as follows:
void Thread::start() throw(Thread::Exception) {
if (pthread_create(&thread_id, 0, thread_starter, (void *) this) != 0) {
throw Exception("Thread::start: Cannot create thread");
}
}
void* Thread::thread_starter(void* thread_obj) {
Thread* self = reinterpret_cast<Thread*> (thread_obj);
self->setRunning(true);
self->run();
self->setRunning(false);
return 0;
}
In socket.cpp the exception is thrown:
int BtSocket::recieveMessage(AbstractDatagram *abstractDgm)
{
int numBytes;
if ((numBytes = recv(s, abstractDgm->getData(), abstractDgm->getSize(), 0)) < 0)
{
cout << strerror(errno) << endl;
}
return numBytes;
}
I really don't know what the problem is.
Any help would be extremely appreciated!
EDIT: (also from socket.cpp)
Socket::Socket(int pNumber)
{
portNumber = pNumber;
blocking = 1;
if ((s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) == -1)
{
throw SocketException("socket failed (socket())", true);
}
locAddr.rc_family = AF_BLUETOOTH;
str2ba(dest, &locAddr.rc_bdaddr);
locAddr.rc_channel = portNumber;
}
void BtSocket::connectToServer()
{
struct sockaddr_rc serverAddr;
serverAddr.rc_family = AF_BLUETOOTH;
serverAddr.rc_channel = 1;
str2ba(dest, &serverAddr.rc_bdaddr);
if (connect(s, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1)
{
throw SocketException("Connect failed (connect())", true);
}
}
I think I opend the socket correctly!?
Could it have been closed somehow?
I have an assignment and it's to build a queue linked list.
Our professor gave us the skeleton and told us to use his main.
I wrote my functions and everything compiled right but when I ran it, I got this error.
Don't know what to do from here.
Source Code:
#include<iostream>
using namespace std;
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
template<class ItemType>
class Queue
{
private:
int size;
NodeType<ItemType>* front; // It points to the front of a singly-linked list
NodeType<ItemType>* rear; // It points to the end of a singly-linked list
public:
Queue(); // default constructor: Queue is created and empty
Queue(const Queue<ItemType> &x); // copy constructor: implicitly called
// for a deep copy
void MakeEmpty(); // Queue is made empty; you should deallocate all
// the nodes of the linked list
bool IsEmpty( ); // test if the queue is empty
bool IsFull( ); // test if the queue is full; assume MAXITEM=5
int length( ); // return the number of elements in the queue
void Print( ); // print the value of all elements in the queue in the sequence
// from the front to rear
void Enqueue(ItemType x); // insert x to the rear of the queue
// Precondition: the queue is not full
void Dequeue(ItemType &x); // delete the element from the front of the queue
// Precondition: the queue is not empty
~Queue(); // Destructor: memory for the dynamic array needs to be deallocated
};
template<class ItemType>
Queue<ItemType>::Queue()
{
size = 0;
front = NULL;
rear = NULL;
}
template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType> &x)
{
NodeType<ItemType>* ptr1 ;
NodeType<ItemType>* ptr2 ;
if ( x.front == NULL )
{
front = NULL ;
}
else // allocate memory for first node
{
front = new NodeType<ItemType> ;
front->info = x.front->info ;
ptr1 = x.front->next ;
ptr2 = front ;
while ( ptr1 != NULL ) // deep copy other nodes
{
ptr2->next = new NodeType<ItemType> ;
ptr2 = ptr2->next ;
ptr2->info = ptr1->info ;
ptr1 = ptr1->next ;
}
ptr2->next = NULL;
rear = ptr2;
}
}
template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(front != NULL)
{
tempPtr = front;
front = front->next;
delete tempPtr;
}
rear=NULL;
}
template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{
return (size == 0);
}
template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return (size >= 5);
}
template<class ItemType>
int Queue<ItemType>::length()
{
return size;
}
template<class ItemType>
void Queue<ItemType>::Enqueue(ItemType x)
{
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = x;
newNode->next = NULL;
if(rear == NULL)
{
front = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
size++;
}
template<class ItemType>
void Queue<ItemType>::Dequeue(ItemType &x)
{
NodeType<ItemType>* tempPtr;
if(!IsEmpty())
{
tempPtr = front;
x = front->info;
front = front->next;
if(front == NULL)
{
rear = NULL;
}
delete tempPtr;
}
size--;
}
template<class ItemType>
void Queue<ItemType>::Print()
{
NodeType<ItemType> *temp;
temp = rear;
while(temp != NULL)
{
cout<<temp->info<<endl;
temp = temp->next;
}
}
template<class ItemType>
Queue<ItemType>::~Queue()
{
MakeEmpty();
}
int main()
{
Queue<int>IntQueue;
int x;
IntQueue.MakeEmpty();
IntQueue.Dequeue(x);
IntQueue.Enqueue(10);
IntQueue.Enqueue(20);
IntQueue.Enqueue(30);
IntQueue.Enqueue(40);
cout << "int length 3 = " << IntQueue.length() << endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << IntQueue.length() << endl;
cout << "The int queue contains: " << endl;
IntQueue.Print();
if(IntQueue.IsFull() == false)
cout << "The int queue is not full !" << endl;
else
cout << "The int queue is full !" << endl;
Queue<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << "The float queue contains: " << endl;
FloatQueue.Print();
Queue<float> FloatQueue2 = FloatQueue;
cout << "The float queue 2 contains: " << endl;
FloatQueue2.Print();
FloatQueue.MakeEmpty();
cout << "The float queue 3 contains: " << endl;
FloatQueue2.Print();
system("pause");
return 0;
}
The problem I'm having is obviously with the print function.
Any help is appreciated.
Inside your copy constructor, you aren't setting rear to anything when x.front == NULL. This sets temp to an invalid value inside Print, causing you both to loop when you shouldn't and dereference an invalid pointer.
I'm searching for a library to get the outline of a glyph in a true type font on a linux system. We are using Pango and Cairo but unfortunatly I didn't find anything.
I'm looking for somethig similar to GlyphTypeface.GetGlyphOutline under .NET
Any help or hints are appreciated!
Thanks in advance
The solution is to use FreeType, it offers the range of function I need:
#include <string>
#include <iostream>
#include <freetype/ftglyph.h>
#include <freetype/freetype.h>
//******************* check error code ********************
void Check(FT_Error ErrCode, const char* OKMsg, const char* ErrMsg)
{
if(ErrCode != 0)
{
std::cout << ErrMsg << ": " << ErrCode << "\n";
std::cout << "program halted\n";
exit(1);
}
else
std::cout << OKMsg << "\n";
}
//******************** get outline ************************
int GetOutLine(FT_Glyph glyph, FT_OutlineGlyph* Outg)
{
int Err = 0;
switch ( glyph->format )
{
case FT_GLYPH_FORMAT_BITMAP:
Err = 1;
break;
case FT_GLYPH_FORMAT_OUTLINE:
*Outg = (FT_OutlineGlyph)glyph;
break;
default:
;
}
return Err;
}
//*********** print outline to console ***************
int PrintOutLine(FT_OutlineGlyph Outg)
{
int Err = 0;
FT_Outline* Ol = &Outg->outline;
int Start = 0; //start index of contour
int End = 0; //end index of contour
short* pContEnd = Ol->contours; //pointer to contour end
FT_Vector* pPoint = Ol->points; //pointer to outline point
char* pFlag = Ol->tags; //pointer to flag
for(int c = 0; c < Ol->n_contours; c++)
{
std::cout << "\nContour " << c << ":\n";
End = *pContEnd++;
for(int p = Start; p <= End; p++)
{
char Ch = *pFlag++ + '0';
std::cout << "Point " << p <<": X=" << pPoint->x << " Y="<<pPoint->y << " Flag=" << Ch << "\n";
pPoint++;
}
Start = End + 1;
}
return Err;
}
//*********** get glyph index from command line *************
FT_UInt GetGlyphIndex(int argc, char* argv[], int Nr)
{
if(argc > Nr)
{
return atoi(argv[Nr]);
}
else
{
return 36;
}
}
//*********** get font name from command line *************
void GetFontName(int argc, char* argv[], int Nr, std::string& FontName)
{
if(argc > Nr)
{
FontName += argv[Nr];
}
else
{
FontName += "FreeMono.ttf";
}
}
//*********** get font size from command line *************
int GetFontSize(int argc, char* argv[], int Nr)
{
short FontSize = 50 * 64;
if(argc > Nr)
FontSize += atoi(argv[Nr]);
return FontSize;
}
//******************** M A I N ************************
// par1: FontName, par2:Glyph-Nr, par3: FontSize as FT_F26Dot6
int main(int argc, char* argv[])
{
FT_Face face;
FT_Library library;
FT_Error error;
error = FT_Init_FreeType( &library );
Check(error, "", "error initializing FT lib");
std::string FontName;
FontName = "/usr/share/fonts/truetype/freefont/";
GetFontName(argc, argv, 1, FontName);
error = FT_New_Face( library, FontName.c_str(), 0, &face );
Check(error, "", "error loading font");
FT_F26Dot6 font_size = GetFontSize(argc, argv, 3);
error = FT_Set_Char_Size( face, font_size, font_size, 72, 72 );
Check(error, "", "error setting char size");
FT_UInt glyph_index = GetGlyphIndex(argc, argv, 2);
FT_Int32 load_flags = FT_LOAD_DEFAULT;
error = FT_Load_Glyph( face, glyph_index, load_flags );
Check(error, "", "error loading glyph");
FT_Glyph glyph;
error = FT_Get_Glyph( face->glyph, &glyph );
Check(error, "", "error getting glyph");
FT_OutlineGlyph Outg;
error = GetOutLine(glyph, &Outg);
Check(error,"", "error getting outline");
std::cout << "=======================================================\n";
std::cout << "Font: " << FontName << "\n";
std::cout << "Glyph Index: " << glyph_index << "\n";
std::cout << "=======================================================\n";
error = PrintOutLine(Outg);
Check(error,"", "error printing outline");
return 0;
}
Cairo has this capability builtin.
cairo_glyph_path() will get the glyph outline and make it the current path, and allow you to get the current path and iterate over it.