File: anim.c

package info (click to toggle)
tuxpuck 0.8.2-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 880 kB
  • sloc: ansic: 3,317; makefile: 108
file content (101 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* anim.c - Copyright (C) 2001-2002 Jacob Kroon, see COPYING for details */

#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>

void errorc(char *msg)
{
  fprintf(stderr, "%s\n", msg);
  exit(1);
}

void errorcc(char *msg1, char *msg2)
{
  fprintf(stderr, "%s%s\n", msg1, msg2);
  exit(1);
}

void write_file(FILE * out, char *filename)
{
  FILE *in = NULL;
  struct stat theStat;
  uint8_t *data;
  uint32_t size;

  if ((in = fopen(filename, "rb")) == NULL)
    errorcc("Couldn't open file, ", filename);
  stat(filename, &theStat);
  data = (uint8_t *) malloc(theStat.st_size);
  size = theStat.st_size;
  fwrite(&size, sizeof(uint32_t), 1, out);
  if (fread(data, theStat.st_size, 1, in) != 1)
    errorcc("Error reading from file, ", filename);
  fwrite(data, theStat.st_size, 1, out);
  free(data);
  fclose(in);
}

int main(int argc, char **argv)
{
  FILE *in = NULL, *out = NULL;
  char buffer1[100];
  char buffer2[100];
  char *ptr;
  uint8_t nbrOfFrames, nbrOfAnimations;
  uint32_t uint32;
  int j = 0, i = 0;

  if (argc != 3)
    errorc("Usage : anim <infile> <outfile>");
  if ((in = fopen(argv[1], "rb")) == NULL)
    errorcc("Couldn't open file for reading: ", argv[1]);
  if ((out = fopen(argv[2], "wb")) == NULL)
    errorcc("Couldn't open file for writing: ", argv[2]);
  if (fscanf(in, "NbrOfFrames: %d\n", &uint32) != 1)
    errorc("Wrong number of frames!");
  nbrOfFrames = (uint8_t) uint32;
  fwrite(&nbrOfFrames, 1, 1, out);
  ptr = strrchr(argv[1], '/');
  if (ptr)
    argv[1][strlen(argv[1]) - strlen(ptr) + 1] = 0;
  else
    argv[1][0] = 0;
  for (i = 0; i < nbrOfFrames; i++) {
    if (fscanf(in, "%s\n", buffer1) != 1)
      errorc("Couldnt find image name!");
    sprintf(buffer2, "%s%s", argv[1], buffer1);
    write_file(out, buffer2);
  }
  if (fscanf(in, "NbrOfAnimations: %d\n", &uint32) != 1)
    errorc("Wrong number of animations!");
  nbrOfAnimations = (uint8_t) uint32;
  fwrite(&nbrOfAnimations, 1, 1, out);
  for (i = 0; i < nbrOfAnimations; i++) {
    uint32_t n;
    uint8_t n2;

    if (fscanf(in, "%d\n", &n) != 1)
      errorc("Couldnt read number of frames in animation!");
    n2 = (uint8_t) n;
    fwrite(&n2, 1, 1, out);
    for (j = 0; j < n2; j++) {
      uint32_t frame;
      uint32_t time;
      uint8_t frame2;
      uint16_t time2;

      if (fscanf(in, "%d %d\n", &frame, &time) != 2)
	errorc("Error reading frames");
      frame2 = (uint8_t) frame;
      time2 = (uint16_t) time;
      fwrite(&frame2, 1, 1, out);
      fwrite(&time2, sizeof(time2), 1, out);
    }
  }
  fclose(in);
  fclose(out);
  return 0;
}