struct apples { int i; double d; int *ip; int nsimple; int *simple; struct beef cattle; struct beef *kine; unsigned char uc[10]; char pc[10]; union baz luhrmann; unsigned ntwoD; unsigned nptwoD; unsigned long long **twoD; }; /*********** The structure's name is 'apples' The structure has the following members: 'i' is of type 'signed int' 'd' is of type 'double' 'ip' is of type 'pointer to signed int' 'nsimple' is of type 'signed int' 'simple' is of type 'pointer to signed int' 'cattle' is of type 'struct beef' 'kine' is of type 'pointer to struct beef' 'uc' is of type 'array of [10] unsigned char' 'pc' is of type 'array of [10] char' 'luhrmann' is of type 'union baz' 'ntwoD' is of type 'unsigned int' 'nptwoD' is of type 'unsigned int' 'twoD' is of type 'pointer to pointer to unsigned long long int' ***********/ #include #include #include unsigned long read_unsigned(size_t sz, FILE *fp) { unsigned char ch; if (sz == 0) return 0; ch = getc(fp); return (read_unsigned(sz-1, fp) << CHAR_BIT) | ch; } signed long read_signed(size_t sz, FILE *fp) { unsigned long u = read_unsigned(sz, fp); if (u > LONG_MAX) return (u - ULONG_MAX); return u; } int read_string(char **s, FILE *fp) { int ch; int i = 0; int len = 8; char *rtmp; *s = malloc(len); if (*s == NULL) return -3; do { ch = getc(fp); if (ch == EOF) { (*s)[i] = '\0'; return -1; } (*s)[i++] = ch; if (i == len-1) { rtmp = realloc(*s, 2*len); if (rtmp == NULL) { (*s)[i] = '\0'; return -2; } len *= 2; *s = rtmp; len *= 2; } } while (ch != '\0'); return 0; } int write_unsigned(unsigned long u, size_t sz, FILE *fp) { size_t i; for (i=0; i < sz; ++i) { if (putc(u % (UCHAR_MAX+1), fp) == EOF) return -1; u /= (UCHAR_MAX+1); } return 0; } int write_signed(signed long s, size_t sz, FILE *fp) { unsigned long u = s; return write_unsigned(u, sz, fp); } int ReadBufferApples(struct apples *, FILE *); struct apples *ReadDynamicApples(FILE *); int WriteBufferApples(struct apples *, FILE *); /* Buffer output function */ int WriteBufferApples(struct apples *p, FILE *fp) { int i; int j; write_signed(p->i, sizeof p->i, fp); /* Warning: I can't handle type 'double' yet. */ /* Warning: suspicious pointer thing! */ if (p->ip) { putc(0xFF, fp); write_signed(*p->ip, sizeof *p->ip, fp); } else { putc(0x00, fp); } write_signed(p->nsimple, sizeof p->nsimple, fp); for (i=0; i < p->nsimple; ++i) { write_signed(p->simple[i], sizeof p->simple[i], fp); } WriteBufferBeef(&p->cattle, fp); /* Warning: suspicious pointer thing! */ if (p->kine) { putc(0xFF, fp); WriteBufferBeef(&*p->kine, fp); } else { putc(0x00, fp); } for (i=0; i < 10; ++i) { write_unsigned(p->uc[i], sizeof p->uc[i], fp); } for (i=0; p->pc[i]; ++i) putc(p->pc[i], fp); /* Warning: I can't handle type 'union baz' yet. */ write_unsigned(p->ntwoD, sizeof p->ntwoD, fp); write_unsigned(p->nptwoD, sizeof p->nptwoD, fp); for (i=0; i < p->ntwoD; ++i) { for (j=0; j < p->nptwoD; ++j) { write_unsigned(p->twoD[i][j], sizeof p->twoD[i][j], fp); } } return 0; } /* Buffer input function */ int ReadBufferApples(struct apples *p, FILE *fp) { int i; int j; p->i = read_signed(sizeof p->i, fp); /* Warning: I can't handle type 'double' yet. */ /* Warning: pointer 'p->ip' has no corresponding length field 'nip' */ if (getc(fp) != 0x00) { p->ip = malloc(sizeof *p->ip); if (p->ip == NULL) return -3; *p->ip = read_signed(sizeof *p->ip, fp); } else { p->ip = NULL; } p->nsimple = read_signed(sizeof p->nsimple, fp); p->simple = malloc(p->nsimple * sizeof *p->simple); if (p->simple == NULL) return -3; for (i=0; i < p->nsimple; ++i) { p->simple[i] = read_signed(sizeof p->simple[i], fp); } ReadBufferBeef(&p->cattle, fp); /* Warning: pointer 'p->kine' has no corresponding length field 'nkine' */ if (getc(fp) != 0x00) { p->kine = malloc(sizeof *p->kine); if (p->kine == NULL) return -3; ReadBufferBeef(&*p->kine, fp); } else { p->kine = NULL; } for (i=0; i < 10; ++i) { p->uc[i] = read_unsigned(sizeof p->uc[i], fp); } for (i=0; (p->pc[i] = getc(fp)) != '\0'; ++i) continue; /* Warning: I can't handle type 'union baz' yet. */ p->ntwoD = read_unsigned(sizeof p->ntwoD, fp); p->nptwoD = read_unsigned(sizeof p->nptwoD, fp); p->twoD = malloc(p->ntwoD * sizeof *p->twoD); if (p->twoD == NULL) return -3; for (i=0; i < p->ntwoD; ++i) { p->twoD[i] = malloc(p->nptwoD * sizeof *p->twoD[i]); if (p->twoD[i] == NULL) return -3; for (j=0; j < p->nptwoD; ++j) { p->twoD[i][j] = read_unsigned(sizeof p->twoD[i][j], fp); } } return 0; }