fork download
  1. # Create the prototypes for all functions
  2. # on a given C source file
  3. use strict;
  4. use warnings;
  5.  
  6. my @ctypes = qw(
  7. unsigned
  8. signed
  9. long
  10. short
  11. int
  12. float
  13. double
  14. struct
  15. char
  16. static
  17. const
  18. );
  19.  
  20. my @contents = <>;
  21.  
  22. for my $line (0 .. $#contents) {
  23. my $lref = \$contents[$line];
  24. my $prot = '';
  25. for (@ctypes) {
  26. if ($$lref =~ /^$_/) {
  27. my $func = $contents[++$line];
  28. $func =~ s/\w*,/,/g;
  29. $func =~ s/\w*\)/)/g;
  30. $prot = "$$lref $func;";
  31. $prot =~ s/\n//g;
  32. print "$prot\n";
  33. next;
  34. }
  35. }
  36. }
  37.  
  38.  
Success #stdin #stdout 0.01s 4728KB
stdin
/* author: Daniel Hilst */ 

/*=================================*
 *    HASH TABLE IMPLEMENTATION    *
 *=================================*/

#include "hash.h"

static void *
xmalloc (size_t siz)
{
	void *n = malloc (siz);
	if (!n) {
		perror ("malloc");
		exit (EXIT_FAILURE);
	}
	return n;
}

void
init_list (struct list *l)
{
	l->first = l->last = NULL;
	l->siz = 0;
}

int
add_node (struct list *l, const char *k, const char *v)
{
	struct node *n;
	if (!l || !k || !v) {
		fprintf (stderr, "add_node: invalid paraeter\n");
		return -1;
	}
	
	n = xmalloc (sizeof (struct node));
	n->next = NULL;
	n->k = xmalloc (strlen (k) + 1);
	n->v = xmalloc (strlen (v) + 1);
	memcpy (n->k, k, strlen (k) + 1);
	memcpy (n->v, v, strlen (v) + 1);
	if (l->siz == 0) {
		l->first = l->last = n;
	} else {
		l->last->next = n;
		l->last = n;
	} 
	l->siz++;
	return 0;
}

int
del_node (struct list *l, struct node *n, struct node *prior)
{
	if (!l || !n || !prior) {
		fprintf (stderr, "del_node: invalid parameter\n");
		return -1;
	} else if (!l->siz) {
		return 1;
	} else if (l->first == n) {
		l->first = l->first->next;
	} else if (l->last == n) {
		l->last = prior;
		l->last->next = NULL;
	} else {
		prior->next = n->next;
	}		
	free (n->k);
	free (n->v);
	free (n);
	l->siz--;
	return 0;
}

int
free_list (struct list *l)
{
	if (!l) {
		fprintf (stderr, "free_list: invalid parameters\n");
		return -1;
	}
	while (!del_node (l, l->first, NULL))
		;
	return 0;
}

struct node **
search_node (struct list *l, const char *k)
{
	struct node **n = xmalloc (sizeof (struct node) * 2);
	if (!l) {
		fprintf (stderr, "search_node: Invalid parameter\n");
		exit (EXIT_FAILURE);
	}
	n[0] = l->first;
	n[1] = NULL; /* prior node */;
	for (; n[0]; n[1] = n[0], n[0] = n[0]->next) {
		if (!strcmp (n[0]->k, k))
			return n;
	}
	return NULL;
}


char *
seria_node (struct node *n)
{
	char *ser, *p;
	int len;
	len = strlen (n->k) + strlen (n->v) + 2; /* key=value\0*/
	ser = xmalloc (len);
	strncpy (ser, n->k, strlen (n->k) + 1);
	strncat (ser, "=", 2);
	strncat (ser, n->v, strlen (n->v) + 1); 
	return ser;
} 

struct node *
unseria_node (const char *ser)
{
	struct node *n;
	char *eq;
	eq = strchr (ser, '=');
	if (!eq) {
		fprintf (stderr, "unseria_node: Bad pair format\n");
		exit (EXIT_FAILURE);
	}
	*eq++ = '\0';
	n = xmalloc (sizeof (struct node));	
	n->k = xmalloc (strlen (ser) + 1);	
	n->v = xmalloc (strlen (eq) + 1);
	strncpy (n->k, ser, strlen (ser) + 1);
	strncpy (n->v, eq, strlen (eq) + 1);
	return n;
}

