Automatically remove the tmp – JBOSS

March 20, 2014 Leave a comment

rem Remove tmp folder on startup
set TMP_FOLDER=%JBOSS_HOME%\server\lc_turnkey\tmp
rmdir "%TMP_FOLDER%" /s /q
Categories: Linux

SetSecurityInfo

March 14, 2014 Leave a comment

#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <iostream>
#include <Aclapi.h>
using namespace std;

DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
    cout<<“Inside the thread”<<endl;
    int     Data = 0;
    int     count = 0;
    Data = *((int*)lpParam);
    cout<<“Data is “<<Data<<endl;    
    return 0;
}
int main(void)
{
   HANDLE hThreads;
   int i = 10;
   unsigned threadId;
   SECURITY_ATTRIBUTES  sa = {sizeof(SECURITY_ATTRIBUTES),0,TRUE};
   
   int Data = 1;
   HANDLE hThread = NULL;
   hThread = CreateThread( NULL, 0, Thread_no_1, &Data, 0, NULL);  
   if ( hThread == NULL)
      cout<<“Thread Creation failed!  “<<endl;

    DWORD dwResult = 0;
    dwResult = SetSecurityInfo(hThread, SE_KERNEL_OBJECT, THREAD_QUERY_INFORMATION,0, 0, 0, 0) ;
    cout<<“SetSecurityInfo -> “<<dwResult<<endl;

    if(dwResult != ERROR_SUCCESS) {
        cout<<“SetSecurityInfo  -> “<<dwResult<<endl;
        CloseHandle(hThreads);
    }

    return 0;
}

Categories: C++, Programming, VC++, Win32

Read ImageToAlphanumeric using Tesserract OCR

March 12, 2014 Leave a comment

#include <iostream>
#include <stdio.h>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <algorithm>
#include <string>
using namespace std;

int main()
{

//string str(“TN 07 BQ \n 8109”);
system(“clear”);
char* outText;
cout<<“Entering…”<<endl;
tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
cout<<“Test”<<endl;
printf(“Version is %s\n”,api->Version());
if(api->Init(NULL,”eng”)){
fprintf(stderr,”-Init tess\n”);
exit(1);
}
Pix* image = pixRead(“/home/pi/src/1.png”);
api->SetImage(image);
outText = api->GetUTF8Text();
printf(“OCR output %s”,outText);

api->End();

//char *s = “TN-07 BQ \n 81-09”;
std::string str(outText);
cout<<“The input string is “<<str<<endl;

char cRemoveTokens[] = “.- \r\n”;
for (unsigned int i = 0; i < strlen(cRemoveTokens); ++i)
{
str.erase (std::remove(str.begin(), str.end(), cRemoveTokens[i]), str.end());
}

cout << str << endl;

//Sqlite Part here; Fetch the record from the db, trim the output (vechile no.) and do compare
//delete [] outText;
pixDestroy(&image);
delete api;
}

Categories: C++, Tesserract, ubuntu

Remove special char from a buffer using STL

March 12, 2014 Leave a comment

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
//string str(“TN 07 BQ \n 8109”);

const char *s = “TN 07 BQ \n 8109”;
std::string str(s);
cout<<“The input string is “<<str<<endl;

char cRemoveTokens[] = “- \r\n”;
for (unsigned int i = 0; i < strlen(cRemoveTokens); ++i)
{
str.erase (std::remove(str.begin(), str.end(), cRemoveTokens[i]), str.end());
}

cout << str << endl;
}

Categories: VC++

Can a static variable be declared in a header file?

January 24, 2014 Leave a comment

Yes, We can declare a static variable in a header file but the definition of the static variable also needs to be in the same file. The source file(.cpp) files which includes the header file will have a separate copy of the declared static variable which of course is not the intended purpose of declaring a static variable.

However it is a bad idea of declaring a static variable in a header file. While including the header file containing static variable we need to make sure the header file doesn’t  get included more than once which shall cause the compiler to complain. This can be achieved by putting our static variable in #ifndef and #endif block as mentioned  below.

ifndef __static_var_definitions__
#define __static_var_definitions__

static int istatic;

#endif

Categories: Linux

auto_ptr vs unique_ptr

September 18, 2013 Leave a comment

