Category Archives : c/C++


Lexical Analysis: You should use FLEX…right?

It’s been a hobby of mine for that last few years to try to create my own programming language. Don’t ask why, I just want to. Over the years I have tried and failed multiple times. This is mainly due to the fact that I am a self taught programmer with no CS background, or Math background, or any real education. When you set out to learn how one makes a programming language you will receive a series of answers similar to this: Use flex to generate a lexical analyzer. Use Bison to generate a Parser Forget both of those and use ANTLR To understand what you need to do to create a programming language, you will need to understand that in general all programming languages have a set of modules that take turns with the users input (i.e. the script). The Lexer The Lexer, or Scanner is the first component of a programming language. It reads a stream of characters and groups them together into meaningful parts. The Lexer adds no semantic meaning to the stream. Lexers create “meaningful” tokens. They generally, but not always, do not create whitespace tokens, they “ignore” them in the sense that they do not lead to the emission of a token, but they “use” them to delineate tokens. Flex has a simple interface for creating a lexer, a simple Scanner will look like this: If we save that as test.y and run: flex test.y cc lex.yy.c -o example -lfl ./example We can then start entering in some text. […]


clang++ and C++11 generic Makefile

As a general rule, I have a little scratch pad Makefile that I use as a template for all my C++ apps. I thought I would post my basic template when I am prototyping some code, or just running a little test example. CXX=clang++ FLAGS=-std=c++11 -stdlib=libc++ all: clean: rm -fr build/* test: $(CXX) $(FLAGS) src/test.cpp -o build/test.bin -Isrc/ .PHONY: all clean test


Qt C++: Threaded Communication with Artema Hybrid on Linux

Communicating with an Artema Hybrid CC Payment device is actually deceptively simple. I’ve written an application what uses a webkit widget exclusively for the UI display, and so I hook into the Javascript to provide some extra functionality to the app, in this case, reading and writing to/from the Artema Hybrid device. The documentation that I received was in German exclusively, so this took a bit of work to get going, but once you have it down it’s very easy. The device is connected via it’s POS connection Serial->RJ45 connection to a Serial->USB (FTDI chipset) converter that gets plugged into a usb port. You use the standard open, read, write and close functions. The Artema Hybrid constantly communicates with your system, so you’ll need to create a QThread to run in the background reading from the device once every second. The Artema Hybrid will send you an ENQ (0×05) and you need to respond with either an ACK (0×06) or an STX (0×02). With the way that I have it set up, the c++ code does the absolute minimum required which is it reads from the device and emits a dataRead() signal which is connected in Javascript. If I read ENQ, then I emit dataRead(“ENQ”). and in the Javascript function that is connected to the dataRead() signal, I see if the string passed up == ENQ. In the javascript, I have an array of data to write to the device, if that array is empty, I just write back to the Device ACK, otherwise, I […]


Qt C++ Screen Capture using BitBlt 2

So I got to thinking I wanted to be able to take abitrary screen captures based on some startx/starty -> endx/endy input. I found various sites showing how to do this, but most of them were pretty confusing. I thought I would post the final working code here. void RSystem::snap(int startx, int starty,int endx,int endy) { HDC hdcScreen,hdcMemDC = NULL; HBITMAP hbmScreen = NULL; BITMAP bmpScreen; HWND hDesktopWnd = NULL; hDesktopWnd = GetDesktopWindow(); hdcScreen = GetDC(hDesktopWnd); hdcMemDC = CreateCompatibleDC(hdcScreen); if (!hdcScreen) { qDebug()


Qt C++ application framework evangelism

I am normally not much of an evangelist, but after working with Qt C++ on a few different apps, (my first experience was back when it was still trolltech and I was running KDE on Mandrake Linux, ahh the good ol days.) and all I can say is: This is C++? Qt is the C++ framework of the future, and those who haven’t adopted it should, now, as soon as possible. A little case study will suffice. I have been working on a project for a few months now and a few days ago it came up that there needed to be plugin architecture integrated into the software so that third party vendors could easily add functionality to the program. Sounds fun huh? The answer was simple, Qt already has a signals and slots mechanism, asking third party vendors to use Qt isn’t unreasonable, its a dead easy framework to use, and no, it’s not that bloated. Here were the steps: Create a standard message definition that can be used to communicate steps and program state Create a general purpose relay QObject (I called it Telegraph) with a send and receive. It sends, whatever it receives. Pass the relay object to the library allowing them to connect to the send/receive methods on the object The entire plugin architecture was up and running in less than an hour, and tested, it hasn’t had an issue yet, though I am sure something might need some finaigling before it ships. The great thing about it is, plugins can […]


HowTo: C Function Pointers for a Linear Search, void star pointer trick, comparison using memcmp, string replace

Well, I was messing around in C again today, sometimes I just love C, it’s a fun language when you understand it, which I really don’t, but am beginning to grok it. Anyway, I was checking out a little tutorial on C and saw this cool little trick that I thought I would share. It’s the beginnings of a generalized linear search algorithm. #include #include #include int linearSearch (void * key, /* pointer to the data to compar*/ void * base, /* base of the data*/ int n, /* number of elements */ int size, /* quantum of data*/ int ( * cmpfunc)(void * e1,void * e2, int size) /* comparison function */); int simpleCompare(void * e1, void * e2, int size); int main ( int argc, char *argv[]) { int array[5]; /* a simple array of ints*/ int i; for (i = 0; i < 5; i++) { array[i] = i; /*load the array so we can search*/ } int s = 24;/* this is the search value, change it around to test */ /*we pass in the addresses of s, the array, which is a pointer already, the number of elements, the sizeof the data, and the address of the function*/ if (linearSearch(&s, array, 5, sizeof(int), &simpleCompare) != -1) { printf(“%i was found in array. \n”,s); } else { printf(“%i was not found in array.\n”,s); } } int linearSearch (void * key, /* pointer to the data to compar*/ void * base, /* base of the data*/ int n, /* number of elements […]


cmake error: could not create named generator xxx 5

This is a problem I had, and most of the responses on google were in the form of mailing lists, uggh. Anyway, I did this reading so you don’t have to, first thing to try: Did you spell the generator correctly? type cmake with no arguments, see if your generator is listed, if so, are  you spelling it exactly? Try again. If you have messed around with this for awhile, you might need to del cmakecache.txt file. Hope this helps someone else when they have the issue. And remember kiddies, don’t count on people spelling their directions correctly.


New C++ library with Happy Ending

Hi, Having a problem with <insert problem in c++ here>, don’t fret, as I have written an awesome lib that will solve your problem in 1 line of code, here’s a tasty code sample: MyLib::MySpecialValue solved = MyLib::solveYourProblem(ProblemData); All you need to do is download my project, and use <insert obscure replacement for make here> then type:  obscuremake all and you are done! Don’t have obscuremake? No problem, just download and install and you’ll be off an running! [While source is downloading...] Welcome to ObscureMake.sourceforge.net This page updated sufficiently long ago to make you think twice about using this tool. ObscureMake is a new, improved form of make that is nothing even like make, using a special Fortran/COBOL hybrid language called Fortrol, you create your app in a ridiculously non-standard directory structure, and obscuremake does the rest, finding all of your randomly hidden include and inline files. ObscureMake is a complete waste of time, if you are about to download a project using obscuremake, please reconsider, by the time you download the project to compile and this tool, you’ll spend hours trying to figure out how to get it to run, once you do get it to run, it will spew out a bunch of nonsensical errors, which you will then spend 2 days tracking down trying to correct, finally, the project will compile, but once you include it in your current project, it won’t compile anymore because of some random error or collision. To be honest, you are better off writing it yourself.