Thursday, April 16, 2015

source code of separation of string into substring at specified delimeter in C(Line parsing)

In writing a program , we may reach to a situation where we need to read the string and seperate the string depending upon certain symbol or delimeter. Here I have written a simple program to parse the string with new line character.

Requirement:

let's say I have a string as "this \n is  \n a \n test \n program" and I need to read the string and seperate the string. To acheive this, we have a strtok function included in string.h.

Source code:
#include <stdio.h>
#include <string.h>


int main()
{
	char *token, *remstr=NULL ;
char str[] = " this \n is \n the \n test\n program";
token = strtok_r(str,"\n",&remstr);



        

		while(token != NULL)
		{
		
		    printf("here i=%s\n",token);
		    printf("remstr=%s\n",remstr);

		    token = strtok_r(NULL, "\n", &remstr);
		
		}

		return 0;

}

strtok function takes the string and the seperation symbol and this function scan the seperator and save the seperated sub string at token .  At remstr the remaining string is stored and the loop is repeated until there is substring at the remaining string. The loop is repeated until token has null value. When the last substirng is reached, the strtok function will produce the null value and this null value is the indication of end of string.

Note: At the end of string , the remaining string will store a null value in mac where as in ubuntu/linux the empty string is stored.

similarly we can seperate the string to sub string depending upon different seperator as per our requirement  in the program using strtok or strtok_r function included in string.h header.


In large program , we may come across the situation that we need to accumulate the seperated token or substring in to the array of string  and return the array of substing to the main calling function to perform the specific task for each substring.

for accmulation of substring , I have used the list of glib library.

source code:
#include <stdio.h>
#include <string.h>
#include <glib.h>

char *col_trim_whitespace(char *str)
{
  char *end;

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = 0;

  return str;
}


GSList* line_parser(char *str,GSList* list)
{


        
        char *token, *remstr=NULL ;
 
		token = strtok_r(str,"\n",&remstr);


		while(token != NULL)
		{
			if(token[0] == ' ')
			{

			token = col_trim_whitespace(token);
			if(strcmp(token,"")==0)
		         {
		             token = strtok_r(NULL, "\n", &remstr);
		              continue;
		          }
		    }

		    list = g_slist_append(list, token);
            token = strtok_r(NULL,"\n",&remstr);
            
             


		}


        

		
		return list;


}

int main()
{
	

 int *av,i,j,length;
 i=0;


char str[] = " this";

 GSList* list = NULL;

 
GSList *list1 = line_parser(str,list);
// printf("The list is now %d items long\n", g_slist_length(list));
 length = g_slist_length(list1);
for(int j=0;j<length;j++)printf("string = %s\n",(char *)g_slist_nth(list1,j)->data);
return 0;
}

Thursday, April 2, 2015

creating a GCC shared library in linux and accessing the shared library in C/C++

A library is a file containing compiled code from various object files stuffed into a single file.  A library can be of two types:

1.     Shared Library
2.     Static library

1.   Shared Library:


Shared Libraries are the libraries that can be linked to any program at run-time. They provide a means to use code that can be loaded anywhere in the memory. Once loaded, the shared library code can be used by any number of programs.

 2.   Shared Library Names:


Every shared library has a special name called the “soname”. The soname has the prefix “lib”, the name of the library, the phrase “so'', followed by a period and a version number that is incremented whenever the interface changes .

Eg libtest.so, libtest.so.1.1

3.   Placement in File System:

There are mainly three standard locations in the filesystem where a library can be placed.

·      /lib
·      /usr/lib
·      /usr/local/lib

we can even use the non standatd library location. In that case the path should be added to the LD_LIBRARY_PATH

1.   How to create the Shared library with GCC  in linux


Step 1:Let us suppose a simple code  shared.c
int a(int b)
{
  return b+1;
}

int c(int d)
{
  return a(d)+1;
}




Step2:  compile our library source code in to position independent code(PIC)

          gcc –c  -Wall –Werror –fpic  shared.c

       Here libtest.so is the name of the shared library



Step 3: create a shared library from object file

     gcc  -shared –o libtest.so  shared.o

Step 4: making the library available at runtime using LD_LIBRARY PATH

     export LD_LIBRARY_PATH=/home/username:$LD_LIBRARY_PATH

Here /home/username is the sample path to the shared library. This should be
different as per the location of the shared library

Step 4:  create a  file that uses the shared library name test.c

     
#include 
#include 
#include 

typedef int (*pointer)(int b);


int main()
{
int b,d;
void *lib;
 pointer calc;
  lib=dlopen("libtest.so",RTLD_LAZY);
   if (!lib)
 {
  printf("failed to open libtest.so: %s \n", dlerror());
  exit(1);
 }


  calc= (pointer) dlsym(lib,"a");
  b= calc(2);
  calc= (pointer) dlsym(lib,"c");
  d= calc(2);

  dlclose(lib);


  printf("b is %d d is %d\n",b,d);
  return 0;
}

Here  the above program uses the <dlfcn.h> library to open the shared library  libtest.so , locate the symbol and call the function from shared library.

dlopen() function opens the shared library and dlsym() looks up a symbol in a shared library.