* Changes the next cluster in a file */
| 262 | * Changes the next cluster in a file |
| 263 | */ |
| 264 | bool FatSystem::writeNextCluster(unsigned int cluster, unsigned int next, int fat) |
| 265 | { |
| 266 | int bytes = (bits == 32 ? 4 : 2); |
| 267 | #ifndef __WIN__ |
| 268 | char buffer[bytes]; |
| 269 | #else |
| 270 | char *buffer = new char[bytes]; |
| 271 | __try |
| 272 | { |
| 273 | #endif |
| 274 | |
| 275 | if (!validCluster(cluster)) { |
| 276 | throw string("Trying to access a cluster outside bounds"); |
| 277 | } |
| 278 | |
| 279 | int offset = fatStart+fatSize*fat+(bits*cluster)/8; |
| 280 | |
| 281 | if (bits == 12) { |
| 282 | readData(offset, buffer, bytes); |
| 283 | int bit = cluster*bits; |
| 284 | |
| 285 | if (bit%8 != 0) { |
| 286 | buffer[0] = ((next&0x0f)<<4)|(buffer[0]&0x0f); |
| 287 | buffer[1] = (next>>4)&0xff; |
| 288 | } else { |
| 289 | buffer[0] = next&0xff; |
| 290 | buffer[1] = (buffer[1]&0xf0)|((next>>8)&0x0f); |
| 291 | } |
| 292 | } else { |
| 293 | for (int i=0; i<(bits/8); i++) { |
| 294 | buffer[i] = (next>>(8*i))&0xff; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return writeData(offset, buffer, bytes) == bytes; |
| 299 | #ifdef __WIN__ |
| 300 | } |
| 301 | __finally |
| 302 | { |
| 303 | delete[] buffer; |
| 304 | } |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | bool FatSystem::validCluster(unsigned int cluster) |
| 309 | { |