void run_script(char* filename) { unsigned short pFile[MAX_FILENAME_SIZE+1]; Bfile_StrToName_ncpy(pFile, (unsigned char*)filename, strlen(filename)+1); int hFile = Bfile_OpenFile_OS(pFile, READWRITE, 0); // Get handle // Check for file existence if(hFile >= 0) // Check if it opened { // Returned no error, file exists, open it int size = Bfile_GetFileSize_OS(hFile); // File exists and has size 'size' // Read file into a buffer if ((unsigned int)size > MAX_TEXTVIEWER_FILESIZE) { Bfile_CloseFile_OS(hFile); printf("Stop: script too big\n"); return; //file too big, return } unsigned char* asrc = (unsigned char*)alloca(size*sizeof(unsigned char)+5); // 5 more bytes to make sure it fits... memset(asrc, size+5, 0); //alloca does not clear the allocated space. Make sure the string is null-terminated this way. int rsize = Bfile_ReadFile_OS(hFile, asrc, size, 0); Bfile_CloseFile_OS(hFile); //we got file contents, close it asrc[rsize]='\0'; execution_in_progress = 1; run((char*)asrc); execution_in_progress = 0; } }
off_t lseek(int fd, off_t offset, int whence){ if(fd>5){ int fileno = toNativeFD(toNativeFD(fd)); switch (whence) { case SEEK_CUR: offset = Bfile_TellFile_OS(fileno) + offset; break; case SEEK_END: offset = Bfile_GetFileSize_OS(fileno); break; case SEEK_SET: break; default: return -1; } // TODO can this fail? Probably. Bfile_SeekFile_OS(fileno, (int)offset); }else return -1; }
void select_strip_script() { if(!is_running_in_strip()) { printf("This function is only available\n"); printf("when running as an eActivity\n"); printf("strip.\n"); return; } textArea text; textElement elem[10]; text.elements = elem; elem[0].text = (char*)"This function lets you run a script when this eActivity strip is opened."; elem[1].newLine = 1; elem[1].text = (char*)"When sharing the eActivity file, it will not be necessary to share any other files - the script is included in the eActivity."; elem[2].newLine = 1; elem[2].text = (char*)"You will be informed if the script is too big to fit inside the eActivity file."; elem[3].newLine = 1; elem[3].text = (char*)"Press EXIT now to continue and select a script."; text.numelements = 4; doTextArea(&text); char filename[MAX_FILENAME_SIZE+1] = ""; if(fileBrowser(filename, (unsigned char*)"*.txt", "Scripts")) { // get the size of the selected script on SMEM. // get free size on the "MCS" of the strip we're running on and see if the script fits unsigned short pFile[MAX_FILENAME_SIZE+1]; Bfile_StrToName_ncpy(pFile, (unsigned char*)filename, strlen(filename)+1); int hFile = Bfile_OpenFile_OS(pFile, READWRITE, 0); // Get handle if(hFile >= 0) // Check if it opened { //opened unsigned int filesize = Bfile_GetFileSize_OS(hFile); //get free size of MCS int MCSmaxspace; int MCScurrentload; int MCSfreespace; MCS_GetState( &MCSmaxspace, &MCScurrentload, &MCSfreespace ); if((int)filesize < MCSfreespace - 50) { // 50 bytes for any headers and the like // fits, copy selected script to MCS unsigned char* scontents = (unsigned char*)alloca(filesize); memset(scontents, filesize, 0); int rsize = Bfile_ReadFile_OS(hFile, scontents, filesize, 0); scontents[rsize]='\0'; // script is now in buffer scontents // write it to the "MCS" int createResult = MCS_CreateDirectory( DIRNAME ); if(createResult != 0) // Check directory existence { // directory already exists, so delete the exiting file that may be there MCSDelVar2(DIRNAME, SCRIPTFILE); } MCSPutVar2(DIRNAME, SCRIPTFILE, rsize, scontents); printf("Script set successfully.\n"); } else { printf("The script is too big to be\n"); printf("included in the eActivity.\n"); } Bfile_CloseFile_OS(hFile); return; // don't get to the error message } } printf("There was a problem setting the script for this strip.\n"); }
void textfileEditor(char* filename, char* basefolder) { int newfile = (filename == NULL); char sText[TEXT_BUFFER_SIZE] = ""; if(!newfile) { newfile = 0; int openerror = 0; int hFile = fileOpen(filename); // Get handle if(hFile >= 0) // Check if it opened { //opened unsigned int filesize = Bfile_GetFileSize_OS(hFile); if(!filesize || filesize > TEXT_BUFFER_SIZE) { openerror = 1; } else { Bfile_ReadFile_OS(hFile, sText, TEXT_BUFFER_SIZE, 0); } Bfile_CloseFile_OS(hFile); } else { openerror = 1; } if(openerror) { //Error opening file, abort AUX_DisplayErrorMessage(0x2B); // Data ERROR return; } } textEdit input; //input.forcetext=1; input.charlimit=TEXT_BUFFER_SIZE; input.buffer = (char*)sText; // calculate checksum so we can check for changes unsigned char origHash[20] = ""; sha1((unsigned char*)sText, strlen(sText), origHash); while(1) { input.key=0; int res = doTextEdit(&input); int exit = 0; switch(res) { case TEXTEDIT_RETURN_EXIT: { exit = 1; unsigned char newHash[20] = ""; sha1((unsigned char*)sText, strlen(sText), newHash); if(!memcmp(origHash, newHash, 20)) return; else { mMsgBoxPush(4); mPrintXY(3, 2, "Save this file?", TEXT_MODE_TRANSPARENT_BACKGROUND, TEXT_COLOR_BLACK); if(closeMsgBox(1, 4)) { // fall through } else { return; } } } case TEXTEDIT_RETURN_CONFIRM: { char newfilename[MAX_FILENAME_SIZE]; unsigned short newfilenameshort[0x10A]; if(newfile) { int backToEditor = 0; SetBackGround(13); drawScreenTitle("Text Editor", "Save file as:"); drawFkeyLabels(0x036F); // < textInput ninput; ninput.forcetext=1; ninput.charlimit=MAX_NAME_SIZE; char nfilename[MAX_NAME_SIZE]; nfilename[0] = 0; ninput.buffer = (char*)nfilename; while(1) { ninput.key = 0; int nres = doTextInput(&ninput); if (nres==INPUT_RETURN_EXIT || (nres==INPUT_RETURN_KEYCODE && ninput.key==KEY_CTRL_F1)) { // user aborted backToEditor = 1; break; } else if (nres==INPUT_RETURN_CONFIRM) { if(stringEndsInG3A(nfilename)) { mMsgBoxPush(4); multiPrintXY(3, 2, "g3a files can't\nbe created by\nan add-in.", TEXT_MODE_TRANSPARENT_BACKGROUND, TEXT_COLOR_BLACK); closeMsgBox(); } else { // create and save file strcpy(newfilename, basefolder); strcat(newfilename, nfilename); Bfile_StrToName_ncpy(newfilenameshort, newfilename, 0x10A); break; } } } if(backToEditor) continue; } else { // delete, then create and save file Bfile_StrToName_ncpy(newfilenameshort, filename, 0x10A); Bfile_DeleteEntry(newfilenameshort); } size_t size = strlen(sText); if(Bfile_CreateEntry_OS(newfilenameshort, CREATEMODE_FILE, &size) < 0) { //create the file // it appears file exists, overwrite? if(overwriteFilePrompt(newfilename)) { Bfile_DeleteEntry(newfilenameshort); Bfile_CreateEntry_OS(newfilenameshort, CREATEMODE_FILE, &size); } else continue; // abort file save so user can discard the file, or type another filename. } int h = Bfile_OpenFile_OS(newfilenameshort, READWRITE, 0); if(h >= 0) { // Still failing? //Write file contents Bfile_WriteFile_OS(h, sText, size); Bfile_CloseFile_OS(h); // clear unsaved changes "flag": sha1((unsigned char*)sText, strlen(sText), origHash); } if(exit) return; } break; } } }