1
0
mirror of git://projects.qi-hardware.com/fped.git synced 2024-11-04 23:32:48 +02:00
fped/util.c
werner 9e7e804d1a - delete.c: added more destructor functions
- deallocate all our data structures on exit (to help find memory leaks, bad
  pointers, and general logic errors)
- fixed memory leak when allocating pad names in instantiation
- added "magic" environment variable FPED_NO_GUI to run fped without 
  initializing Gtk+
- added valgrind wrapper "leakcheck"
- delete.c: destroy() now requires a deletion to exist
- vacate_op: free string expressions
- destroy_obj: free measurement labels
- delete_references: use do_delete_obj so the we don't bump the group number
- delete_frame: delete references after deleting the frame itself, so they end
  up on the stack above the frame and get destroyed first
- do_delete_vec: like above, even though it doesn't matter here



git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5506 99fdad57-331a-0410-800a-d7fa5415bdb3
2009-08-21 08:34:17 +00:00

115 lines
1.9 KiB
C

/*
* util.c - Common utility functions
*
* Written 2009 by Werner Almesberger
* Copyright 2009 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
/* ----- printf buffer allocation ------------------------------------------ */
char *stralloc_vprintf(const char *fmt, va_list ap)
{
va_list aq;
char *buf;
int n;
va_copy(aq, ap);
n = vsnprintf(NULL, 0, fmt, aq);
va_end(aq);
buf = alloc_size(n+1);
vsnprintf(buf, n+1, fmt, ap);
return buf;
}
char *stralloc_printf(const char *fmt, ...)
{
va_list ap;
char *s;
va_start(ap, fmt);
s = stralloc_vprintf(fmt, ap);
va_end(ap);
return s;
}
/* ----- identifier syntax check ------------------------------------------- */
int is_id_char(char c, int first)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
return 1;
if (first)
return 0;
return c >= '0' && c <= '9';
}
int is_id(const char *s)
{
const char *p;
if (!*s)
return 0;
for (p = s; *p; p++)
if (!is_id_char(*p, s == p))
return 0;
return 1;
}
/* ----- unique identifiers ------------------------------------------------ */
static struct unique {
char *s;
struct unique *next;
} *uniques = NULL;
/* @@@ consider using rb trees */
const char *unique(const char *s)
{
struct unique **walk;
for (walk = &uniques; *walk; walk = &(*walk)->next)
if (!strcmp(s, (*walk)->s))
return (*walk)->s;
*walk = alloc_type(struct unique);
(*walk)->s = stralloc(s);
(*walk)->next = NULL;
return (*walk)->s;
}
void unique_cleanup(void)
{
struct unique *next;
while (uniques) {
next = uniques->next;
free(uniques->s);
free(uniques);
uniques = next;
}
}