void Save_Config(const char* fn,int n) { /* 存储分页及书签配置 参数说明: fn: 当前打开文件的文件名 n: 已缓存页数 */ char tmp[64]; FONTCHARACTER fname[64]; int handle,size=0; memset(tmp,0,sizeof(tmp)); strncpy(tmp,fn,strlen(fn)-strlen(strrchr(fn,'.'))); // 取文件名部分 strcat(tmp,".cfg"); char_to_font(tmp,fname); Bfile_CreateEntry_OS(fname,1,&size); // 创建 .cfg 文件 handle=Bfile_OpenFile_OS(fname,2); if (handle<=0) return; Bfile_WriteFile_OS(handle,&n,4); // 前 4 字节,写入已缓存页数 Bfile_SeekFile_OS(handle,4); Bfile_WriteFile_OS(handle,bookmark,16); // 4*4 字节,写入书签指向的页数 Bfile_SeekFile_OS(handle,20); Bfile_WriteFile_OS(handle,bytes,n*4); // 4*n 字节,写入页面缓存 Bfile_CloseFile_OS(handle); }
// TODO make ptr, stream restrict for optimization size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) { if (isstdstream(stream)) { if (stream->fileno == 2) { // stderr: serial return fwrite_serial(ptr, size, nitems, stream); } else if (stream->fileno == 1) { // stdout: display return fwrite_term(ptr, size, nitems, stream); } else { // stdin..? } } // TODO this must be able to fail, but how? Bfile_WriteFile_OS(handle_tonative(stream->fileno), ptr, size * nitems); return nitems; }
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; } } }
void input_eval_loop(int isRecording) { char** recHistory = NULL; int curRecHistEntry = 0; if(isRecording) recHistory = (char**)alloca(200); // space for 200 pointers to history entries while (1) { DefineStatusMessage((char*)"", 1, 0, 0); strcpy(expr, (char*)""); printf("\x1e"); dConsoleRedraw(); int res = gets(expr,INPUTBUFLEN); if(res == 2) { dConsolePut("\n"); select_script_and_run(); continue; } if(res == 4) { dConsolePut("\n"); select_strip_script(); continue; } if(res == 3) { dConsolePut("\n"); char buf[100] = ""; sprintf(buf, "prizmUIkeyHandler(%d,%d)", custom_key_to_handle, custom_key_to_handle_modifier); strcpy(expr, (char*)buf); execution_in_progress = 1; run(buf); execution_in_progress = 0; check_do_graph(); if(run_startup_script_again) { run_startup_script_again = 0; run_startup_script(); } continue; } puts(expr); update_cmd_history(expr); dConsoleRedraw(); if(strcmp(expr, "testmode") == 0) { TestMode(1); } else if(strcmp(expr, "meminfo") == 0) { print_mem_info(); } else if(strcmp(expr, "memgc") == 0) { gc(); } else if(strcmp(expr, "record") == 0) { if(!isRecording) script_recorder(); else { // create and save a script. this must be done here, because we used alloca // the "clean" way would be using malloc&free, but on the Prizm the heap is already being heavily used by the Eigenmath core. if(curRecHistEntry == 0) { printf("Nothing to record.\n"); return; } printf("Recording stopped.\n"); printf("Type a name for the script, or\n"); printf("leave empty to discard.\n:"); char inputname[MAX_FILENAME_SIZE+1] = ""; gets(inputname,MAX_FILENAME_SIZE-50); puts(inputname); if(!strlen(inputname)) { // user aborted printf("Recording discarded.\n"); return; } if (aborttimer > 0) { Timer_Stop(aborttimer); Timer_Deinstall(aborttimer); } char filename[MAX_FILENAME_SIZE+1] = ""; sprintf(filename, "\\\\fls0\\%s.txt", inputname); unsigned short pFile[MAX_FILENAME_SIZE+1]; Bfile_StrToName_ncpy(pFile, (unsigned char*)filename, strlen(filename)+1); // calculate size int size = 0; int maxHistory = curRecHistEntry - 1; //because we ++'ed at the end of last addition for(int i=0; i <= maxHistory; i++) { size = size + strlen(recHistory[i]) + 1; // 1 byte for \n. we will use unix line termination } int BCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size); if(BCEres >= 0) // Did it create? { BCEres = Bfile_OpenFile_OS(pFile, READWRITE, 0); // Get handle for(int i=0; i <= maxHistory; i++) { char* buf = (char*)alloca(strlen(recHistory[i])+5); strcpy(buf, recHistory[i]); strcat(buf, (char*)"\n"); Bfile_WriteFile_OS(BCEres, buf, strlen(recHistory[i])+1); } Bfile_CloseFile_OS(BCEres); printf("Script created.\n"); } else { printf("An error occurred when creating the script for recording.\n"); } aborttimer = Timer_Install(0, check_execution_abort, 100); if (aborttimer > 0) Timer_Start(aborttimer); return; } } else { execution_in_progress = 1; has_drawn_graph = 0; run(expr); // run_startup_script cannot run from inside eval_clear because then it would be a run() inside a run() if(run_startup_script_again) { run_startup_script_again = 0; run_startup_script(); } execution_in_progress = 0; // if recording, add input to record if(isRecording && curRecHistEntry <= 200) { recHistory[curRecHistEntry] = (char*)alloca(strlen(expr)+2); // 2 bytes for security strcpy(recHistory[curRecHistEntry], expr); curRecHistEntry++; } check_do_graph(); } } }
void passwordGenerator() { Menu menu; menu.type = MENUTYPE_FKEYS; menu.title = (char*)"Password Generator"; menu.height = 7; MenuItem items[6]; int length = 10; int seed = RTC_GetTicks() * (GetMainBatteryVoltage(1) % 100); char lstr[10]; items[1].text = (char*)"Include symbols"; items[1].type = MENUITEM_CHECKBOX; items[2].text = (char*)"Include numbers"; items[2].type = MENUITEM_CHECKBOX; items[2].value = MENUITEM_VALUE_CHECKED; items[3].text = (char*)"Include uppercase"; items[3].type = MENUITEM_CHECKBOX; items[3].value = MENUITEM_VALUE_CHECKED; items[4].text = (char*)"Include confusable"; items[4].type = MENUITEM_CHECKBOX; items[4].value = MENUITEM_VALUE_CHECKED; items[5].text = (char*)"Memorable vowel mix"; items[5].type = MENUITEM_CHECKBOX; menu.numitems = 6; menu.items = items; while(1) { drawFkeyLabels(0x03B3, 0, 0, 0, 0, 0x0184); // FILE, EXE (white) itoa(length, (unsigned char*)lstr); char t[20]; strcpy(t, "Length: "); strcat(t, lstr); items[0].text = t; switch(doMenu(&menu)) { case MENU_RETURN_EXIT: return; case MENU_RETURN_SELECTION: if(menu.selection > 1) items[menu.selection-1].value = !items[menu.selection-1].value; else { Selector sel; sel.min = 6; sel.value = length; sel.max = 30; sel.cycle = 1; sel.title = (char*)"Password Generator"; sel.subtitle = (char*)"Length"; if(doSelector(&sel) == SELECTOR_RETURN_SELECTION) { length = sel.value; } } break; case KEY_CTRL_F1: { Selector sel; sel.min = 1; sel.value = 10; sel.max = 1000; sel.cycle = 1; sel.title = (char*)"Generate to file"; sel.subtitle = (char*)"Number of passwords"; if(doSelector(&sel) != SELECTOR_RETURN_SELECTION) break; SetBackGround(10); drawScreenTitle("Generate to file", "Filename:"); char newname[MAX_NAME_SIZE]; newname[0] = 0; textInput input; input.forcetext=1; input.symbols = 0; input.charlimit=MAX_NAME_SIZE; input.buffer = (char*)newname; int inscreen = 1; while(inscreen) { input.key=0; int res = doTextInput(&input); if (res==INPUT_RETURN_EXIT) break; // user aborted else if (res==INPUT_RETURN_CONFIRM) { inscreen = 0; } } if(inscreen) break; char newfilename[MAX_FILENAME_SIZE]; strcpy(newfilename, SMEM_PREFIX); strcat(newfilename, newname); strcat(newfilename, ".txt"); unsigned short pFile[0x10A]; Bfile_StrToName_ncpy(pFile, newfilename, 0x10A); unsigned int size = 1; int ntry = 0; while(ntry < 2) { ntry++; int BCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size); if(BCEres >= 0) { int hFile = Bfile_OpenFile_OS(pFile, READWRITE, 0); // Get handle if(hFile >= 0) { char password[35]; char line[37]; for(int i = 0; i < sel.value; i++) { generateRandomString(password, length, items[1].value, items[2].value, items[3].value, items[4].value, items[5].value, &seed); sprintf(line, "%s\r\n", password); Bfile_WriteFile_OS(hFile, line, length+2); } Bfile_CloseFile_OS(hFile); } else AUX_DisplayErrorMessage(0x2B); break; } else if(ntry < 2) { // File creation probably failed due to the presence of a file with the same name if(overwriteFilePrompt(newfilename)) Bfile_DeleteEntry(pFile); else break; } else AUX_DisplayErrorMessage(0x2B); } break; } case KEY_CTRL_F6: int inscreen = 1; while(inscreen) { Bdisp_AllClr_VRAM(); drawScreenTitle("Password Generator", "Generated passwords:"); textArea text; text.type = TEXTAREATYPE_INSTANT_RETURN; text.scrollbar = 0; text.y = 48+3; text.lineHeight = 20; textElement e[5]; char passwords[5][35]; for(int i = 0; i < 5; i++) { generateRandomString(passwords[i], length, items[1].value, items[2].value, items[3].value, items[4].value, items[5].value, &seed); e[i].text = passwords[i]; if(i) e[i].newLine = 1; } text.elements = e; text.numelements = 5; doTextArea(&text); drawFkeyLabels(0x036F, 0, 0, 0, 0, 0x02B9); // <, REPEAT (white) while(1) { int key; mGetKey(&key); if(key == KEY_CTRL_F6) break; if(key == KEY_CTRL_F1 || key == KEY_CTRL_EXIT) { inscreen = 0; break; } } } break; } } }