mardi 17 septembre 2013

Coding Style: Detect tabulation and use a given number of space instead.

 

If you need to respect a Coding Style, and that your coding style require tab to be replaced by a given number of space (2 in my case), you may have to check before each commit that the files you have in your working copy doesn’t contain any tab.

To realize that preliminary step, you can use grep.

Run: grep -n -P "\t" *.h && grep -n -P "\t" *.cpp

It will get you the file name and the line number where grep found tabulation.

Now if we want to automatically apply the Coding Styles rules and replace tabs by 2 spaces we can also use a command line.

find ./ -type f -name "*.h" -exec sed -i 's/\t/  /g' {} \;

and

find ./ -type f -name "*.cpp" -exec sed -i 's/\t/  /g' {} \;

The number of space between \t/ and /g has to be the number of space for tab !

Note that grep, find and sed are Linux command, but Windows developer can also use those command if they setup Cygwin.

lundi 16 septembre 2013

C++: Find max top 5 number from 3 array.


Yesterday, I read that blog post where C# developer demonstrate how powerful is LINQ to solve a simple problem.
We have 3 array of Integer  and we want to find the 5 maximum number from those 3 Array.  With LINQ, 7 cascaded operation are enough to do that in 3 line of code.
And I’m wondering how many line I would use in C++ to do the same !
Here is my 1st Draft made in 5min. I use only 5 line of code.
//Headers we need
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int Array1 [] = { 9, 65, 87, 89, 888 };
int Array2 [] = { 1, 13, 33, 49, 921 };
int Array3 [] = { 22, 44, 66, 88, 110 };

vector<int> A1(begin(Array1), end(Array1)); 
A1.insert(end(A1), begin(Array2), end(Array2)); 
A1.insert(end(A1), begin(Array3), end(Array3));
sort(begin(A1), end(A1));
vector<int> max(end(A1)-5, end(A1));

//to output the result
copy(begin(max), end(max), ostream_iterator<int>(cout, " "));




But I would see on Stack Overflow if someone has a better solution.

[UPDATE] : you can found interesting to read answer to my StackOverFlow question.
 C++ and C++11 solution are proposed, taking into account the complexity introduce by the sorting. 

jeudi 12 septembre 2013

Let your hand on the keyboard.

 

I don’t know all the “magic” keyboard shortcut but I always try to learn new shortcut, just because typing and switching between keyboard and mouse is just losing time.

Today I was try to get a shortcut for word/outlook word spell suggestion and find that it’s easy. If the spell checker is enable and that something you just typed appears with the red-underline (saying something is wrong), get back on that word and use MAJ+F10 to open the suggestion list.

Now I can correct a lot of typo mistake on-the-fly without living my keyboard !

C++/Boost Finding files in a folder.

 

Interaction with the file system are not so easy to implement using C++, and that’s where Boost can help you. Boost contain a lot of useful libraries, and FileSystem is one I used the the most.

To build a simple example, let say that we would go through all files in a given folder and remove them with a specific extension '.bak.

First include boost FileSystem header file and I would recommend to use a namespace alias to reduce the length of your code line (typing boost::filesystem:: blahblah every time is too long !)

#include "boost/filesystem.hpp"
namespace fs = boost::filesystem;



Now we have to define the folder path and directory_iterator

fs::path outputFolder(".");
for(fs::directory_iterator it(outputFolder); it != fs::directory_iterator() ; ++it)
{
....
}



Here we iterate over files in the current directory of the program. But using a specific path string you could reach all accessible folder.


Now in the loop, using the valid directory_iterator it, you can do a lot of different things. Below I will test if extension is .bak and if true remove that file.

if(fs::extension(it.path().filename()) == ".bak") 
{
fs::remove(file);
}



Now you can still rely to system command and that you don’t have to del with different FileSystem, under windows just use the system function (<stdio.h>) like : system(‘del *.bak”);


It mainly depend of your project, because adding and using Boost for using only one  of those functionality may be a bad idea.

jeudi 5 septembre 2013

Terminal 2–Don’t assign CTRL+C for Copy

 

Terminal 2 is a powerful tools under windows to run cmd.exe, Cygwin bash and other command prompt. Users can customized their hot-key (hit CTRL+S to open the Settings dialog box). But if you assign the CTRL+C keyboard shortcut for the common “Copy to clipboard” action, you may have trouble in killing programs you run.

Prefer the “copy on select” behavior and re-assign the copy action to another shortcut (i.e. SHIFT+CTRL+C).

After that change, you run program and stop them with CTRL+C as in the classic cmd.exe (windows prompt)

mercredi 4 septembre 2013

C++11: Combining lambda and smart pointer to handle create/release API

I use a library in which several class use create/release pattern for memory and resources management. it means that there is no public constructor (ctor) and destructor (dtor).

Example:

class FileInterface
{
public:
virtual ~FileInterface() {}

virtual bool isValid() = 0 ;
virtual void release() = 0;
};

class RealFile : public FileInterface
{
public:
static int createFileInterface(const std::string& filename, FileInterface*& pFileInst)
{
try {
pFileInst = new RealFile(filename);
} catch (...){
return -1;
}
return 0;
}
virtual bool isValid() { return (m_pFile != NULL);}
virtual void release() { delete this;}

protected:
RealFile(const std::string& filename)
: m_pFile(NULL)
{
m_pFile = fopen(filename.c_str(), "wb");
if(m_pFile == NULL) {
throw std::runtime_error("error while opening file.");
}
}
~RealFile() {
std::cout << "DTOR" << std::endl;
fclose(m_pFile);
}
private:
FILE* m_pFile;
};





To use that kind of class you have to deal with the release by yourself. it means that if you have several return path or a complex exception management, you have to put a call to the release function everywhere (with additional check for  not null).

FileInterface* pFile = nullptr;
int ret = RealFile::createFileInterface("test.bin", pFile);
if( ..... )
{
....
std::cout << "isValid = " << pFile->isValid() << std::endl;
pFile->release();
return ...;
}
else
{
....
if(pFile != nullptr)
pFile->release();
return ...;
}



That’s why I like the C++ smart pointer, at allocation you define the custom deleter and when the smart pointer instance goes out-of the scope, everything will be free.

auto smartDeleter = [](FileInterface* ptr){ptr->release();};
auto smartAllocator = [](const std::string& filename) -> FileInterface* {
FileInterface* pFile = nullptr;
int ret = RealFile::createFileInterface(filename, pFile);
if (ret != 0) return nullptr;
else return pFile;
};

std::unique_ptr<FileInterface, decltype(smartDeleter)> smartFile(smartAllocator("test.bin"));
std::cout << "isValid = " << smartFile->isValid() << std::endl;



Now we can use the smartFile instance as a pointer on a FileInterface and let the life management responsibility to the unique_ptr.

IE–“Toolbars and extension management” (StExBar missing)

 

Sometime I don’t understand how Microsoft organize & manage the relation between the different program running under Windows (Seven) ?

I had an Explorer shell extension call StExBar and today after an update and the restart (as usual), it disappear and the in Explorer Menu (View –>ToolBar) I found it but it was grey and un-clickable !

After some Googling, I found some discussion in which users had the same issue. In fact depending of the version (x86 or x64) of the plugin or extension you use (the StExBar in my case) you can Enable/disable them through the x86 or the x64 version of Internet Explorer (a.k.a IE).

Open the right version of IE, ALT+T (Menu “Tools”) –> “Internet Options”

image

Go into “Programs'” and click ”Manage Add-ons”

image

It’s definitely not the place where I had search by myself to found enable/disable button for shell extension used only by Explorer.exe. I’m wondering in why those settings are in IE….