FskErr FskCopyFile(char *src, char *dst) { char buf[4096]; UInt32 amt, amtWrt; int doit=0; FskFileInfo info, infoSrc; FskFile srcFref = NULL, dstFref = NULL; FskErr err = kFskErrNone; if (kFskErrFileNotFound == FskFileGetFileInfo(dst, &info)) { FskAndroidFilesPrintfDebug("dst: %s not found\n", dst); doit = 1; } else if (kFskErrNone == FskFileGetFileInfo(src, &infoSrc)) { if (infoSrc.filesize != info.filesize) { FskAndroidFilesPrintfDebug("src size: %d, dstSize: %d\n", infoSrc.filesize, info.filesize); doit = 1; } } if (doit) { FskAndroidFilesPrintfDebug("Need to copy it over.\n"); err = FskFileOpen(src, kFskFilePermissionReadOnly, &srcFref); BAIL_IF_ERR(err); err = FskFileCreate(dst); BAIL_IF_ERR(err); err = FskFileOpen(dst, kFskFilePermissionReadWrite, &dstFref); BAIL_IF_ERR(err); while (kFskErrNone == err) { err = FskFileRead(srcFref, 4096, buf, &amt); BAIL_IF_ERR(err); if (0 >= amt) break; while (amt) { err = FskFileWrite(dstFref, amt, buf, &amtWrt); BAIL_IF_ERR(err); amt -= amtWrt; } } err = FskFileClose(dstFref); BAIL_IF_ERR(err); dstFref = NULL; err = FskFileClose(srcFref); BAIL_IF_ERR(err); srcFref = NULL; } bail: if(kFskErrEndOfFile == err){ err = kFskErrNone; } if(dstFref) FskFileClose(dstFref); if(srcFref) FskFileClose(srcFref); return err; }
void fxWriteBreakpoints(txMachine* the) { char *breakpoints; FskFile fref = NULL; txSlot *aBreakpoint = NULL; FskInt64 size = 0; FskECMAScriptGetDebug(NULL, &breakpoints, NULL); if ((NULL == breakpoints) || (0 == *breakpoints)) goto bail; FskFileCreate(breakpoints); if (kFskErrNone != FskFileOpen(breakpoints, kFskFilePermissionReadWrite, &fref)) goto bail; if (kFskErrNone != FskFileSetSize(fref, &size)) goto bail; while (true) { char aPath[1024], aLine[32]; aBreakpoint = fxGetNextBreakpoint(the, aBreakpoint, aPath, aLine); if (NULL == aBreakpoint) break; FskFileWrite(fref, FskStrLen(aPath), (void *)aPath, NULL); FskFileWrite(fref, 1, (void *)"\t", NULL); FskFileWrite(fref, FskStrLen(aLine), (void *)aLine, NULL); FskFileWrite(fref, 1, (void *)"\n", NULL); } bail: FskFileClose(fref); }
void unpackAndroid() { char * src, *dst, buf[4096]; int doit=0; UInt32 amt, amtWrt; FskFileInfo info, infoSrc; FskFile srcFref, dstFref; FskErr err; dst = FskStrDoCat(gAndroidCallbacks->getStaticDataDirCB(), "ca-bundle.crt"); src = FskStrDoCat(gAndroidCallbacks->getStaticAppDirCB(), "/res/raw/kinoma.jet/ca-bundle.crt"); if (kFskErrFileNotFound == FskFileGetFileInfo(dst, &info)) { fprintf(stderr, "dst: %s not found\n", dst); doit = 1; } else if (kFskErrNone == FskFileGetFileInfo(src, &infoSrc)) { if (infoSrc.filesize != info.filesize) { fprintf(stderr, "src size: %lld, dstSize: %lld\n", infoSrc.filesize, info.filesize); doit = 1; } } if (doit) { fprintf(stderr, "Need to copy it over.\n"); err = FskFileOpen(src, kFskFilePermissionReadOnly, &srcFref); if (err) fprintf(stderr, "open %s failed %d\n", src, err); err = FskFileCreate(dst); if (err) fprintf(stderr, "create %s failed %d\n", dst, err); err = FskFileOpen(dst, kFskFilePermissionReadWrite, &dstFref); if (err) fprintf(stderr, "open %s failed %d\n", dst, err); while (kFskErrNone == err) { err = FskFileRead(srcFref, 4096, buf, &amt); if (err) fprintf(stderr, "read %s (%lu bytes) failed %d\n", src, amt, err); if (0 >= amt) break; while (amt) { err = FskFileWrite(dstFref, amt, buf, &amtWrt); if (err) fprintf(stderr, "write %s (%lu bytes) failed %d\n", dst, amt, err); amt -= amtWrt; } } err = FskFileClose(dstFref); if (err) fprintf(stderr, "close %s failed %d\n", src, err); err = FskFileClose(srcFref); if (err) fprintf(stderr, "close %s failed %d\n", src, err); } FskMemPtrDispose(src); FskMemPtrDispose(dst); }
FskErr loadGrammar(const char *xsbPath, xsGrammar *theGrammar) { FskErr err; FskFile fref = NULL; UInt32 atom[2]; err = FskFileOpen(xsbPath, kFskFilePermissionReadOnly, &fref); if (err) goto bail; err = FskFileRead(fref, sizeof(atom), atom, NULL); if (err) goto bail; atom[0] = FskEndianU32_BtoN(atom[0]); atom[1] = FskEndianU32_BtoN(atom[1]); if (atom[1] == 'XS11') { SInt32 totalSize = (SInt32)atom[0] - sizeof(atom); while (totalSize > 0) { UInt32 blockSize; char *block; err = FskFileRead(fref, sizeof(atom), atom, NULL); if (err) break; atom[0] = FskEndianU32_BtoN(atom[0]); atom[1] = FskEndianU32_BtoN(atom[1]); totalSize -= atom[0]; blockSize = atom[0] - sizeof(atom); err = FskMemPtrNew(blockSize, &block); if (err) break; err = FskFileRead(fref, blockSize, block, NULL); if (err) break; switch (atom[1]) { case 'SYMB': theGrammar->symbols = block; theGrammar->symbolsSize = blockSize; break; case 'CODE': theGrammar->code = block; theGrammar->codeSize = blockSize; break; default: FskMemPtrDispose(block); err = kFskErrBadData; break; } } } else err = kFskErrBadData; bail: FskFileClose(fref); return err; }
void KPR_system_set_backGesture(xsMachine *the) { FskErr err = kFskErrNone; char* path = NULL; char* pathName = NULL; FskFile file = NULL; char* data = xsToString(xsArg(0)); xsIntegerValue size = FskStrLen(data); #if MINITV pathName = TOUCHD_CONF; // system("/etc/init.d/touchd.sh stop"); #else bailIfError(FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &path)); pathName = FskStrDoCat(path, "touchd.conf"); bailIfNULL(pathName); #endif err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file); if (kFskErrFileNotFound == err) { bailIfError(FskFileCreate(pathName)); err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file); } else { FskInt64 zero = 0; err = FskFileSetSize(file, &zero); } bailIfError(err); bailIfError(FskFileWrite(file, size, data, NULL)); bail: FskFileClose(file); #if MINITV int pipe = open(TOUCH_PIPE, O_WRONLY | O_NONBLOCK); if (pipe >= 0) { char data = 1; write(pipe, &data, 1); close(pipe); } // system("/etc/init.d/touchd.sh start"); #endif if (path) { FskMemPtrDispose(path); FskMemPtrDispose(pathName); } xsThrowIfFskErr(err); }
FskErr KprDBExecuteFile(KprDB self, char* path) { FskErr err = kFskErrNone; FskFileInfo info; FskFile fref = NULL; char *buffer = NULL;; bailIfError(FskFileGetFileInfo(path, &info)); bailIfError(KprMemPtrNew(info.filesize + 1, &buffer)); bailIfError(FskFileOpen(path, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, info.filesize, buffer, NULL)); buffer[info.filesize] = 0; sqlite3_exec(self->data, buffer, NULL, NULL, NULL); bail: KprMemPtrDispose(buffer); FskFileClose(fref); return err; }
void fxOpenProfileFile(txMachine* the, char* theName) { FskFile fref; char *applicationPath = NULL; char *profilePath = NULL; if (the->profileDirectory) profilePath = FskStrDoCat(the->profileDirectory, theName); else { applicationPath = FskEnvironmentDoApply(FskStrDoCopy("[applicationPath]")); profilePath = FskStrDoCat(applicationPath, theName); } FskFileDelete(profilePath); FskFileCreate(profilePath); if (kFskErrNone == FskFileOpen(profilePath, kFskFilePermissionReadWrite, &fref)) the->profileFile = fref; FskMemPtrDispose(profilePath); FskMemPtrDispose(applicationPath); }
void fxLoadModuleJSB(txMachine* the, txString path, txID moduleID) { FskErr err = kFskErrNone; txScript* script = NULL; FskFile fref = NULL; Atom atom; bailIfError(FskMemPtrNewClear(sizeof(txScript), &script)); bailIfError(FskFileOpen(path, kFskFilePermissionReadOnly, &fref)); bailIfError(fxLoadModuleJSBAtom(the, fref, &atom)); bailAssert(atom.atomType == XS_ATOM_BINARY); bailIfError(fxLoadModuleJSBAtom(the, fref, &atom)); bailAssert(atom.atomType == XS_ATOM_VERSION); bailIfError(FskFileRead(fref, sizeof(script->version), script->version, NULL)); bailAssert(script->version[0] == XS_MAJOR_VERSION); bailAssert(script->version[1] == XS_MINOR_VERSION); bailAssert(script->version[2] == XS_PATCH_VERSION); bailAssert(script->version[3] != -1); bailIfError(fxLoadModuleJSBAtom(the, fref, &atom)); bailAssert(atom.atomType == XS_ATOM_SYMBOLS); script->symbolsSize = atom.atomSize - sizeof(atom); bailIfError(FskMemPtrNew(script->symbolsSize, &script->symbolsBuffer)); bailIfError(FskFileRead(fref, script->symbolsSize, script->symbolsBuffer, NULL)); bailIfError(fxLoadModuleJSBAtom(the, fref, &atom)); bailAssert(atom.atomType == XS_ATOM_CODE); script->codeSize = atom.atomSize - sizeof(atom); bailIfError(FskMemPtrNew(script->codeSize, &script->codeBuffer)); bailIfError(FskFileRead(fref, script->codeSize, script->codeBuffer, NULL)); bail: if (fref) FskFileClose(fref); if (err) { if (script) { fxDeleteScript(script); script = NULL; } } fxResolveModule(the, moduleID, script, NULL, NULL); }
void KprFILEServiceInvoke(KprService service UNUSED, KprMessage message) { FskErr err = kFskErrNone; char* path = NULL; FskFileInfo info; FskDirectoryIterator iterator = NULL; char* pathName = NULL; char* sniff = NULL; FskFile fref = NULL; char *name = NULL; if (KprMessageContinue(message)) { if (!message->method || FskStrCompare(message->method, "GET")) { bailIfError(KprURLToPath(message->url, &path)); bailIfError(FskFileGetFileInfo(path, &info)); if (kFskDirectoryItemIsDirectory == info.filetype) { unsigned char buffer[4096]; UInt32 itemType; double date; UInt32 size; xsBeginHostSandboxCode(gFILEService.machine, NULL); { xsVars(3); { xsTry { xsVar(0) = xsGet(xsGlobal, xsID("Files")); xsResult = xsNewInstanceOf(xsArrayPrototype); bailIfError(FskDirectoryIteratorNew(path, &iterator, 0)); while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &itemType)) { if (name[0] == '.') { FskMemPtrDisposeAt(&name); continue; } if (kFskDirectoryItemIsFile == itemType) { pathName = FskStrDoCat(path, name); bailIfError(FskFileGetFileInfo(pathName, &info)); bailIfError(FskFileOpen(pathName, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size)); FskFileClose(fref); fref = NULL; if (kFskErrNone == FskMediaPlayerSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } else if (kFskErrNone == FskImageDecompressSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } FskMemPtrDispose(pathName); pathName = NULL; } xsVar(1) = xsNewInstanceOf(xsObjectPrototype); xsNewHostProperty(xsVar(1), xsID("name"), xsString(name), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("path"), xsString(name), xsDefault, xsDontScript); FskStrCopy((char*)buffer, message->url); FskStrCat((char*)buffer, name); if (kFskDirectoryItemIsDirectory == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("directoryType")); FskStrCat((char*)buffer, "/"); } else if (kFskDirectoryItemIsFile == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("fileType")); if (info.fileModificationDate > info.fileCreationDate) date = info.fileModificationDate * 1000.0; else date = info.fileCreationDate * 1000.0; xsNewHostProperty(xsVar(1), xsID("date"), xsNumber(date), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("size"), xsNumber(info.filesize), xsDefault, xsDontScript); if (sniff) { xsNewHostProperty(xsVar(1), xsID("mime"), xsString(sniff), xsDefault, xsDontScript); FskMemPtrDispose(sniff); sniff = NULL; } } else xsVar(2) = xsGet(xsVar(0), xsID("linkType")); xsNewHostProperty(xsVar(1), xsID("type"), xsVar(2), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("url"), xsString((char*)buffer), xsDefault, xsDontScript); (void)xsCall1(xsResult, xsID("push"), xsVar(1)); FskMemPtrDisposeAt(&name); } xsResult = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("stringify"), xsResult); message->response.body = FskStrDoCopy(xsToString(xsResult)); message->response.size = FskStrLen(message->response.body); KprMessageTransform(message, gFILEService.machine); } xsCatch { } } } xsEndHostSandboxCode(); } else if (kFskDirectoryItemIsFile == info.filetype) {
} xsResult = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("stringify"), xsResult); message->response.body = FskStrDoCopy(xsToString(xsResult)); message->response.size = FskStrLen(message->response.body); KprMessageTransform(message, gFILEService.machine); } xsCatch { } } } xsEndHostSandboxCode(); } else if (kFskDirectoryItemIsFile == info.filetype) { KprStream stream = message->stream; FskInt64 size; bailIfError(FskFileOpen(path, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileGetSize(fref, &size)); bailIfError(FskMemPtrNew(size, &message->response.body)); bailIfError(FskFileRead(fref, size, message->response.body, &message->response.size)); if (stream && stream->dispatch->receive) { (*stream->dispatch->receive)(stream, message, gFILEService.machine, message->response.body, message->response.size); FskMemPtrDisposeAt(&message->response.body); message->response.size = 0; } KprMessageTransform(message, gFILEService.machine); } else err = kFskErrUnimplemented; } else err = kFskErrUnimplemented;
FskErr FskTextFreeTypeInstallFonts(char* fontsPath, char* defaultFont) { FskErr err = kFskErrNone; FskTextEngine fte; char *fromDirectory = NULL; char* fromPath = NULL; FskDirectoryIterator iterator = NULL; char *name = NULL; UInt32 type; #if TARGET_OS_ANDROID char *toDirectory = NULL; char* toPath = NULL; FskFileInfo itemInfo; FskFileMapping map = NULL; unsigned char *data; FskInt64 dataSize; FskFile file = NULL; #endif err = FskTextEngineNew(&fte, NULL); if (err) goto bail; fromDirectory = FskEnvironmentDoApply(FskStrDoCopy(fontsPath)); if (!fromDirectory) { err = kFskErrMemFull; goto bail; } #if TARGET_OS_ANDROID if (!FskStrCompareWithLength(fromDirectory, "/data/app/", 10)) { err = FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, false, NULL, &toDirectory); if (err) goto bail; } #endif err = FskDirectoryIteratorNew(fromDirectory, &iterator, 0); if (err) goto bail; while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &type)) { if (type == kFskDirectoryItemIsFile) { fromPath = FskStrDoCat(fromDirectory, name); if (!fromPath) { err = kFskErrMemFull; goto bail; } FskDebugStr("from %s", fromPath); #if TARGET_OS_ANDROID if (toDirectory) { toPath = FskStrDoCat(toDirectory, name); FskDebugStr("to %s", toPath); if (kFskErrNone != FskFileGetFileInfo(toPath, &itemInfo)) { err = FskFileMap(fromPath, &data, &dataSize, 0, &map); if (err) goto bail; err = FskFileCreate(toPath); if (err) goto bail; err = FskFileOpen(toPath, kFskFilePermissionReadWrite, &file); if (err) goto bail; err = FskFileWrite(file, dataSize, data, NULL); if (err) goto bail; FskFileClose(file); file = NULL; FskFileDisposeMap(map); map = NULL; } FskDebugStr("add %s", toPath); FskTextAddFontFile(fte, toPath); FskMemPtrDisposeAt(&toPath); } else #endif FskTextAddFontFile(fte, fromPath); FskMemPtrDisposeAt(&fromPath); } FskMemPtrDisposeAt(&name); } #if TARGET_OS_ANDROID if (gAndroidCallbacks->getModelInfoCB) { char* osVersion; gAndroidCallbacks->getModelInfoCB(NULL, &osVersion, NULL, NULL, NULL); if ((FskStrStr(osVersion, "android.5") == osVersion) // for KPR applications || (FskStrStr(osVersion, "5.") == osVersion)) { // for tests BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/fonts.xml")); } if ((FskStrStr(osVersion, "android.4") == osVersion) // for KPR applications || (FskStrStr(osVersion, "4.") == osVersion)) { // for tests BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/system_fonts.xml")); err = FskFTAddMapping("/vendor/etc/fallback_fonts.xml"); if (err != kFskErrFileNotFound) BAIL(err); BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/fallback_fonts.xml")); } else { defaultFont = "Droid Sans"; BAIL_IF_ERR(err = FskFTAddMapping(NULL)); } } #endif if (defaultFont) FskTextDefaultFontSet(fte, defaultFont); bail: #if TARGET_OS_ANDROID if (file) FskFileClose(file); if (map) FskFileDisposeMap(map); FskMemPtrDispose(toPath); FskMemPtrDispose(toDirectory); #endif FskMemPtrDispose(name); FskDirectoryIteratorDispose(iterator); FskMemPtrDispose(fromPath); FskMemPtrDispose(fromDirectory); FskTextEngineDispose(fte); return err; }
FskErr mp3ReaderExtract(FskMediaReader reader, void *readerState, FskMediaReaderTrack *track, UInt32 *infoCount, FskMediaReaderSampleInfo *info, unsigned char **dataOut) { mp3Reader state = readerState; FskErr err = kFskErrNone; UInt32 spaceInOutput; unsigned char *data; FskMediaReaderSampleInfo lastInfo = NULL; UInt32 infoCountFree; #if SUPPORT_MP3_LOG_TO_FILE UInt32 outputSize; #endif spaceInOutput = (state->mi.bitrate * 128) / 2; // about half a second if (spaceInOutput < 4096) spaceInOutput = 4096; #if SUPPORT_MP3_LOG_TO_FILE outputSize = spaceInOutput; #endif *track = &state->track; *infoCount = 0; *info = NULL; infoCountFree = state->mi.frequency >> 11; // quick divide by 2048... allocate enough entries for about half a second of frames err = FskMemPtrNew(infoCountFree * sizeof(FskMediaReaderSampleInfoRecord), (FskMemPtr*)(void*)info); if (err) return err; err = FskMemPtrNew(spaceInOutput + kFskDecompressorSlop, (FskMemPtr *)&data); if (err) { FskMemPtrDisposeAt((void **)info); return err; } *dataOut = data; while (0 != infoCountFree) { if (state->hasEndTime && (state->atTime >= state->endTime)) { if (0 == *infoCount ) err = kFskErrEndOfFile; goto bail; } if ((state->readBufferEnd - state->readBufferPtr) < 6) { err = mp3RefillReadBuffer(state, 6); if (err) goto bail; } if ((0xff == state->readBufferPtr[0]) && (0xe0 == (state->readBufferPtr[1] & 0xe0))) { DIDLMusicItemRecord mi; mi.doExtendedParsing = 0; if (true == (state->doHeaderParse)(state->readBufferPtr, &mi)) { if (spaceInOutput < mi.frameLength) goto bail; if ((state->readBufferEnd - state->readBufferPtr) < (SInt32)mi.frameLength) { err = mp3RefillReadBuffer(state, mi.frameLength); if (err) goto bail; } // When starting from a new location, make extra sure we're really sitting on a valid frame, // by validating the next frame. if (state->resync) { if (state->readBufferEnd - (state->readBufferPtr + mi.frameLength) > 6) { DIDLMusicItemRecord mi2; UInt8 *nextFramePtr = state->readBufferPtr + mi.frameLength; if ((0xff != nextFramePtr[0]) || (0xe0 != (nextFramePtr[1] & 0xe0))) { state->readBufferPtr += 1; continue; } mi2.doExtendedParsing = 0; if (false == (state->doHeaderParse)(nextFramePtr, &mi2)) { state->readBufferPtr += 1; continue; } } state->resync = false; } if (kFskAudioFormatAACADTS == mi.codec) { // skip adts header to get to raw aac frame. @@ if frame Count != 1 this isn't going to work mi.frameLength -= 7; state->readBufferPtr += 7; } if ((NULL != lastInfo) && (lastInfo->sampleDuration = mi.samplesPerFrame) && (lastInfo->sampleSize == mi.frameLength)) lastInfo->samples += 1; else { lastInfo = &(*info)[*infoCount]; *infoCount += 1; infoCountFree -= 1; FskMemSet(lastInfo, 0, sizeof(FskMediaReaderSampleInfoRecord)); lastInfo->samples = 1; lastInfo->sampleSize = mi.frameLength; lastInfo->flags = kFskImageFrameTypeSync; lastInfo->decodeTime = state->atTime; lastInfo->sampleDuration = mi.samplesPerFrame; lastInfo->compositionTime = -1; } FskMemMove(data, state->readBufferPtr, mi.frameLength); data += mi.frameLength; state->readBufferPtr += mi.frameLength; spaceInOutput -= mi.frameLength; state->atTime += mi.samplesPerFrame; continue; } } state->readBufferPtr += 1; state->resync = true; } bail: if (kFskErrNone != err) { if ((kFskErrNeedMoreTime == err) || (kFskErrEndOfFile == err)) { if (0 != *infoCount) err = kFskErrNone; } if (kFskErrNone != err) { FskMemPtrDisposeAt((void **)info); FskMemPtrDisposeAt((void **)dataOut); *infoCount = 0; } } #if SUPPORT_MP3_LOG_TO_FILE if (kFskErrNone == err) { if (NULL == gFref) { FskFileDelete("c:/dump.mp3"); FskFileCreate("c:/dump.mp3"); FskFileOpen("c:/dump.mp3", kFskFilePermissionReadWrite, &gFref); } FskFileWrite(gFref, outputSize - spaceInOutput, *dataOut, NULL); } #endif return err; }
void fxWriteAddressAutomatic(txMachine* the) { /* write debugAddress and debugAutomatic to a file... */ #if TARGET_OS_ANDROID char *folder = NULL; char *file = FskStrDoCat(folder, "/sdcard/debug.txt"); { #else char *folder; if (kFskErrNone == FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &folder)) { char *file = FskStrDoCat(folder, "debug.txt"); #endif FskFile fref; FskFileDelete(file); FskFileCreate(file); if (kFskErrNone == FskFileOpen(file, kFskFilePermissionReadWrite, &fref)) { char *data = FskStrDoCat(debugAddress, debugAutomatic ? "\n1\n" : "\n0\n"); if (data) { FskFileWrite(fref, FskStrLen(data), data, NULL); FskFileClose(fref); FskMemPtrDispose(data); } } } } #endif /* !KPR_CONFIG */ void fxReadBreakpoints(txMachine* the) { char *breakpoints; FskMemPtr file = NULL; FskInt64 fileSize; char *p; FskECMAScriptGetDebug(NULL, &breakpoints, NULL); if ((NULL == breakpoints) || (0 == *breakpoints)) goto bail; if (kFskErrNone != FskFileLoad(breakpoints, &file, &fileSize)) goto bail; p = (char *)file; while (true) { char *path = p; char *line = FskStrChr(p, '\t'); char *cr; if (NULL == line) break; cr = FskStrChr(line, '\n'); if (NULL == cr) break; *line++ = 0; *cr = 0; fxSetBreakpoint(the, path, line); p = cr + 1; } bail: FskMemPtrDispose(file); }
char *FskUUIDGetForKey(const char *key) { char *uuidStr; FskUUIDRecord uuid; char *uuidCachePath = NULL, *prefFolder; FskFile fref; if (kFskErrNone == FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &prefFolder)) { uuidCachePath = FskStrDoCat(prefFolder, "fskuuidcache.txt"); FskMemPtrDispose(prefFolder); } // load the cache if (NULL == gUUIDCache) { char *uuidCache; FskInt64 cacheSize; gUUIDCache = FskAssociativeArrayNew(); if ((NULL != uuidCachePath) && (kFskErrNone == FskFileLoad(uuidCachePath, (unsigned char **)(void *)&uuidCache, &cacheSize))) { char *p = uuidCache; while (true) { char *uuid = p; char *mykey = FskStrChr(p, '\t'); char *cr; if (NULL == mykey) break; cr = FskStrChr(mykey, '\n'); if (NULL == cr) break; *mykey++ = 0; *cr = 0; FskAssociativeArrayElementSetString(gUUIDCache, mykey, uuid); p = cr + 1; } FskMemPtrDispose(uuidCache); } } // check the cache uuidStr = FskAssociativeArrayElementGetString(gUUIDCache, key); if (uuidStr) { FskMemPtrDispose(uuidCachePath); return uuidStr; } // not in cache FskUUIDCreate(&uuid); uuidStr = FskUUIDtoString_844412(&uuid); FskAssociativeArrayElementSetString(gUUIDCache, key, uuidStr); FskMemPtrDispose(uuidStr); // flush cache FskFileDelete(uuidCachePath); FskFileCreate(uuidCachePath); if (kFskErrNone == FskFileOpen(uuidCachePath, kFskFilePermissionReadWrite, &fref)) { FskAssociativeArrayIterator iterate = FskAssociativeArrayIteratorNew(gUUIDCache); char tab = '\t', cr = '\n'; while (iterate) { FskFileWrite(fref, FskStrLen(iterate->value), iterate->value, NULL); FskFileWrite(fref, 1, &tab, NULL); FskFileWrite(fref, FskStrLen(iterate->name), iterate->name, NULL); FskFileWrite(fref, 1, &cr, NULL); iterate = FskAssociativeArrayIteratorNext(iterate); } FskFileClose(fref); } FskMemPtrDispose(uuidCachePath); return FskAssociativeArrayElementGetString(gUUIDCache, key); // caller doesn't have to dispose }
FskErr KprSoundLoadSpoolerOpen(FskMediaSpooler spooler, UInt32 permissions) { KprSoundLoadSpoolerRefcon refcon = spooler->refcon; return FskFileOpen(refcon->path, permissions, &refcon->fref); }
FskErr KprSoundLoad(KprSound self) { FskErr err = kFskErrNone; char *path = NULL; FskFile fref = NULL; FskMediaSpoolerRecord spooler; KprSoundLoadSpoolerRefconRecord refcon; FskMediaReader reader = NULL; FskMediaReaderTrack track; FskMediaPropertyValueRecord property; char *audioFormat = NULL; double duration; unsigned char *formatInfo = NULL; UInt32 formatInfoSize; UInt16 numChannels; double sampleRate; double timeScale; FskMediaReaderSampleInfo info = NULL; unsigned char *buffer = NULL; KprSoundFrame frames = NULL; UInt32 frameCount = 0; unsigned char *data = NULL; UInt32 dataSize = 0; if (self->data) goto bail; bailIfError(KprURLToPath(self->url, &path)); if (!self->mime) { unsigned char buffer[4096]; UInt32 size; char *sniff = NULL; bailIfError(FskFileOpen(path, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size)); FskFileClose(fref); fref = NULL; bailIfError(FskMediaPlayerSniffForMIME(buffer, size, NULL, self->url, &sniff)); self->mime = sniff; } FskMemSet(&spooler, 0, sizeof(spooler)); FskMemSet(&refcon, 0, sizeof(refcon)); refcon.path = path; spooler.refcon = &refcon; spooler.doOpen = KprSoundLoadSpoolerOpen; spooler.doClose = KprSoundLoadSpoolerClose; spooler.doRead = KprSoundLoadSpoolerRead; spooler.doGetSize = KprSoundLoadSpoolerGetSize; bailIfError((*FskMediaReaderNewProc)(&reader, self->mime, self->url, &spooler, NULL, NULL)); bailIfError((*FskMediaReaderGetPropertyProc)(reader, kFskMediaPropertyTimeScale, &property)); timeScale = property.value.integer; (*FskMediaReaderGetPropertyProc)(reader, kFskMediaPropertyDuration, &property); duration = property.value.number; (*FskMediaReaderGetTrackProc)(reader, 0, &track); (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyFormat, &property); audioFormat = property.value.str; if (kFskErrNone == (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyFormatInfo, &property)) { formatInfo = property.value.data.data; formatInfoSize = property.value.data.dataSize; } else formatInfoSize = 0; (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyChannelCount, &property); numChannels = property.value.integer; (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertySampleRate, &property); sampleRate = property.value.integer; bailIfError((*FskMediaReaderStartProc)(reader, NULL, NULL)); for (;;) { UInt32 c = 0, i, s; unsigned char *d; err = (*FskMediaReaderExtractProc)(reader, &track, &c, &info, &buffer); if (err == kFskErrEndOfFile) { err = kFskErrNone; break; } if (err != kFskErrNone) goto bail; for (i = 0, d = buffer; i < c; i++, d += s) { s = info[i].sampleSize * info[i].samples; bailIfError(FskMemPtrRealloc((frameCount + 1) * sizeof(KprSoundFrameRecord), &frames)); frames[frameCount].count = info[i].samples; frames[frameCount].frameSize = info[i].sampleSize; frames[frameCount].samplesPerFrame = info[i].sampleDuration; frames[frameCount].frameOffset = dataSize; frameCount++; bailIfError(FskMemPtrRealloc(dataSize + s, &data)); FskMemCopy(data + dataSize, d, s); dataSize += s; } FskMemPtrDispose(buffer); buffer = NULL; FskMemPtrDispose(info); info = NULL; } (*FskMediaReaderStopProc)(reader); self->audioFormat = audioFormat; self->duration = duration; self->formatInfo = formatInfo; self->formatInfoSize = formatInfoSize; self->numChannels = numChannels; self->sampleRate = sampleRate; self->timeScale = timeScale; self->data = data; self->dataSize = dataSize; self->frames = frames; self->frameCount = frameCount; bail: if (err) { FskMemPtrDispose(data); FskMemPtrDispose(frames); FskMemPtrDispose(formatInfo); FskMemPtrDispose(audioFormat); } FskMemPtrDispose(buffer); FskMemPtrDispose(info); (*FskMediaReaderDisposeProc)(reader); FskFileClose(fref); FskMemPtrDispose(path); return err; }