sexta-feira, 7 de janeiro de 2011

Middleware manager..

Yeap, I got a job. Intern on middleware management area.. I really like it.. I have
close contact with commercial aplications and linux/unix environments... and the warm/pressure of the businesses.. There are nice guys there.. some times looks like an arena.. but with little fat funny guys instead of big muscle losers.. :-) 

by the way.. an tree prototype...


tree.h
#ifndef GKO_TREE_H
#define GKO_TREE_H

#include "stack.h"
#include "ll.h"
#include "common.h"

typedef struct branch {
    char *name;
    char *data;
    ll cont;
    struct branch *next;
} branch;
    
void init_tree (branch *,  char *,  char *) ;
void set_branch (branch *,  char *,  char *);
branch *get_branch (branch *, char *);

#endif


tree.c
#include "tree.h"

/* Initialize a Tree, set the root node */
void
init_tree (branch *t, char *name, char *data) 
{
    cpystr (&t->name, name);
    cpystr (&t->data, data);
    init_ll (&t->cont);
    t->next = NULL;
}

/* Create a new branch or edit an existent one */
void
set_branch (branch *t, char *name, char *data)
{
    lln **node = search_lln (&t->cont, name);
    branch *b;

    if (node) { /* edit branch */
        b = node[0]->data; /* the address of found branch */
        free (b->data);
        cpystr (&b->data, data);
    } else { /* create a brand new branch */
        b = xmalloc (sizeof (branch));
        init_tree (b, name, data);
    }
    
    add_lln (&t->cont, name, b); /* tree contains branch */
}

/* Return the address of a branch */
branch *
get_branch (branch *t, char *name)
{
    lln **node = search_lln (&t->cont, name);
    if (node)
        return node[0]->data; /* return void * don't need casts */
    return NULL;
}

/* Delete a branch an everything below that node */
/* @TODO */
void
del_branch ()
{
}



driver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tree.h"

main ()
{
    branch myt;
    branch *buf;

    init_tree (&myt, "root", "root_data");
    set_branch (&myt, "etc", "etc_data");
    set_branch (&myt, "usr", "usr_data");

    buf = get_branch (&myt, "etc");
    printf ("Searched %s\n", buf->data);
    
    set_branch (buf, "rc.conf", "rc.conf data");
    buf = get_branch (buf, "rc.conf");
    printf ("Searched %s\n", buf->data);

    buf = get_branch (&myt, "usr");
    printf ("User data? %s\n", buf->data); 

    set_branch (&myt, "etc", "etc_edited_data");
    buf = get_branch (&myt, "etc");
    printf ("Searched %s\n", buf->data);

    return 0;
}


MAYBE I work more on this..

segunda-feira, 22 de novembro de 2010

Prototype maker 2.0

There is no patter alignment anymore .. !

# Create the prototypes for all functions
# on a given C source file
use strict;
use warnings;

my @ctypes = qw(
    void
    unsigned 
    signed 
    long 
    short 
    int 
    float 
    double 
    struct 
    char
    static
    const
);

my @contents = <>;

for my $line (0 .. $#contents) {
    my $lref = \$contents[$line];
    my $prot = '';
    for my $type (@ctypes) {
        if ($$lref =~ /^$type/) {
             while ($contents[$line] !~ /\{/) { # seek for {
                $prot .= "$contents[$line++] ";
            }
            chop $prot;            # the last space
            $prot =~ s/\n//go;     # remove LFs 
            $prot =~ s/\w*,/, /go; # remove argument intentifiers
            $prot =~ s/\w*\)/)/go; # remove the last argument 
            $prot .= ';';           # append a semicolon 
            print $prot, "\n";
            next;
        }
    }
}

sábado, 20 de novembro de 2010

Perl script to generate basic prototypes..

Typedefs are not supported and you need to code your
functions with follow patter:
type
function_indetifier (list_of_parameters)
{ ... hereafter makes no difference

usage: cat souce.c | perl prot.pl > prots.txt
then copy and paste where you want...




# Create the prototypes for all functions
# on a given C source file
use strict;
use warnings;

my @ctypes = qw(
    unsigned 
    signed 
    long 
    short 
    int 
    float 
    double 
    struct 
    char
    static
    const
);

my @contents = <>;

for my $line (0 .. $#contents) {
    my $lref = \$contents[$line];
    my $prot = '';
    for (@ctypes) {
        if ($$lref =~ /^$_/) {    
            my $func = $contents[++$line];
            $func =~ s/\w*,/,/g;
            $func =~ s/\w*\)/)/g;
            $prot = "$$lref $func;";
            $prot =~ s/\n//g;
            print "$prot\n";
            next;
        }
    }
}
           
test: http://ideone.com/5tE0F
note: I will coment that hash implementation soon..

quarta-feira, 10 de novembro de 2010

Mini web server in perl (really small han?)

use strict;
use warnings;
use LWP;
use HTTP::Daemon;

# the parameters are 
# the user/group user to restrict permissions, the port (default 80)
# the local addres (default locahost)
# respectivaly
my ($userg, $port, $laddres) = @ARGV;
$userg or die 'You need to suply a nonroot user/group as first parameter'; 
my ($user, $group) = split ('/', $userg);

$port = 80 unless $port;
$laddres = 'localhost' unless $laddres;

my $server = new HTTP::Daemon (
    LocalAddr => $laddres,
    LocalPort => $port,
    Listen => 5,
    Reuse => 1,
) || die $!;

$> = getpwnam $user || die $!; 
$) = getgrnam $group || die $!; 


while (my $con = $server->accept()) {
    my $pid = fork();
    if (!$pid) { 
        my $req = $con->get_request();
        print $req->as_string();
        my $file = $req->url();
        $file =~ s/^.//g;    
        $con->send_file_response($file);
    } elsif ($pid) { next;  
    } else { print STDERR "fork: $!\n" };
}


