[ create a new paste ] login | about

Link: http://codepad.org/VUZkPoNu    [ raw code | output | fork ]

C, pasted on May 18:
#include <stdio.h>
#include <stdlib.h>

/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define XMALLOC_ARRAY1 1001
#define XMALLOC_ARRAY2 1002


void release(int ***adjacent, int n) {
  int i;
  if (*adjacent == 0)
    return;
  for (i = 0; i < n; i++)
    xfree((*adjacent)[i], XMALLOC_ARRAY2);
  xfree(*adjacent, XMALLOC_ARRAY1);
}


int allocationAdjacentMap(int ***adjacent, int n) {
  int i, j;
  if ((*adjacent = xmalloc(n * sizeof(int *), XMALLOC_ARRAY1)) == 0)
    return 0;
  for (i = 0; i < n; i++)
    (*adjacent)[i] = 0;
  for (i = 0; i < n; i++) {
    if (((*adjacent)[i] = xmalloc(n * sizeof(int), XMALLOC_ARRAY2)) == 0)
      break;
    for (j = 0; j < n; j++)
      (*adjacent)[i][j] = 0;
  }
  if (i == n)
    return 1;
  release(adjacent, n);
  return 0;
}

#define BUFFLEN 1024
static char buffS[BUFFLEN];

int putInfo2AdjacentMap(FILE *fp, int ***adjacent, int n) {
  int from, to;
  int line;
  line = 0;
  while (line++, fgets(buffS, BUFFLEN, fp) != 0) {
    if (sscanf(buffS, "%d %d", &from, &to) != 2) {
      fprintf(stderr, "input data file, line %d is bad form.\n", line);
      return 0;
    }
    if (from < 0 || from >= n) {
      fprintf(stderr, "input data file, out of range.\n");
      return 0;
    }
    if (to < 0 || to >= n) {
      fprintf(stderr, "input data file, out of range.\n");
      return 0;
    }
    (*adjacent)[from][to] = (*adjacent)[to][from] = 1;
  }
  return 1;
}

void getNodeNum(FILE *fp, int *n) {
  if ((fgets(buffS, BUFFLEN, fp) == 0) || (sscanf(buffS, "%d", n) != 1)) {
    fprintf(stderr, "input data file: first line is not a number.\n");
    *n = 0;
  }
}

void getAdjacentMap_sub(FILE *fp, int ***adjacent, int *n) {
  /* read first line -> n */
  if (getNodeNum(fp, n), *n > 0) {
    if (allocationAdjacentMap(adjacent, *n) > 0) {
      if (putInfo2AdjacentMap(fp, adjacent, *n) > 0) {
        /* do nothing, OK */
      } else {
/* bellow, error handling */
        goto error;
      }
    } else {
      fprintf(stderr, "cannot allocate enough memory.\n");
      *n = 0;
    }
  } else {
    /* bad file format */
  error:
    *n = 0;
  }
}

int getAdjacentMap(char const * const filename, int ***adjacent, int *n) {
  FILE *fp;
  if ((fp = fopen(filename, "r")) != 0) {
    if (getAdjacentMap_sub(fp, adjacent, n), n == 0) {
      fprintf(stderr, "bad file format: %s.\n", filename);
      return 0;
    }
    fclose(fp);
  } else {
    fprintf(stderr, "cannot open the file: %s.\n", filename);
    return 0;
  }
  return 1;
}

#define FILENAME "graph1.txt"
int main() {
  int n;
  int i, j;
  int **adjacent;
  adjacent = 0;
  if (getAdjacentMap(FILENAME, &adjacent, &n)) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++)
        printf("%d ", adjacent[i][j]);
      putchar('\n');
    }
  }
  release(&adjacent, n);
  xmallocdump();
  return 0;
}
/* end */


Output:
1
cannot open the file: graph1.txt.


Create a new paste based on this one


Comments: