File listing command under windows

I got this simple request from someone I know. He wants to list all the files in a specific folder, copy them to a text file, so he can send these to someone else for processing.

Well, this is very easy. We just use the dir command for that, and then we redirect it to a file. For example, lets list all the jpeg files:

dir *.jpg > my_pictures.txt

However, We might have pictures in many formats. So, we would like to include these as well:

dir *.jpg *.png *.jpeg > my_pic.txt

That will do the trick. Note, however, that some header information is still included. We only want the file names, nothing else. So, we use the /b (bare – no headers) switch to achieve this goal:

dir /b *.jpg *.png *.jpeg > my_pic.txt

Now this is a serious progress. But… Lets say that our files are not listed in a single directory. Lets say we have some sub directories that we like to search as well. No problem! we will add the /s (sub directory) switch:

dir /b/s *.jpg *.png *.jpeg > my_pic.txt

That’s All! we can now list any Items in this folder and any folder inside it into a text file. if that is a big file, I suggest compressing it.

Processing XML with C++ under ubuntu with xerces

Setting up

  • apt-get this: ¬†apt-get install libxerces-c3.1 libxmltooling-dev -y
  • Project linking libraries: /usr/lib/x86_64-linux-gnu/libxerces-c.so (path might vary, use locate to find out)

After setting up everything, first thing is to generate XML data that we can port to a file and a string. the file is for storage and for us to read from in the second part, the string is to pass the data around.

First thing: initializing Xerces:

before any xerces method is called, XMLPlatformUtils::Initialize(); must be executed. At the end of your program, don’t forget to call XMLPlatformUtils::Terminate();. both are defined in xercesc/util/PlatformUtils.hpp.

The XML “factory”

First thing in creating DOM documents, is to initialize the key object, the DOMImplementation.This is done like this:

DOMImplementation *pImplement = nullptr;
pImplement = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));

To create a document, Xerces requires us to create a document type first.

DOMDocumentType* pDoctype = nullptr;
pDoctype=pImplement->createDocumentType(XMLString::transcode("xml"),0,0);
DOMDocument * pDoc = pImplement->createDocument(XMLString::transcode("xml"),

XMLString::transcode("xml"),pDoctype);

Sidebar: theXMLString

The XML string is a utf16 string. after creating it, you have to release it or it will leak memory! I’m not doing this here to get a cleaner code, but it must be done. there are many ways to handle this, and I will show one of them later

From this point, the creating of elements is very easy and done by calling

DOMDocument::createElement(XMLString). Again, elements returned here, must be release.

To add a child element to a parent, use DOMElement::appendChild(DOMElement * child)

To extract the xml data to a string we use the DOMLSSerializer class. Like many others, this class is also retained from the DOMImplementation class:

DOMImplementation *pImplement = nullptr;
pImplement = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));
pSerializer=pImplement->createLSSerializer();

(of course, if you already have an instance you don’t need to create a new one!)

Sidebar: nullptr is a new type that is a part of the C11 standard. If you can’t compile with it, you can replace it with a regular NULL

To extract the whole document to a string we need to root element and not the document itself. Here is how to do that:

string data=XMLString::transcode(pSerializer->writeToString(pRoot));

Fixing the XMLString memory leaks:

As I mentioned, there are many options to solve this problem. Here is one:

Define a buffer to hold the data, for example:XMLCh buffer[100];

Now, you can use this buffer with the XMLString::transcode method. This method usually create a new string (that you have to release!) but when used like this: XMLString::transcode(“user”,buffer,99); it will only copy the standard string to the XMLCh buffer, saving you all the releasing problems. Seeing this, you are probably thinking of macro or encapsulating class. If you re familiar with them, I strongly recommend that you use one of them!

 

Xerces API here: http://xerces.apache.org/xerces-c/apiDocs-3/classes.html

Developping linux mysql application under ubuntu

apt-get this:

apt-get install libmysqlcppconn5 libmysqlcppconn-dev libmysqlclient-dev -y

Note here that you need to check the version of libmysqlcppconn. It might be higher then 5.

Include this library in your linker code:libmysqlcppconn.so.

To create a connection:

  1. create a driver
  2. call the drivers connect method (it might throw an error, so use a try catch block).

includes and name spaces:

#include<mysql_connection.h>
#include<mysql_driver.h>
#include<cppconn/statement.h>
using namespace sql::mysql;

You don’t have to use name spaces obviously, but this will usually make things easier.

create the driver:MySQL_Driver * p_driver = sql::mysql::get_driver_instance();
Then you can use it to connect with the connect method. Sample connection strings can be found here: http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-query.html.

creating the statement object:

p_stmt=p_connection->createStatement(); The important thing here is to remember to include the right headers. many of them are under cppconn, including the statement (<cppconn/statement.h>) and RecordSet(<cppconn/resultset.h>).

using the statement object:

ResultSet * set = p_stmt->executeQuery(“select username from users_tbl”);

The statement has many more options to look at. also, you should be looking at the prepare statement object(cppconn/prepared_statement.h).

Getting values from the record set

The first value from the record set can be fetched with the first() method.

To get the rest of the data use the next() method. Each of these return boolean that state the result.

To get a value from Result set use one of the getXXXXX(index column). methods. Note that the column index starts with 1.