Tuesday, November 15, 2011

Parsing a text file, sorting lexigraphically and writing the output.

Here is code to parse a text file, sort it, and output it to another file.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define MAX_LINE_LEN 1024

typedef struct{
int arraySize;
int arrayElements;
char ** arrayPtr;
} da_string;

void da_string_Init(da_string * dal, int initialSize){
dal->arrayElements = 0;
dal->arraySize = initialSize;
dal->arrayPtr = (char **)malloc(sizeof(char *)*initialSize);
}

void da_string_Destroy(da_string * dal){
    int i;
    for(i = 0; i < dal->arrayElements; i++)
        free( dal->arrayPtr[i]);

    free(dal->arrayPtr);
    dal->arrayPtr = NULL;
dal->arrayElements = 0;
dal->arraySize = 0;
}

void da_string_AddItem(da_string * dal, char * item){
if(dal->arrayElements >= dal->arraySize){
dal->arraySize *= 2;    
dal->arrayPtr = (char ** ) realloc (dal->arrayPtr,  dal->arraySize * 
sizeof(char *));
}
dal->arrayPtr[dal->arrayElements++] = item;
}

char * da_string_GetItem(da_string * dal, int index){
return dal->arrayPtr[index];
}

int compareAscInt (const void * a, const void * b){
    return ( *(int*)a - *(int*)b );
}

int compareDcsString (const void * a, const void * b){
    return (strcmp(*(char **)b,*(char **)a));
}

FILE *inputFile = NULL;
FILE *outputFile = NULL;

da_string stringArray;

char lineBuffer[MAX_LINE_LEN];

int main(int argc, char *argv[]){
    
    if(argc != 3){
        fprintf(stderr, "error argg\n");
        return -1;
    }
    inputFile = fopen(argv[1], "r+"); //stdin is a valid file stream
    outputFile = fopen(argv[2],"w+");
da_string_Init(&stringArray,10);
    
    while ( fgets (lineBuffer, sizeof(lineBuffer), inputFile) != NULL){
        
        char temp[MAX_LINE_LEN];
        
        int i,j;
        for(i = 0,j = -1; i < MAX_LINE_LEN; i++){
            if(lineBuffer[i] == ','){
                j = i; 
                printf("%d\n",j);
            }
        }
        
        if(sscanf(lineBuffer,"%s",temp) == 1){
            
            int dataLen = strlen(temp) + 1;
            if(dataLen <= 1)
                continue;
            
            char  * data = calloc(dataLen,sizeof(char));
            data[dataLen - 1 ] = 0;
            
            strncpy(data,temp,dataLen);
            
            da_string_AddItem(&stringArray,data);
        }

    }
    qsort (stringArray.arrayPtr, stringArray.arrayElements, sizeof(char *), compareDcsString);
    
    int i;
    for(i = stringArray.arrayElements-1; i >= 0; i--){
        fprintf(outputFile,"%s\n",da_string_GetItem(&stringArray,i));
    }
    
    fclose (inputFile);
    fclose (outputFile);
    
da_string_Destroy(&stringArray);

    return 0;
}

 

Saturday, January 1, 2011

Removing Hal From Gentoo

As you may know already hal has been depreciated, its functionally has been merged with udev. If you are having issues with hal or just want it removed then there are some things you need to know.

  1. The first step to removing hal is to update your USE flags to exclude hal (USE="-hal"). 
  2. Then update your system with emerge --update --deep --newuse world
  3. Now you need to determine what packages require hal, these will have to be removed. Execute: emerge --depclean --verbose --pretend hal. 
  4. You will now have a list of programs that you need to unmerge. In my case gnome-mount. I removed it after checking the dependencies with emerge --depclean gnome-mount.
  5. Now once your system is clean of any dependencies to hal, remove it with emerge --unmerge hal.
  6. Congratulations hal is no longer in control. I recommend you re-emerge xf86-input-evdev before restarting X, if you don't you may run into a situation where your mouse and keyboard are not recognized by X.

Tuesday, December 14, 2010

Interfacing Hantek DSO Oscilloscope with Mac OSX

I had recently bought a Hantek DSO oscilloscope. It is a usb based system written for windows mostly, but thankfully the open source community has developed some software to interface with it.

Running it under linux is possible by using OpenHantek , HantekDSO, or DsoSoda.

Before you can use the oscilloscope on OSX you need to download the firmware... this can be done on Linux using dsoextractfw (found in the open source projects above).

You should obtain two files:  DSO2090_firmware.hex  and DSO2090_loader.hex. (I use a DSO 2090 Oscilloscope obviously)

Now download and compile https://github.com/nall/nexys2-osx/tree/master/fxload. This is the fxload equivalent for OSX.

Connect your DSO to your mac device and run the following command:
./fxload-osx -v -t fx2 -I /Volumes/backy/DSO2090_firmware.hex -s /Volumes/backy/DSO2090_loader.hex -D 0x04b4:0x2090

The -D flag requires the venderID:deviceID as an argument. Your Hantek DSO has a vendorID of 0x04b4 and your model number will be your Hantek DSO Oscilloscope version, which in my case is 0x2090.

If everything went well then your DSO will turn on and start flashing a red light.


I am currently in the process of creating code that will extract data from the HantekDSO. If you are interested in writing your own software then get libusb-legacy from ports. Then extract and compile the code from one of the open source projects. I recommend you use Open Hantek since the code is the most up to date.