MCPcopy Create free account
hub / github.com/SimHacker/micropolis / getResource

Method getResource

MicropolisCore/src/MicropolisEngine/src/resource.cpp:85–166  ·  view source on GitHub ↗

* Find the resource with the given name and identification. * @param name Name of the resource (a 4 character string) * @param id Identification of the resource. * @return Pointer to the resource. * @bug Function is not safely handling strings. * @bug File handling is not safe across platforms (text-mode may modify data). * @todo What is the point of a \c Quad \a id when we cast it to an \

Source from the content-addressed store, hash-verified

83 * @todo What is the point of a \c Quad \a id when we cast it to an \c int ?
84 */
85Resource *Micropolis::getResource(const char *name, Quad id)
86{
87 Resource *r = resources;
88 char fname[4096];
89
90 while (r != NULL) {
91 if (r->id == id && strncmp(r->name, name, 4) == 0) {
92 return r;
93 }
94 r = r->next;
95 }
96
97 // Resource not found, load it from disk
98
99 // Allocate memory for the resource administration itself
100 r = (Resource *)newPtr(sizeof(Resource));
101 assert(r != NULL);
102
103 /// @bug Not safe!
104 r->name[0] = name[0];
105 r->name[1] = name[1];
106 r->name[2] = name[2];
107 r->name[3] = name[3];
108 r->id = id;
109
110 // Load the file into memory
111
112 /// @bug Not safe (overflow, non-printable chars)
113 sprintf(
114 fname,
115 "%s/%c%c%c%c.%d",
116 resourceDir.c_str(),
117 r->name[0], r->name[1], r->name[2], r->name[3],
118 (int)r->id);
119
120 struct stat st;
121 FILE *fp = NULL;
122
123 if (stat(fname, &st) < 0) { // File cannot be found/loaded
124 goto loadFailed;
125 }
126
127 if (st.st_size == 0) { // File is empty
128 goto loadFailed;
129 }
130
131 r->size = st.st_size;
132 r->buf = (char *)newPtr(r->size);
133 if (r->buf == NULL) { // No memory allocated
134 goto loadFailed;
135 }
136
137 // XXX Opening in text-mode
138 fp = fopen(fname, "r"); // Open file for reading
139 if (fp == NULL) {
140 goto loadFailed;
141 }
142

Callers 9

getAudioFileMethod · 0.80
MainWindowClass · 0.80
makeToolBtnMethod · 0.80
setMapLegendMethod · 0.80
loadTileImagesMethod · 0.80
loadSpriteImageMethod · 0.80
loadImageMethod · 0.80
makeDataBtnMethod · 0.80
loadImageMethod · 0.80

Calls 1

statClass · 0.70

Tested by

no test coverage detected