Archive for category Projects

Sqlitemm: C++ wrapper for sqlite3

As part of writing inkbook, I decided to use sqlite3 for data storage. The C/C++ API is actually a C API, and while it is object oriented and rather intuitive, it’s just not C++. Considering that the API is very simple and, in particular, the subset of the API I wanted to use was very simple, I went ahead and wrote a quick wrapper.

Sqlitemm provides a C++ style interface to creation of database connections and statements (as in Connection and Statement are classes). Objects are reference counted using Glib::RefPtr so memory management is a bit easier.

You can find the project in my tracker , but heres an example of it’s usage:

 
#include <sqlitemm.h>
 
int main(int argc, char** argv)
{
    // open a connection to a sqlite database
    Glib::RefPtr<sqlite::Connection> sqlite =
            sqlite::Connection::create("/path/to/db.sqlite");
 
    // we'll reuse this variable for different statements
    Glib::RefPtr<sqlite::Connection> stmt;
 
    // prepare a read statement
    stmt = sqlite->prepare("SELECT * FROM table_a WHERE field_a=?");
 
    // bind a value to one of the parameters
    stmt->bind<int>(1,10);
 
    // execute the select statement
    stmt->step();
 
    // read out the result set
    while(stmt->hasAnotherRow())
    {
        // retrieve the first column of the result set as an integer
        int             field_a = stmt->get<int>(0);
 
        // retrieve the second column of the result set as a string
        std::string     field_b = stmt->get<std::string>(1);
 
        // retrieve the third column of the result set as a double
        double          field_c = stmt->get<double>(2);
 
        // do something
    }
 
    // if we want to reuse the statement we need to call this
    stmt->reset();
 
    // prepare a second statement, note that all memory allocated for the
    // first statement is released here, because the smart pointer is
    // reassigned and the object it points to only has one outstanding reference
    stmt = m_sqlite->prepare("UPDATE table SET field_a=? WHERE field_b=?" );
    stmt->bind<int>(1,20);
    stmt->bind<std::string>(2,"something");
 
    // now actually execute the statement
    stmt->step();
 
    // note that we do not have to explicitly close the connection, when
    // the sqlite variable goes out of scope, the smart pointer will drop it's
    // reference to the underlying Connection object, and the connection
    // object will be destroyed. The databse is closed during the destructor
    // of the Connection object.
    return 0;
}

No Comments

directoryWatch

As I was working on texmake I decided that I didn’t want to figure out what all the possible auxilary output files would be for a latex document. Also, I’m suspecting that it will depend on what packages are included and things. Anyway, I wanted a way to just monitor the build directory and see all the files that were created while running latex. It turns out this is very easy on linux. This is a very simple program which watches a directory, and will print out any files that are created, modified, opened, closed, moved, or deleted. It prints out the file name, followed by a comma, followed by the notification event.

You can find the code in project tracker

No Comments

svg2pdf and svg2eps (convert svg to pdf or eps from the command line)

I’ve been working on a new makefile for my latex projects. The goal is to have single-source builds of dvi, pdf, and xhtml documents. I ran into a problem of generating figures. latex expects eps graphics, pdflatex expects pdf figures, and latexml expects png figures (or will try to generate them). In order to generate documents with make, I need a way to generate eps and pdf figures from svg sources (I usually use inkscape to make my figures). Inkscape can be run from the command line, but I dont want to install inkscape on my server because that will require installing a ton of libraries that don’t make sense to have on a server with no graphical interface.

As it turns out, writing such a command line tool is very easy with librsvg and cairo. Carl Worth of redhat has produced a nice demo of svg2pdf which can be found at freedesktop.org. I cloned his project, create a cmake project out of it, and made a trivial modification to turn it into svg2eps as well.

You can find my code at git://git.cheshirekow.com/svg2x.git and there’s some doxygen documentation Doxygen documentation.

3 Comments