Exemplo n.º 1
0
int main(int argc, char** argv) {
  SList* sl;
  int val;

  sl = NewSList(10);
  for (;;) {
    if (scanf("%d", &val) != 1) {
      break;
    }
    AddToSList((void*) val, val, sl);
  }
  ApplySList(sl, printval, 0);
  printf("\n\nDebug out:\n\n");
  print_slist(sl);
  return 0;
} // main()
Exemplo n.º 2
0
Arquivo: atobm.c Projeto: aosm/X11
static void
doit (FILE *fp, 
      char *filename, 
      char *chars, 
      int xhot, int yhot, 
      char *name)
{
    int i, j;
    int last_character;
    char buf[BUFSIZ];
    char *cp, *newline;
    int width = 0, height = 0;
    int len;
    int removespace = (((isascii(chars[0]) && isspace(chars[0])) ||
			(isascii(chars[1]) && isspace(chars[1]))) ? 0 : 1);
    int lineno = 0;
    int bytes_per_scanline = 0;
    struct _scan_list {
	int allocated;
	int used;
	unsigned char *scanlines;
	struct _scan_list *next;
    } *head = NULL, *slist = NULL;
    static unsigned char masktable[] = {
	0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
    int padded = 0;
	
#define NTOALLOC 16
#define NewSList() \
	    slist = (struct _scan_list *) calloc (1, sizeof *slist); \
	    if (!slist) { \
		fprintf (stderr, "%s:  unable to allocate scan list\n", \
			 ProgramName); \
		return; \
	    } \
	    slist->allocated = NTOALLOC * bytes_per_scanline; \
	    slist->scanlines = (unsigned char *) calloc(slist->allocated, 1); \
	    if (!slist->scanlines) { \
		fprintf (stderr, "%s:  unable to allocate char array\n", \
			 ProgramName); \
		return; \
	    } \
	    slist->used = 0; \
	    slist->next = NULL; 

    while (1) {
	buf[0] = '\0';
	lineno++;
	if (fgets (buf, sizeof buf, fp) == NULL) break;

	cp = buf;
	if (removespace) {
	    for (cp = buf; *cp && isascii(*cp) && isspace(*cp); cp++) ;
	}
	if (*cp == '\n' || !*cp) continue;  /* empty line */

	newline = strchr(cp, '\n');
	if (!newline) {
	    fprintf (stderr, "%s:  line %d too long.\n",
		     ProgramName, lineno);
	    return;
	}

	if (removespace) {
	    for (; --newline > cp && isascii(*newline) && isspace(*newline); );
	    newline++;
	}

	if (newline == cp + 1) continue;

	*newline = '\0';
	len = strlen (cp);

	if (width == 0) {
	    width = len;
	    padded = ((width & 7) != 0);
	    bytes_per_scanline = (len + 7) / 8;
	    NewSList ();
	    head = slist;
	} else if (width != len) {
	    fprintf (stderr,
		     "%s:  line %d is %d characters wide instead of %d\n",
		     ProgramName, lineno, len, width);
	    return;
	}

	if (slist->used + 1 >= slist->allocated) {
	    struct _scan_list *old = slist;
	    NewSList ();
	    old->next = slist;
	}

	/* okay, parse the line and stick values into the scanline array */
	for (i = 0; i < width; i++) {
	    int ind = (i & 7);
	    int on = 0;

	    if (cp[i] == chars[1]) {
		on = 1;
	    } else if (cp[i] != chars[0]) {
		fprintf (stderr, "%s:  bad character '%c' on line %d\n",
			 ProgramName, cp[i], lineno);
	    }

	    if (on) slist->scanlines[slist->used] |= masktable[ind];
	    if (ind == 7) slist->used++;
	}
	if (padded) slist->used++;
	height++;
    }

    printf ("#define %s_width %d\n", name, width);
    printf ("#define %s_height %d\n", name, height);
    if (xhot >= 0) printf ("#define %s_x_hot %d\n", name, xhot);
    if (yhot >= 0) printf ("#define %s_y_hot %d\n", name, yhot);
    printf ("\n");
    printf ("static unsigned char %s_bits[] = {\n", name);

    j = 0;
    last_character = height * bytes_per_scanline - 1;
    for (slist = head; slist; slist = slist->next) {
	for (i = 0; i < slist->used; i++) {
	    printf (" 0x%02x", slist->scanlines[i]);
	    if (j != last_character) putchar (',');
	    if ((j % 12) == 11) putchar ('\n');
	    j++;
	}
    }
    printf (" };\n");
    return;
}