unique_ptr:
1.The class template unique_ptr <T> manages a pointer to an object of type T. Will usually construct an object of this type by calling new to create an object in the unique_ptr constructor
2. The semantic is as follows “std::unique_ptr<foo> p( new foo(42) );”
3. After calling the constructor, use the object very much like a raw pointer. The * and -> operators work exactly like same as raw pointer also very efficient.
4. unique_ptr go automatic destruction of the contained object when the pointer goes out of scope and even it will be destroyed if your function exits via an exception.
5. Declaring that you are going to have exactly one copy of this pointer.
6. Support “move semantics” to change the ownership of the pointer like std::move(p)
7. It is not copyable, but movable
8. The advantage over auto_ptr is unique_ptr “objects can be stored in containers, work properly when containers are resized or moved, and will still be destroyed when the container is destroyed”.

Example:
unique_ptr<T> myPtr(new T); // Okay
unique_ptr<T> myOtherPtr = myPtr; //Error!, because it need move semantics
unique_ptr<T> myOtherPtr = std::move(myPtr); // Now okay! 🙂

auto_ptr:
1. Deprecated version of RIAA
2. The semantic is “std::unique_ptr<foo> p( new foo(42) );”
3. Couldn’t store auto_ptr objects in containers
4. It is copyable
Example:
auto_ptr<T> myPtr(new T); // Okay
auto_ptr<T> myOtherPtr = myPtr; //Now, the myOtherPtr holds the copy of myPtr. But the myPtr address is NULL.

Here, the ownership is transferred by copyable nature but can’t with unique_ptr

shared_ptr:
1. shared_ptr, allows for multiple pointers to point at a given resource.
2. When the very last shared_ptr to a resource is destroyed, the resource will be deallocated.
3. Example:
shared_ptr<T> myPtr(new T);
shared_ptr<T> myOtherPtr = myPtr;

Here, both myPtr and myOtherPtr points to the same memory location, here it uses reference count to manage the snumber of shared pointers.

For Reference:

http://www.drdobbs.com/cpp/c11-uniqueptr/240002708

Categories: Linux

Member Initialization list Constructor

September 18, 2013 Leave a comment

What is the difference between Initializing and Assignment inside constructor?

Member Initialization:

Foo(int num): bar(num) {};

Member Assignment:

Foo(int num)

{

bar = num;

}

There is a significant difference between Initializing a member using Member initializer list and assigning it an value inside the constructor body.

When initialize fields via Member initializer list the constructors will be called once and the object will be constructed and initialized in one operation.

If use assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.

Cost of Member Initialization = Object Construction

Cost of Member Assignment = Object Construction + Assignment

The latter is actually equivalent to:

Foo(int num) : bar() {bar = num;}

While the former is equivalent to just:

Foo(int num): bar(num){}

For an inbuilt (your code example) or POD class members there is no practical overhead.

When to use Member Initializer list?

You will have(rather forced) to use a Member Initializer list if:

  • Your class has a reference member
  • Your class has a non static const member or
  • Your class member doesn’t have a default constructor or
  • For initialization of base class members or
  • When constructor’s parameter name is same as data member(this is not really a MUST)
·         class MyClass
·         {
·             public:
·                 //Reference member, has to be Initialized in Member Initializer List
·                 int &i;       
·                 int b;
·                 //Non static const member, must be Initialized in Member Initializer List
·                 const int k;  
·          
·             //Constructor's parameter name b is same as class data member 
·             //Other way is to use this->b to refer to data member
·             MyClass(int a, int b, int c):i(a),b(b),k(c)
·             {
·                  //Without Member Initializer
·                  //this->b = b;
·             }
·         };
·          
·         class MyClass2:public MyClass
·         {
·             public:
·                 int p;
·                 int q;
·                 MyClass2(int x,int y,int z,int l,int m):MyClass(x,y,z),p(l),q(m)
·                 {
·                 }
·          
·         };
·          
·         int main()
·         {
·             int x = 10;
·             int y = 20;
·             int z = 30;
·             MyClass obj(x,y,z);
·          
·             int l = 40;
·             int m = 50;
·             MyClass2 obj2(x,y,z,l,m);
·          
·             return 0;

  • MyClass2 doesn’t have a default constructor so it has to be initialized through member initializer list.
  • Base class MyClass does not have a default constructor, So to initialize its member one will need to use Member Initializer List.

Online version of code sample.


Important points to Note while using Member Initializer Lists:

Class Member variables are always initialized in the order in which they are declared in the class.

They are not initialized in the order in which they are specified in the Member Initalizer List.
In short, Member initialization list does not determine the order of initialization.

Given the above it is always a good practice to maintain the same order of members for Member initialization as the order in which they are declared in the class definition. This is because compilers do not warn if the two orders are different but a relatively new user might confuse member Initializer list as the order of initialization and write some code dependent on that.

Categories: Linux