int
hash_init (struct hash *h, int siz)
{
	int i;
	if (!h || siz <= 0) {
		fprintf (stderr, "hash_init: invalid parameters\n");
		exit (EXIT_FAILURE);
	}
	h->siz = siz;
	h->vec = xmalloc (siz * sizeof (struct list));
	for (i = 0; i < h->siz; i++) {
		init_list (&h->vec[i]);
	}
	return 0;	
}

unsigned
hashing (const char *k)
{
	unsigned hash = 0;
	for (; *k; k++) 
		hash += *k - 'A';
	return hash;
}	
	
int
hash_set (struct hash *h, const char *k, const char *v)
{
	unsigned indx;
	struct node **n;
	if (!h || !k || !v) {
		fprintf (stderr, "hash_set: invalid parameters\n");
		exit (EXIT_FAILURE);
	}
	indx = hashing (k) % h->siz;
	n = search_node (&h->vec[indx], k);
	if (!n) {
		add_node (&h->vec[indx], k, v);
	} else {
		free (n[0]->v);
		n[0]->v = malloc (strlen (v) + 1);
		strncpy (n[0]->v, v, strlen (v) + 1);
	}
	free (n);
	return 0;
}

char *
hash_get (struct hash *h, const char *k)
{
	unsigned indx;
	struct node **n;
	char *v;
	if (!h || !k) {
		fprintf (stderr, "hash_get: invalid parameters\n");
		exit (EXIT_FAILURE);
	}
	indx = hashing (k) % h->siz;
	n = search_node (&h->vec[indx], k);
	if (!n) {
		return NULL;
	} else {
		v = strdup (n[0]->v);
		free (n);
		return v;
	}
	free (n);
	return NULL;
}

int
hash_save (struct hash *h, const char *file)
{
	FILE *fp;
	int i;
	char *buf;
	struct node *n;
	if (!h || !file) {
		fprintf (stderr, "hash_save: Invalid parameters\n");
		exit (EXIT_FAILURE);
	}
	
	if (!(fp = fopen (file, "w"))) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	for (i = 0; i < h->siz; i++) {
		for (n = h->vec[i].first; n; n = n->next) {
			buf = seria_node (n);
			fwrite (buf, strlen (buf), 1, fp);
			fputc ('\n', fp);
			free (buf);
		}
	}
	fclose (fp);
	return 0;
}

int
hash_load (struct hash *h, const char *file)
{
	int i, len; 
	struct node *n;
	FILE *fp;
	char buf[0x400];
	char eq;
	if (!h || !file) {
		fprintf (stderr, "hash_load: invalid parameters\n");
		exit (EXIT_FAILURE);
	}

	if (!(fp = fopen (file, "r"))) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	while (fgets (buf, 0x400, fp)) {
		buf[strlen (buf) - 1] = '\0';  /* chop the fgets new line */
		n = unseria_node (buf);
		hash_set (h, n->k, n->v);
		free (n);
	}
	fclose (fp);
}


void
hash_dbg (struct hash *h)
{
	int i;
	struct node *n;
	for (i = 0; i < h->siz; i++) {
		printf (">> %05d >>\n", i);
		if (!h->vec[i].siz) {
			puts ("EMPTY LIST");
		} else {
			for (n = h->vec[i].first; n; n = n->next) {
				printf ("node( %10p ) next( %10p ) %s => %s\n", n, n->next, n->k, n->v);
			}
		}
		printf ("<< %05d <<\n", i);
	}
}

stdout
static void * xmalloc (size_t );
int add_node (struct list *, const char *, const char *);
int del_node (struct list *, struct node *, struct node *);
int free_list (struct list *);
struct node ** search_node (struct list *, const char *);
char * seria_node (struct node *);
struct node * unseria_node (const char *);
int hash_init (struct hash *, int );
unsigned hashing (const char *);
int hash_set (struct hash *, const char *, const char *);
char * hash_get (struct hash *, const char *);
int hash_save (struct hash *, const char *);
int hash_load (struct hash *, const char *);