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;
}

sexta-feira, 22 de outubro de 2010

Layout update

Today I update the blog layout, select a cool space backgroud
and etc. Thanks Google :-)

quinta-feira, 21 de outubro de 2010

The 3 simple sort algorithms

/*    Bubble Sort    */
void
bsort (int ar[], int len)
{
    int control = 1, i, tmp;
    while (control--) {
        for (i = 0; i < len - 1; i++) 
            if (ar[i] > ar[i + 1]) {
                tmp = ar[i], ar[i] = ar[i + 1], ar[i + 1] = tmp;
                control = 1;
            }
    }
}

/*    Inserction Sort    */
void
isort (int ar[], int len)
{
    int i, j, tmp;
    for (i = 1; i < len; i++, ar[j + 1] = tmp) 
        for (j = i -1, tmp = ar[i]; j >= 0 && ar[j] > tmp; j--)
            ar[j + 1] = ar[j];
}

/*    Selection Sort    */
void
ssort (int ar[], int len)
{
    int i, j, lesser, tmp, at;
    for (i = 0; i < len - 1; 
         tmp = ar[i], ar[i] = lesser, ar[at] = tmp, i++)
        for (j = i + 1, lesser = ar[i], at = i; j < len; j++) 
            if (lesser > ar[j]) 
                lesser = ar[j], at = j;
}
tested at http://codepad.org/3Co4Qipn