command line option --version now works
[swftools.git] / src / swfdump.c
1 /* swfdump.c\r
2    Shows the structure of a swf file\r
3 \r
4    Part of the swftools package.\r
5    \r
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>\r
7 \r
8    This file is distributed under the GPL, see file COPYING for details */\r
9 \r
10 #include <stdio.h>\r
11 #include <fcntl.h>\r
12 #include "../lib/rfxswf.h"\r
13 #include "../lib/args.h"\r
14 \r
15 char * filename = 0;\r
16 \r
17 /* idtab stores the ids which are defined in the file. This allows us\r
18    to detect errors in the file. (i.e. ids which are defined more than \r
19    once */\r
20 char idtab[65536];\r
21 \r
22 struct options_t options[] =\r
23 {\r
24  {"v","verbose"},\r
25  {"V","version"},\r
26  {0,0}\r
27 };\r
28 \r
29 \r
30 int args_callback_option(char*name,char*val)\r
31 {\r
32     if(!strcmp(name, "V")) {\r
33         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);\r
34     }\r
35 }\r
36 int args_callback_longoption(char*name,char*val)\r
37 {\r
38     return args_long2shortoption(options, name, val);\r
39 }\r
40 void args_callback_usage(char*name)\r
41 {    \r
42     printf("Usage: %s file.swf\n", name);\r
43 }\r
44 int args_callback_command(char*name,char*val)\r
45 {\r
46     if(filename) {\r
47         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",\r
48                  filename, name);\r
49     }\r
50     filename = name;\r
51     return 0;\r
52 }\r
53   \r
54 int main (int argc,char ** argv)\r
55\r
56     SWF swf;\r
57     TAG*tag;\r
58     int f;\r
59     char prefix[128];\r
60     prefix[0] = 0;\r
61     memset(idtab,0,65536);\r
62 \r
63     processargs(argc, argv);\r
64 \r
65     f = open(filename,O_RDONLY);\r
66 \r
67     if (f<0)\r
68     { \r
69         perror("Couldn't open file: ");\r
70         exit(1);\r
71     }\r
72     if FAILED(ReadSWF(f,&swf))\r
73     { \r
74         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);\r
75         close(f);\r
76         exit(1);\r
77     }\r
78     close(f);\r
79 \r
80     tag = swf.FirstTag;\r
81 \r
82     while(tag) {\r
83         printf("[%02x] %s%s", tag->id, prefix, getTagName(tag));\r
84         if(isDefiningTag(tag)) {\r
85             U16 id = GetDefineID(tag);\r
86             printf(" defines id %04x", id);\r
87             if(idtab[id])\r
88                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);\r
89             idtab[id] = 1;\r
90         }\r
91         else if(tag->id == ST_PLACEOBJECT || \r
92                 tag->id == ST_PLACEOBJECT2) {\r
93             printf(" places id %04x at depth %04x", GetPlaceID(tag), GetDepth(tag));\r
94             if(GetName(tag))\r
95                 printf(" name \"%s\"",GetName(tag));\r
96         }\r
97         else if(tag->id == ST_REMOVEOBJECT) {\r
98             printf(" removes id %04x from depth %04x", GetPlaceID(tag), GetDepth(tag));\r
99         }\r
100         else if(tag->id == ST_REMOVEOBJECT2) {\r
101             printf(" removes object from depth %04x", GetDepth(tag));\r
102         }\r
103         \r
104         printf("\n");\r
105 \r
106         if(tag->id == ST_DEFINESPRITE)\r
107         {\r
108             sprintf(prefix, "         ");\r
109         }\r
110         if(tag->id == ST_END) \r
111         {\r
112             sprintf(prefix, "");\r
113         }\r
114         tag = tag->next;\r
115     }\r
116 \r
117     FreeTags(&swf);\r
118     return 0;\r
119 }\r
120 \r