"Fossies" - the Fresh Open Source Software Archive 
Member "netmapr-1.9c/convert.c" (14 Dec 2009, 3742 Bytes) of package /linux/privat/old/netmapr-1.9c.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "convert.c" see the
Fossies "Dox" file reference documentation.
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <SDL/SDL.h>
11 #include <SDL/SDL_keysym.h>
12 #include "convert.h"
13 #include "constants.h"
14
15 char text[BUFLEN];
16
17 int nummaps;
18 int curmap;
19 int modified;
20
21 int main (int argc, char **argv) {
22 if (argc != 3) {
23 printf("convert - converts old netmapr 0.98 maps to new format.\n",argv[0]);
24 printf("usage: %s source dest\n",argv[0]);
25 exit(1);
26 }
27
28 strcpy(text, argv[1]);
29 loadmap();
30
31 strcpy(text, argv[2]);
32 savemap();
33
34 return 0;
35 }
36
37 int loadmap(void) {
38 char filename[BUFLEN];
39 FILE *f;
40 int namelen;
41 int i;
42 /* TODO: validate */
43 strcpy(filename, text);
44
45
46 f = fopen(filename, "rb");
47 if (!f) {
48 printf("ERROR: Cannot open '%s'.\n",filename);
49 return TRUE;
50 }
51
52 /* TODO: free() variables! */
53
54
55 /* read in number maps */
56 fread(&nummaps, sizeof(int), 1, f);
57
58 for (i = 0; i < nummaps; i++) {
59 fread(&map[i].width, sizeof(int), 1, f);
60 fread(&map[i].height, sizeof(int), 1, f);
61 fread(&map[i].bpp, sizeof(int), 1, f);
62 fread(&map[i].bgcol, sizeof(SDL_Color), 1, f);
63 fread(&map[i].boxcol, sizeof(SDL_Color), 1, f);
64 fread(&map[i].numthings, sizeof(int), 1, f);
65 fread(&map[i].numobjects, sizeof(int), 1, f);
66 fread(&map[i].numlinks, sizeof(int), 1, f);
67 fread(&map[i].numtext, sizeof(int), 1, f);
68 map[i].selecteditem = -1;
69 map[i].selecteditemtype = -1;
70 map[i].selectedlinkpoint = -1;
71 map[i].selectedtype = 0;
72 map[i].curobj = -1;
73 map[i].curlink = -1;
74 map[i].curlinkpoint = -1;
75 map[i].curtext = -1;
76 map[i].startx = -1;
77 map[i].starty = -1;
78 map[i].textanchor = -1;
79 strcpy(map[i].text, "");
80
81 fread(&namelen, sizeof(int), 1, f);
82 fread(&map[i].name, (namelen+1) * sizeof(char), 1, f);
83
84 /* read objects */
85 fread(&map[i].thing, sizeof(thing_t), map[i].numthings, f);
86 fread(&map[i].olink, sizeof(link_t), map[i].numlinks, f);
87 fread(&map[i].obj, sizeof(object_t), map[i].numobjects, f);
88 fread(&map[i].textob, sizeof(text_t), map[i].numtext, f);
89 }
90
91 fclose(f);
92
93 curmap = 0;
94
95 modified = FALSE;
96 printf("Successfully loaded map from '%s' (%d maps).\n",filename,nummaps); fflush(stdout);
97
98 return FALSE;
99 }
100
101
102 int savemap(void) {
103 char filename[BUFLEN];
104 FILE *f;
105 int namelen;
106 int i;
107
108 /* TODO: validate */
109 strcpy(filename, text);
110
111
112 printf("Saving map...\n");
113
114
115 if ((f = fopen(filename, "wb")) == NULL) {
116 printf("ERROR: Could not open '%s'.\n",filename);
117 return TRUE;
118 }
119
120 /* dump out numbers of map[curmap].objects */
121 fwrite(&nummaps, sizeof(int), 1, f);
122
123 for (i = 0; i < nummaps; i++) {
124 fwrite(&map[i].width, sizeof(int), 1, f);
125 fwrite(&map[i].height, sizeof(int), 1, f);
126 fwrite(&map[i].bpp, sizeof(int), 1, f);
127 fwrite(&map[i].bgcol, sizeof(SDL_Color), 1, f);
128 fwrite(&map[i].boxcol, sizeof(SDL_Color), 1, f);
129 fwrite(&map[i].numthings, sizeof(int), 1, f);
130 fwrite(&map[i].numobjects, sizeof(int), 1, f);
131 fwrite(&map[i].numlinks, sizeof(int), 1, f);
132 fwrite(&map[i].numtext, sizeof(int), 1, f);
133 /* selecteditem*/
134 /* selecteditemtype*/
135 /* selectedlinkpoint*/
136 /* selectedtype*/
137 /* curobj*/
138 /* curlink*/
139 /* curlinkpoint*/
140 /* curtext*/
141 /* startx,starty */
142 /* textanchor */
143 /* text*/
144 namelen = strlen(map[i].name);
145 fwrite(&namelen, sizeof(int), 1, f);
146 fwrite(&map[i].name, (namelen+1) * sizeof(char), 1, f);
147
148 /* write objects */
149 fwrite(&map[i].thing, sizeof(thing_t), map[i].numthings, f);
150 fwrite(&map[i].olink, sizeof(link_t), map[i].numlinks, f);
151 fwrite(&map[i].obj, sizeof(mapobject_t), map[i].numobjects, f);
152 fwrite(&map[i].textob, sizeof(text_t), map[i].numtext, f);
153 }
154
155 fclose(f);
156
157
158 modified = FALSE;
159 printf("Successfully saved map to '%s'.\n",filename);
160
161
162 return 0;
163 }