Posts Tagged ‘c++’
* Qt Remove Directory and Its Contents
Posted on June 8th, 2010 by John. Filed under programming.
When dealing with directories, Qt has a large number of functions to make manipulating them easy. However, it does not include a way to delete a non-empty directory. This little omission is easily solved.
Following is a recursive function that will delete a directory along with all of it’s contents. This will delete depth first. Meaning it will recurse into sub-directories and only start deleting once the directory has no sub-directories. Changing QDir::DirsFirst to QDir::DirsLast will change this into a breadth first search.
fileutils.h
#ifndef FILEUTILS_H #define FILEUTILS_H #include <QString> class FileUtils { public: static bool removeDir(const QString &dirName); }; #endif // FILEUTILS_H
fileutils.cpp
#include <QDir> #include <QFile> #include <QFileInfo> #include <QFileInfoList> #include "fileutils.h" /*! Delete a directory along with all of its contents. \param dirName Path of directory to remove. \return true on success; false on error. */ bool FileUtils::removeDir(const QString &dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir()) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; }
* C++ Derived Classes and Object Destruction
Posted on May 29th, 2010 by John. Filed under programming.
While working on lebookread I realized that the the destructor for my reader classes would never be called. lebook read has a base class (FormatReader) that exports all of the necessary functionality for use by applications using the library. All of the readers are a subclass of FormatReader. The library will find the appropriate reader for the specified ebook create a reader object and return it as a FormatReader pointer.
When you are dealing with a pointer p of type base that points to an object of type derived you need to take special care. To have the destruction of p call the derived and base destructor the base class must have a virtual destructor. Otherwise only the base class’s destructor will be called. This is one of those little things to look out for when dealing with C++.
Here is some example code to demonstrate the above.
main.cpp
#include <iostream> #include "base.h" #include "deriv.h" using namespace std; int main(int argc, char** argv) { cout << "Base *b = new Base();" << endl; cout << "delete b;" << endl; Base *b = new Base(); cout << " --- " << endl; delete b; cout << " --- " << endl; cout << "b destroyed" << endl; cout << endl; cout << "Deriv *d = new Deriv();" << endl; cout << "delete d" << endl; Deriv *d = new Deriv(); cout << " --- " << endl; delete d; cout << " --- " << endl; cout << "d destroyed" << endl; cout << endl; cout << "Base *c = new Deriv();" << endl; cout << "delete c" << endl; Base *c = new Deriv(); cout << " --- " << endl; delete c; cout << " --- " << endl; cout << "c destroyed" << endl; return 0; }
#ifndef BASE_H #define BASE_H class Base { public: ~Base(); }; #endif /* BASE_H */
base.cpp
#include <iostream> #include "base.h" using namespace std; Base::~Base() { cout << "base dest" << endl; }
deriv.h
#ifndef DERIV_H #define DERIV_H #include "base.h" class Deriv : public Base { public: ~Deriv(); }; #endif /* DERIV_H */
deriv.cpp
#include <iostream> #include "deriv.h" using namespace std; Deriv::~Deriv() { cout << "deriv dest" << endl; }
The output of this will be:
$ ./a.out Base *b = new Base(); delete b; --- base dest --- b destroyed Deriv *d = new Deriv(); delete d --- deriv dest base dest --- d destroyed Base *c = new Deriv(); delete c --- base dest --- c destroyed
Notice that when destroying c only the Base’s destructor is called. To fix this and have both Base and Deriv’s destructors called just make Base’s destructor virtual.
#ifndef BASE_H #define BASE_H class Base { public: virtual ~Base(); }; #endif /* BASE_H */
This simple change will cause the output to become:
$ ./a.out Base *b = new Base(); delete b; --- base dest --- b destroyed Deriv *d = new Deriv(); delete d --- deriv dest base dest --- d destroyed Base *c = new Deriv(); delete c --- deriv dest base dest --- c destroyed
Now when destroying c the destructor for both Base and Deriv are called.
To compile
$ g++ base.cpp deriv.cpp main.cpp* lebookread
Posted on May 16th, 2010 by John. Filed under lebookread, programming.
I have been taking a short break from blogging again. The pressure at work has only increased and is eating into a lot of my time. I haven’t been motivated to work on personal projects because well they are work. However, this has recently changed a bit.
I’ve started a Qt based library for reading ebooks in a generic manner. It is called lebookread! It is it’s early stages. So far I have it supporting epub, palmdoc pdb, ztxt pdb, tcr, and rb files. I plan to support ereader pdb, mobi, and plucker files in the near future.
The main goal of this project is to make reading ebooks easy for Qt based projects. I’ve chose to write the library in C++. This is also my first attempt at writing a library and it shows. I hope that it will be used by Sigil.
The real motivation of writing lebook read is I really want a good light weight ebook reader. The current offering have issues. I want something that is a bit more advanced in it’s rendering than FBReader. I also didn’t want anything with as large a dependency list as calibre. So, I plan on using lebookread to write my own ebook viewer.
* Sending WM_DELETE_WINDOW client messages
Posted on November 8th, 2009 by John. Filed under KDocker, programming.
In my X11 Intercept Window Close Event post I left out one very important piece of information. How to actually close the embedded window after intercepting the WM_DELETE_WINDOW message. The best thing to do is send our own WM_DELETE_WINDOW message to the embedded window. This will keep the embedded window embedded until it actually closes. Meaning any question dialogs that might stop the window from closing (Gedit asking to save for instance) can be answered and it keeps the window embedded if the user decides not to close.
In the previous post we overrode the WM_DELETE_WINDOW message. All we need to do is send it on to the embedded window. This will keep it embedded until it actually closes. Sending a this message entails setting the type to ClinetMessage, message type to the WM_PROTOCOLS Atom, the first l data value to the WM_DELETE_WINDOW Atom, and the second l data value to CurrentTime. The format of course if 32. Send the message to the embedded window and it will try to close.
#include <QX11Info> #include <X11/XLib.h> Display *display = QX11Info::display(); XEvent ev; memset(&ev, 0, sizeof (ev)); ev.xclient.type = ClientMessage; ev.xclient.window = window; ev.xclient.message_type = XInternAtom(display, "WM_PROTOCOLS", true); ev.xclient.format = 32; ev.xclient.data.l[0] = XInternAtom(display, "WM_DELETE_WINDOW", false); ev.xclient.data.l[1] = CurrentTime; XSendEvent(display, window, False, NoEventMask, &ev);
* X11 Intercept Window Close Event
Posted on November 1st, 2009 by John. Filed under KDocker, programming.
***Note Nov, 8 2009: a few additions have been made to the code samples to make them more complete. Specifically subscribing to X11 events.
A feature request for KDocker was made a few days ago for docking when closed. Basically the person wants the window to iconify when the X button on the window decoration is clicked. It’s something I’ve been thinking about for quite some time but it’s not the easiest feature to implement. Girish thankfully pointed me onto the right path when he suggested looking into using a QX11EmbedContainer. I’ve gotten it working and it will be in the 4.3 release.
There isn’t a whole lot of documentation out there on achieving this so I’m going to detail how I’ve implemented the feature in KDocker. Do be awhare that there are a few short comings. Versions <= 4.2 will iconify all windows associated with the docked one. Meaning Gimp and Audacious (XMMS) which have multiple windows will not have the extra windows iconify when the main one is. Also, decorationless windows like audacious (possibly Google Chrome) are no longer movable by clicking on the top of the window. Alt+Left Mouse Click still works and is currently the only way to move them.
The basic overview of the implementation is: Take the user started application window and get info about it (title, size, location, window decorations …). Create a container window. Use XEmbed to put the window into the container. Set the container properties to those of the window. Intercept the WM_DELETE_WINDOW message. Forward other changes like title and icon from the window to the container.
Getting info about the window requires Xlib calls. I’m using the following to get the minimum size, current size, position, width, height and decorations. window is the window id for the application window. The decoration code requires mwmutil.h which can be found in libmotif and LessTif.
Display *display = QX11Info::display(); XSizeHints sizeHint; long dummy; Window root; int x, y; unsigned int width, height, border, depth; XGetWMNormalHints(display, window, &sizeHint, &dummy); XGetGeometry(display, window, &root, &x, &y, &width, &height, &border, &depth); Atom wm_hints_atom = XInternAtom(display, _XA_MOTIF_WM_HINTS, false); unsigned char *wm_data; Atom wm_type; int wm_format; unsigned long wm_nitems, wm_bytes_after; XGetWindowProperty(display, window, wm_hints_atom, 0, sizeof (MotifWmHints) / sizeof (long), false, AnyPropertyType, &wm_type, &wm_format, &wm_nitems, &wm_bytes_after, &wm_data);
Next create the container. I’m using QX11EmbedContainer because it is is a Qt provided XEmbed container widget.
QX11EmbedContainer *container = new QX11EmbedContainer(); container->embedClient(window); container->show();
We need to tell X that we want to receive certain events about the container and embedded window. If we don’t set the appropriate masks the events we are interested in will never be sent to our X11EventFilter.
long mask_container = StructureNotifyMask | PropertyChangeMask | VisibilityChangeMask | FocusChangeMask; long mask_embed = PropertyChangeMask; XWindowAttributes attr_container; XWindowAttributes attr_embed; XGetWindowAttributes(display, m_container->winId(), &attr_container); XGetWindowAttributes(display, window, &attr_embed); if ((attr_container.your_event_mask & mask_container) != mask_container)) { XSelectInput(display, m_container->winId(), attr_container.your_event_mask | mask_container); } if ((attr_container.your_event_mask & mask_embed) != mask_embed)) { XSelectInput(display, window, attr_embed.your_event_mask | mask_embed); }
Now we want to register WM_DELETE_WINDOW so that instead of closing it will be directed to us as a client message. We will check for the message in the X11EventFilter and take appropriate action. This will only work because we have reparented the window by embedding it into the container we control. Simply setting this on the window will not work.
Atom wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", False); XSetWMProtocols(display, container->winId(), &wm_delete, 1);
Take all the info we obtained from the window and apply it to our container. This way it will look and behave the same as if it was not embedded.
m_container->setMinimumSize(m_sizeHint.min_width, m_sizeHint.min_height); m_container->setGeometry(x, y, width, height); if (wm_type == None) { MotifWmHints hints; memset(&hints, 0, sizeof (hints)); hints.flags = MWM_HINTS_DECORATIONS; hints.decorations = MWM_DECOR_ALL; XChangeProperty(display, m_container->winId(), wm_hints_atom, wm_hints_atom, 32, PropModeReplace, (unsigned char *) & hints, sizeof (MotifWmHints) / sizeof (long)); } else { MotifWmHints *hints; hints = (MotifWmHints *) wm_data; if (!(hints->flags & MWM_HINTS_DECORATIONS)) { hints->flags |= MWM_HINTS_DECORATIONS; hints->decorations = 0; } XChangeProperty(display, m_container->winId(), wm_hints_atom, wm_hints_atom, 32, PropModeReplace, (unsigned char *) hints, sizeof (MotifWmHints) / sizeof (long)); } XFree(wm_data);
Thats it for constructing the window but there are a few important pieces we still need to worry about. Within QApplication’s x11EventFilter we need to processes some events.
bool x11EventFilter(XEvent *ev) { XAnyEvent *event = (XAnyEvent *) ev; if (event->window == m_container->winId()) { if (ev->type == ClientMessage && (ulong) ev->xclient.data.l[0] == XInternAtom(QX11Info::display(), "WM_DELETE_WINDOW", False)) { if (m_iconifyOnClose) { iconifyWindow(); } else { close(); } return true; } } if (event->window == window) { if (event->type == PropertyNotify) { return propertyChangeEvent(((XPropertyEvent *) event)->atom); } } return false } bool propertyChangeEvent(Atom property) { Display *display = QX11Info::display(); static Atom WM_NAME = XInternAtom(display, "WM_NAME", True); if (property == WM_NAME) { updateTitle(); return true; } } void updateTitle() { Display *display = QX11Info::display(); char *windowName = 0; QString title; XFetchName(display, window, &windowName); title = windowName; if (windowName) { XFree(windowName); } container->setWindowTitle(title); }
* KDocker 4.2 released
Posted on September 27th, 2009 by John. Filed under KDocker.
This release includes bug fixes as usual. Some new features: bash completion, iconify on focus lost option and Italian translation thanks to Alessio Cassibba. There is also a small change to the behavior when activating the tray icon. If the window is not visible it will become active and if it is active it will iconify. You can get it at the launchpad page.
Somehow I forget to mention the 4.1 release last week… I don’t think I’m going to be getting into the habit of making a release each week but there is still more I have planned. Mostly refactoring, and not to much in the way of new features.
* KDocker 4.0
Posted on September 16th, 2009 by John. Filed under KDocker.
Recently I’ve become the maintainer of the KDocker project. KDocker is a Qt application what allows you dock any application into the system tray. It currently supports any X Windows system. What I’ve done for the 4.0 release is, move the project to launchpad (Girish, the creator of the project is locked out of the Source Forge page) and port/re-write the entire app to use Qt 4.
The port/re-write to Qt 4 is complete and I’ve released it. The version has jumped from 1.3 to 4.0 to better illustrate the the severity of this change. Also, it takes it to a similar version scheme to Qt and KDE.
There are a few things to note about this release and KDocker in general. For this release session management and auto starting have been removed. This is mainly because modern desktop environments support both of these very well. Also, I believe that environments that do not support this are better of using a dedicated application instead of having the functionality rolled into a docker. Another point to note is KDocker 4.0 as well as the older version are pure Qt and Xlib applications. They do not depend on KDE.
The new project location is at https://launchpad.net/kdocker and you can download the 4.0 release at https://launchpad.net/kdocker/+download.
* eBook Adding Empty Lines At End of File
Posted on December 22nd, 2008 by John. Filed under programming.
Continuing my work to clean up my eBooks I’ve written another little tool to help. I like for my eBooks to have two blank lines at the end of the file.
The only major caveat of this one is it assumes Unix end of lines. Meaning a single \n character. In order for this to work correctly use of the dos2unix tool is necessary for files that use a different new line format.
fix_end_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Ensures that there are 3 newline characters at the end of the file (two blank lines after the last of the text). This assumes Unix \n line characters. Please use dos2unix before running to ensure that the end of line characters are correct. */ #include <QFile> #include <QString> #include <QTextStream> int main(int argc, char **argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // We want to see what the last 3 characters are at the end of the file. ioStream.seek(ebook.size() - 3); content = ioStream.read(3); // Move to the end of the file because we want to add newlines (\n's) to // the end. ioStream.seek(ebook.size()); // We want 3 newline (\n) characters at the end of the file. Add them until // they total 3. for (int i = 0; i < (3 - content.count("\n")); i++) { ioStream << "\n" << flush; } ebook.close(); return 0; }
* eBook Paragraph Formating
Posted on December 21st, 2008 by John. Filed under programming.
Today I wrote two simple programs to help me clean up my ebooks. I prefer to keep my ebook collection as plain text files with paragraphs separated by a blank line. The first program reflows the paragraphs to put each on a single line. The second removes extraneous whitespace from the file.
The reflow is the more intensive of the two. I ran it on the largest ebook I have, Project Gutenberg’s War and Peace by Leo Tolstoy. The file is 3.1 MB.
Time to run: 7m35.494s.
Memory usage: 13.1 MB according to gnome-system-monitor.
Right now I’m loading the entire book into memory and using QStrings to work on it. Memory usage is about 4.5 x the size of the book. Thankfully plain text ebooks are fairly small. Later I’m going to look into optimizing it for size and hopefully speed.
Without further ado here are the two. They are MIT licensed and use the Qt tool kit.
fix_paragraphs_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Reflows txt file ebook paragraphs. Paragraphs should be separated by a blank line. Takes paragraphs that have hard breaks and puts all lines onto a single line. For Example: INPUT This is a multi line paragraph. It comprises a few lines but has hard breaks. Now for the second borken apart paragraph. OUTPUT This is a multi line paragraph. It comprises a few lines but has hard breaks. Now for the second broken apart paragraph. */ #include <QFile> #include <QRegExp> #include <QString> #include <QTextStream> int main(int argc, char** argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Regular expression to search for broken paragraphs. Works by looking // for char newline char. A proper ebook should have paragraphs separated // by a blank line meaning char newline newline char. QRegExp re("[^\n]\n[^\n]"); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // Read the entire file contents into memory. content = ioStream.readAll(); while (content.contains(re)) { // Remove the newline when there is a match with the regular expression. content = content.replace(content.indexOf(re)+1, 1, " "); } // Truncate the ebook so we don't end up with the original contents after // our modified contents. if (!ebook.resize(0)) { errStream << QObject::tr("Error: Could not truncate file") << endl; return 1; } // Store the modified content back on disk. ioStream.seek(0); ioStream << content; ebook.close(); return 0; }
remove_extra_whitespace_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Removes extraneous whitespace in a txt file ebook. This will remove every '\t', '\v', '\f', '\r', and will replace multiple occurrences ' ' with a single one. For Example: INPUT This is a bad line. Now for the second borken line. OUTPUT This is a bad line. Now for the second borken line. */ #include #include #include int main(int argc, char **argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // Read every line and remove the extras we don't want. while (!ioStream.atEnd()) { content += ioStream.readLine().simplified() + "\n"; } // Truncate the ebook so we don't end up with the original contents after // our modified contents. if (!ebook.resize(0)) { errStream << QObject::tr("Error: Could not truncate file") << endl; return 1; } // Store the modified content back on disk. ioStream.seek(0); ioStream << content; ebook.close(); return 0; }
Tags
Archives
- August 2010 (3)
- July 2010 (4)
- June 2010 (1)
- May 2010 (2)
- March 2010 (1)
- January 2010 (8)
- December 2009 (5)
- November 2009 (6)
- October 2009 (4)
- September 2009 (2)
- August 2009 (6)
- July 2009 (6)
- June 2009 (4)
- May 2009 (6)
- April 2009 (4)
- March 2009 (2)
- February 2009 (4)
- January 2009 (4)
- December 2008 (7)
- November 2008 (2)