domingo, 31 de outubro de 2010

Binary Tree made simple

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


struct bt {
    void *data;
    char *key;
    struct bt *right, *left;
};

/* prototypes */
int add_btnode (struct bt **, char *, void *);
void init_bt (struct bt **);
struct bt *search_btnode (struct bt *, char *);
void print_inorder (struct bt *);

int
main (void)
{
    struct bt *root, *p;
    init_bt (&root);
    add_btnode (&root, "Daniel", "C");
    add_btnode (&root, "Bia", "asm");
    add_btnode (&root, "Nelson", "Php");
    p = search_btnode (root, "Bia");
    printf ("Bia is an %s hacker\n", (char *)p->data);
    puts ("\n\nIN ORDER");
    print_inorder (root);
     
    return 0;
}

void
init_bt (struct bt **r)
{
    *r = NULL;
}

int
add_btnode (struct bt **root, char *k, void *dat)
{
    if (!*root) {
        *root = malloc (sizeof (struct bt));
        if (!*root) exit (EXIT_FAILURE);
        (*root)->key = k;
        (*root)->data = dat;
        (*root)->right = (*root)->left = NULL;
        return 0;
    }    
    if (strcmp (k, (*root)->key) > 0)
        add_btnode (&(*root)->right, k, dat);
    else if (strcmp (k, (*root)->key) < 0)
        add_btnode (&(*root)->left, k, dat);
    return -1;
} 

struct bt *
search_btnode (struct bt *root, char *k)
{
    int rval;
    if (!root) return NULL;
    rval = strcmp (k, root->key);
    while (root && rval) {
        if (rval > 0) root = root->right;
        else root = root->left;
        rval = strcmp (k, root->key);
    }
    return root;
}

void
print_inorder (struct bt *root)
{
    if (!root) return;    
        
    print_inorder (root->left);
    printf ("%s => %s\n", root->key, (char *)root->data);
    print_inorder (root->right);
}


Tested at: http://ideone.com/vL1N0

quinta-feira, 28 de outubro de 2010

Testing Linked List against multiple data types

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct instance {
    int a, b;
    float f;
    char str[8];
    int (*p) (const char *);
};

int
main (void)
{
    llist mylist;
    node *bufnode;
    struct search_result sr;
    int i = -1;
    char string[] = "Hello World";
    float myf = 2.5;
    struct instance strc = {1, 2, 3.4, "Five", puts};
    struct instance otherstrc;

    init_llist (&mylist);

    bufnode = init_node ("struct", (char *)&strc, sizeof (struct instance));
    add_node (&mylist, bufnode);

    bufnode = init_node ("int", (char *)&i, sizeof (int));
    add_node (&mylist, bufnode);

    bufnode = init_node ("string", string, strlen ("Hello World") + 1); 
    add_node (&mylist, bufnode);

    bufnode = init_node ("float", (char *)&myf, sizeof (float));
    add_node (&mylist, bufnode);

    sr = search_node (&mylist, "struct");
    memcpy (&otherstrc, sr.n->data, sizeof (struct instance));
    printf ("otherstrc: %d %d %f %s\n", 
        otherstrc.a, otherstrc.b, otherstrc.f , otherstrc.str);
    otherstrc.p("Nice?");
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "int");
    printf ("%d\n", *(int *)sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "float");
    printf ("%f\n", *(float *)sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "string");
    printf ("%s\n", sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    if (! mylist.first)
        printf ("empty\n");
    return 0;
}


Tested here: http://ideone.com/lTlp9

quarta-feira, 27 de outubro de 2010

How to read an unknown lenght line to a string in C

/*===========================*
 *    Reading dinamically    *
 *===========================*/
#include <stdio.h>
#include <stdlib.h>

#define DATALEN BUFSIZ 

struct block {
    char data[DATALEN];
    struct block *next;
};

struct block *
alloc_block (void)
{
    struct block *new = malloc (sizeof (struct block));
    if (!new) {
        perror ("malloc");
        exit (EXIT_FAILURE);
    }
    return new;
}

int
readfrom (char **str, FILE *stream, int finalchar)
{
    struct block *first;
    struct block *ptr;
    struct block *prev;
    struct block *tmp;
    int ch, indx = 0, nblocks = 1, strindx;

    first = ptr = alloc_block();
    ptr->next = NULL;
    /* read and alloc string as a linked list */
    while ((ch = getc(stream)) != finalchar) {
        if (indx == DATALEN) {
            tmp = alloc_block ();
            indx = 0; 
            nblocks++;
            tmp->next = NULL;
            ptr->next = tmp;
            ptr = tmp;
        } else if (indx > DATALEN) {
            fprintf (stderr, "readline error: Index error\n");
            exit (EXIT_FAILURE);
        }
        ptr->data[indx++] = ch;
    }
    ptr->data[indx] = '\0';
    *str = malloc ( (nblocks - 1) * DATALEN + indx );    
    /* run in list freeing it */
    for 
    (ptr = first, strindx = 0; 
     ptr != NULL; 
     prev = ptr, ptr = ptr->next, free (prev)) 
    {
        for 
        (indx = 0; 
         indx < DATALEN && ptr->data[indx] != '\0'; 
         indx++, strindx++) 
        {
            (*str)[strindx] = ptr->data[indx]; 
        }
    }
    (*str)[strindx] = '\0';
    return strindx;
}

        
int
main (void)
{
    char *str;
    int len;
    len = readfrom (&str, stdin, '\n');
    printf ("Your string with len %d is `%s'\n", len, str);
    return 0;
}