//------------------------------------------------------------------- // WavePool members. //------------------------------------------------------------------- void WavePool::load(char *f) { myFile myfile; int i; SDL_RWops *memSample = NULL; // read in header if (!myfile.open(f)) error("WavPool not found: ", f); myfile.read(&nsamples, sizeof(long)); // Just quit if empty library. if (nsamples == 0) { myfile.close(); return; } sample = new Sample[nsamples]; myfile.read(sample, nsamples * sizeof(Sample)); fseek(myfile.getFILE(), myfile.getbase(), SEEK_SET); memSample = SDL_RWFromMem(myfile.getMemory(), myfile.getsize()); // Load WAVES. for (i = 0; i < nsamples; i++) { #ifndef NO_SOUND memSample->seek(memSample, (long)sample[i].sample, RW_SEEK_SET); if((sample[i].sample = Mix_LoadWAV_RW(memSample, SDL_FALSE)) == NULL) error("Error loading sample. ", (char *)Mix_GetError()); #endif } SDL_RWclose(memSample); myfile.close(); }
SDL_RWops *SDL_RWFromBundle(Bundle *bundle, const char *filename) { SDL_RWops *rwops; RWOpsBundle * b = calloc(1, sizeof(RWOpsBundle)); b->handle = bundle->handle; for (int i = 0 ; i < bundle->n_files ; ++i) { if (strcmp(bundle->file[i].name, filename) == 0) { b->file = &bundle->file[i]; break; } } if (!b->file) { free(b); return NULL; } rwops = SDL_AllocRW(); if (rwops != NULL) { rwops->seek = bnd_seek; rwops->read = bnd_read; rwops->write = NULL; rwops->close = bnd_close; rwops->hidden.unknown.data1 = b; rwops->seek(rwops, 0, SEEK_SET); } else { free(b); } return rwops; }
void APKProtocol::load() { auto resolvedFilename = this->resolvedFilename(); auto options = _options; auto protocolPrefixPosition = resolvedFilename.find("://"); if (protocolPrefixPosition != std::string::npos) { resolvedFilename = resolvedFilename.substr(protocolPrefixPosition + 3); } if (resolvedFilename.find("./") == 0u) { resolvedFilename = resolvedFilename.substr(2u); } _options = options; SDL_RWops* file = SDL_RWFromFile(resolvedFilename.c_str(), "rb"); auto loader = shared_from_this(); if (file) { if (_options->loadAsynchronously() && AbstractCanvas::defaultCanvas() != nullptr && AbstractCanvas::defaultCanvas()->isWorkerRegistered("apk-protocol")) { file->close(file); auto worker = AbstractCanvas::defaultCanvas()->getWorker("apk-protocol"); auto instance = std::static_pointer_cast<APKProtocol>(shared_from_this()); _workerSlot = worker->message()->connect( [this, instance](async::Worker::Ptr, async::Worker::Message message) { if (message.type == "complete") { data().assign(message.data.begin(), message.data.end()); complete()->execute(instance); _activeInstances.erase(instance); _workerSlot = nullptr; } else if (message.type == "progress") { // FIXME } else if (message.type == "error") { error()->execute(instance); _activeInstances.erase(instance); _workerSlot = nullptr; } } ); auto offset = options->seekingOffset(); auto length = options->seekedLength(); std::stringstream inputStream; inputStream.write(reinterpret_cast<const char*>(&offset), 4u); inputStream.write(reinterpret_cast<const char*>(&length), 4u); inputStream.write(resolvedFilename.data(), resolvedFilename.size()); const auto input = inputStream.str(); worker->start(std::vector<char>(input.begin(), input.end())); } else { auto offset = options->seekingOffset(); auto size = options->seekedLength() > 0 ? options->seekedLength() : file->size(file); _progress->execute(shared_from_this(), 0.0); data().resize(size); file->seek(file, offset, RW_SEEK_SET); file->read(file, (char*) &data()[0], size, 1); file->close(file); _progress->execute(loader, 1.0); _complete->execute(shared_from_this()); } } else { _error->execute(shared_from_this()); } }
int main(int argc, char *argv[]) { SDL_RWops *rwops = NULL; char test_buf[30]; /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); cleanup(); /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */ rwops = SDL_RWFromFile(NULL, NULL); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(NULL, "ab+"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile("something", ""); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile("something", NULL); if (rwops) RWOP_ERR_QUIT(rwops); SDL_Log("test1 OK\n"); /* test 2 : check that inexistent file is not successfully opened/created when required */ /* modes : r, r+ imply that file MUST exist modes : a, a+, w, w+ checks that it succeeds (file may not exists) */ rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */ if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */ if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(FBASENAME2, "wb"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2, "wb+"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2, "ab"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2, "ab+"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); SDL_Log("test2 OK\n"); /* test 3 : creation, writing , reading, seeking, test : w mode, r mode, w+ mode */ rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */ if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops, "1234567890", 10, 1)) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops, "1234567890", 1, 10)) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops, "1234567", 1, 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); /* we are in write only mode */ rwops->close(rwops); rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */ if (!rwops) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20 != rwops->seek(rwops, -7, RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7 != rwops->read(rwops, test_buf, 1, 7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "1234567", 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 10, 100)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2 != rwops->read(rwops, test_buf, 10, 3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "12345678901234567890", 20)) RWOP_ERR_QUIT(rwops); if (0 != rwops->write(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); /* readonly mode */ rwops->close(rwops); /* test 3: same with w+ mode */ rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */ if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops, "1234567890", 10, 1)) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops, "1234567890", 1, 10)) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops, "1234567", 1, 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */ if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20 != rwops->seek(rwops, -7, RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7 != rwops->read(rwops, test_buf, 1, 7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "1234567", 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 10, 100)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2 != rwops->read(rwops, test_buf, 10, 3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "12345678901234567890", 20)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); SDL_Log("test3 OK\n"); /* test 4: same in r+ mode */ rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */ if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops, "1234567890", 10, 1)) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops, "1234567890", 1, 10)) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops, "1234567", 1, 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */ if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20 != rwops->seek(rwops, -7, RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7 != rwops->read(rwops, test_buf, 1, 7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "1234567", 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 10, 100)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2 != rwops->read(rwops, test_buf, 10, 3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "12345678901234567890", 20)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); SDL_Log("test4 OK\n"); /* test5 : append mode */ rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */ if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops, "1234567890", 10, 1)) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops, "1234567890", 1, 10)) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops, "1234567", 1, 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7 != rwops->read(rwops, test_buf, 1, 7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "1234567", 7)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 1, 1)) RWOP_ERR_QUIT(rwops); if (0 != rwops->read(rwops, test_buf, 10, 100)) RWOP_ERR_QUIT(rwops); if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (3 != rwops->read(rwops, test_buf, 10, 3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); SDL_Log("test5 OK\n"); cleanup(); return 0; /* all ok */ }
int main(int argc, char *argv[]) { SDL_RWops *rwops = NULL; char test_buf[30]; cleanup(); rwops = SDL_RWFromFile(NULL,NULL); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(NULL,"ab+"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile("something",""); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile("something",NULL); if (rwops) RWOP_ERR_QUIT(rwops); printf("test1 OK\n"); rwops = SDL_RWFromFile(FBASENAME2,"rb"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(FBASENAME2,"rb+"); if (rwops) RWOP_ERR_QUIT(rwops); rwops = SDL_RWFromFile(FBASENAME2,"wb"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2,"wb+"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2,"ab"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); rwops = SDL_RWFromFile(FBASENAME2,"ab+"); if (!rwops) RWOP_ERR_QUIT(rwops); rwops->close(rwops); unlink(FBASENAME2); printf("test2 OK\n"); rwops = SDL_RWFromFile(FBASENAME1,"wb"); if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); rwops = SDL_RWFromFile(FBASENAME1,"rb"); if (!rwops) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops); if (0!=rwops->write(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); rwops = SDL_RWFromFile(FBASENAME1,"wb+"); if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); printf("test3 OK\n"); rwops = SDL_RWFromFile(FBASENAME1,"rb+"); if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); printf("test4 OK\n"); rwops = SDL_RWFromFile(FBASENAME1,"ab+"); if (!rwops) RWOP_ERR_QUIT(rwops); if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops); if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops); if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops); if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops); if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops); if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops); if (3!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops); if (SDL_memcmp(test_buf,"123456789012345678901234567123",30)) RWOP_ERR_QUIT(rwops); rwops->close(rwops); printf("test5 OK\n"); cleanup(); return 0; }