void Read_Config(const char* fn,int* n) { /* 读取分页及书签配置 参数说明: fn: 当前打开文件的文件名 n: 接受已缓存页数的缓冲区 */ char tmp[64]; FONTCHARACTER fname[64]; int handle,_n=*n; memset(tmp,0,sizeof(tmp)); strncpy(tmp,fn,strlen(fn)-strlen(strrchr(fn,'.'))); // 取文件名部分 strcat(tmp,".cfg"); char_to_font(tmp,fname); handle=Bfile_OpenFile_OS(fname,0); *n=_n; if (handle<=0) return; Bfile_ReadFile_OS(handle,&_n,4,0);*n=_n-1; // 读入已缓存页数 Bfile_ReadFile_OS(handle,bookmark,16,4); // 读入书签页码 Bfile_ReadFile_OS(handle,bytes,_n*4,20); //读入页面缓存 Bfile_CloseFile_OS(handle); }
ssize_t read(int fd,void * buffer,size_t n){ if(fd==frameBuf_FD){ memcpy(buffer,LCDstatefd,n); LCDstatefd+=n; return n; }else if(fd==urandom_FD){ size_t c=n; unsigned int * d=(unsigned int *)buffer; while (c>=4){ *d++=sys_rand(); c-=4; } unsigned char * r=(unsigned char *)d;//remaindor while(c--){ *r++=sys_rand(); } return n; }else if(fd==0){ //printf("Read %d bytes from stdin\n",n); inputStrTiny(buffer,n,1); }else if(fd>5) return Bfile_ReadFile_OS(toNativeFD(fd),buffer, n,-1); else return -1; }
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; } }
FONTFILE * open_font(const char * cfname) { int fh,r; FONTFILE *ff; fontc ffname[32]; char_to_font(cfname,ffname); fh = Bfile_OpenFile_OS(ffname,0); if (fh<=0) return NULL; ff = (FONTFILE*)malloc(sizeof(FONTFILE)); Bfile_ReadFile_OS (fh,ff,sizeof(FONTFILE)-2*sizeof(int),0); ff->file_handle = fh; ff->width = x86_dword_to_sh(ff->width); ff->height = x86_dword_to_sh(ff->height); ff->asc_offset = x86_dword_to_sh(ff->asc_offset); ff->chs_offset = x86_dword_to_sh(ff->chs_offset); r =ff->width/8 + (ff->width % 8 ? 1 : 0); ff->font_size = r*ff->height; return ff; }
ssize_t pread(int fd, void * ptr, size_t num, off_t off){ if(fd>=5) return Bfile_ReadFile_OS(toNativeFD(fd),ptr, num,off); else if(fd==frameBuf_FD){ memcpy(ptr,(unsigned char *)0xA8000000+off,num); return num; }else if(fd==urandom_FD) return read(fd,ptr,num); else return -1; }
void print_asc_char (int x,int y,int sel,unsigned char c) { unsigned char mat[128]; if (def_font==NULL) return; Bfile_ReadFile_OS( def_font->file_handle, mat, def_font->font_size, c*def_font->font_size + def_font->asc_offset); draw_pic(x,y,def_font->width,def_font->height,sel,mat); }
void print_chs_char (int x,int y,int sel,unsigned char c1,unsigned char c2) { unsigned char mat[128]; int sec,pot; if (def_font==NULL) return; sec = c1-0xa1; pot = c2-0xa1; Bfile_ReadFile_OS( def_font->file_handle, mat, def_font->font_size, (94*sec+pot)*def_font->font_size + def_font->chs_offset); draw_pic(x,y,def_font->width,def_font->height,sel,mat); }
size_t fread(void *buffer, size_t size, size_t count, FILE *stream) { size_t n = size * count; if (isstdstream(stream)) { if (stream->fileno == 0) { // stdin return fread_serial(buffer, size, n, stream); } else { // Reading stdout or stderr? No. return EOF; } } // TODO failure modes unknown size_t ret = Bfile_ReadFile_OS(handle_tonative(stream->fileno), buffer, n, -1); if (ret < 0) { stream->error = 1; } else if (ret < n) { stream->eof = 1; } return ret; }
void iRead_main(const char* filename) { /* 阅读界面主函数 参数说明: filename: 打开的文件名 (从文件浏览器得到) */ int key,handle; char* buf=(char*)malloc(461); FONTCHARACTER fname[64]; char tip[64], tmp[64]; page=0;cached=0; memset(bytes,0,sizeof(bytes)); memset(bookmark,0,sizeof(bookmark));bookmark[3]=0; Read_Config(filename,&cached); // 读取书签及分页配置 // 如果分的页数不满 500 的整数倍,补分页满 if (cached==0) divide_page(filename,500-cached,1); else if (cached%500!=0) divide_page(filename,500-cached%500,1); // 补至 500 的整数倍 totbytes=0; /* 设置状态栏显示文字 0x0001:显示电量 0x0100:显示文字 */ DefineStatusAreaFlags(3, 0x01 | 0x02 | 0x100, 0, 0); beg: font16=open_font("\\\\fls0\\24PX.hzk"); select_font(font16); Bdisp_AllClr_VRAM(); draw_pic(0,192,124,22,0,Menu_Read); draw_pic(126,192,61,22,0,Menu_Sub_Jump); // 若翻下一页时超出已缓存页面范围 if (cached<=page) { // 如果分的页数不满 500 的整数倍,补分页满 if (!divide_page(filename,1,0)) page=cached-1; else if (cached%500!=0) divide_page(filename,500-cached%500,0); close_font(font16); goto beg; } totbytes=bytes[page]; // 修正读取字节指针位置 char_to_font(filename,fname); handle=Bfile_OpenFile_OS(fname,0); // 打开文件 Bfile_ReadFile_OS(handle,buf,400,totbytes); Bfile_CloseFile_OS(handle); print_chs_page(0,24,totbytes,(unsigned char*)buf); // 绘制一页 close_font(font16); // 准备显示浏览进度 char fn_ptr[64]; memset(fn_ptr, 0, sizeof(fn_ptr)); GetDisplayFileName(filename, fn_ptr); memset(tip, 0, sizeof(tip)); memset(tmp, 0, sizeof(tmp)); strcat(tip, fn_ptr); strcat(tip, " "); itoa(page + 1, tmp, 10); strcat(tip, tmp); strcat(tip, "/"); memset(tmp, 0, sizeof(tmp)); itoa(cached, tmp, 10);strcat(tip, tmp); // 状态栏显示文件名及进度 DefineStatusMessage(tip, 0, 0, 0); while (1) { GetKey(&key); switch (key) { case KEY_CTRL_UP: // 跳到上一页 if (page>0) { --page; goto beg; } break; case KEY_CTRL_DOWN: // 跳到下一页 ++page; goto beg; break; case KEY_CTRL_EXIT: // 离开,返回文件浏览器 Save_Config(filename,cached+1); DefineStatusAreaFlags(3, 0x01 | 0x02 | 0x100, 0, 0); return;break; case KEY_CTRL_F2: // 打开存储书签对话框 Save_Bookmark(filename,page,cached+1); goto beg;break; case KEY_CTRL_F1: // 打开读取书签对话框 Read_Bookmark(filename,&page,&cached); goto beg;break; case KEY_CTRL_F3: // 打开跳页对话框 Page_Jump(filename); goto beg;break; } } }
int divide_page(const char* filename,const int pages,int save) { /* 分页函数 参数说明: filename: 当前打开文件的文件名 pages: 需要分的页数 save: 是否需要跳回第一页 (初始化时使用) 返回值: 0: 已读到文件末尾 1: 未读到文件末尾 */ int cx,cy; int i=0,j; const int pp=16; int is_chs=0; // 中文字符标识 int tmp=cached,total=0; int decades_passed = 1; int handle; char* buf=(char*)malloc(460); FONTCHARACTER fname[64]; char_to_font(filename,fname); handle=Bfile_OpenFile_OS(fname,0); tmp=cached; totbytes=bytes[cached]; // 保险修正 // 如果请求的页数超过 9999 页,修正为 9999 页 if ((total=tmp+pages)>9999) total=9999; // 在 9999 页时尝试往后翻一页,跳出 if (cached+1>9999) {Bfile_CloseFile_OS(handle);return 0;}; // 在屏幕上显示分页的进度 ProgressBar(0, total - tmp); for (j=tmp;j<total;++j) // 从当前已缓存页面分到请求的页面,使用模拟填充显示区域方法 { // 尝试读取一段文字以备分页 memset(buf,0,461); Bfile_ReadFile_OS(handle,buf,400,totbytes); // 如果读到文件末尾则跳出 if (!buf[0]) { ProgressBar(total - tmp, total - tmp); Bfile_CloseFile_OS(handle); MsgBoxPop(); return 0; } // 填充位置设置为初始状态 cx=0; cy=24; for (i=0;;) { is_chs=buf[i] & 0x80; // 判断高位字节,确定是否为中文 if ((cx+pp)>368) // 填充超过屏幕右边缘 goto cn; if (is_chs) i+=2; // 中文,跳2字节 else { if (buf[i] == '\r' || buf[i] == '\n') // 若读到回车符直接进入下一行 { i++; if (buf[i] == '\r' || buf[i] == '\n') // 若读到回车符直接进入下一行 i++; goto cn; } i++; } // 将字符填充入当前行,增加字符偏移 if (is_chs) cx+=25; else cx+=18; // 填充超过屏幕右边缘 if (cx>368) { cn: cx=0; cy+=24; // 增加纵向字符偏移,进入下一行 if (cy>190) // 填充超过屏幕下边缘,跳出 break; } } bytes[j+1]=i+totbytes; // 将最后一个字符在文件中的位置存入下一页的缓存,以备读取 totbytes+=i; // 读取字节指针增加 ++cached; // 已缓存页面增加,表示分页成功 // 每分完1/10的总体,增加进度显示 if (j - tmp == (total - tmp) / 10 * decades_passed) { ProgressBar(j - tmp, total - tmp); decades_passed++; } } if (save) page=0; // 跳回第一页 ProgressBar(total - tmp, total - tmp); Bfile_CloseFile_OS(handle); MsgBoxPop(); 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; } } }
int CreateFileMapping(const unsigned short *pFileName,FileMapping *pMap){ int iResult = 0; char cBuffer[FLASH_PAGE_SIZE]; int hFile = Bfile_OpenFile_OS(pFileName,0,0); int iLength; char *pFlashFS = (char *)FLASH_START; pMap->miItemCount = 0; pMap->miTotalLength = 0; iLength = Bfile_ReadFile_OS(hFile,cBuffer,FLASH_PAGE_SIZE,-1); while(iLength > 0) { //do not optimize (= do not move these 2 variables before loop)! // fx-cg allocates pages for file in <random> order so page from the end of the file //can have lower index than page from the beginning const char *pTgt = pFlashFS; int iPageIndx = 0; for(;iPageIndx < FLASH_PAGE_COUNT;iPageIndx++) { if(!Xmemcmp(pTgt,cBuffer,iLength)) { break; } pTgt += FLASH_PAGE_SIZE; } if(iPageIndx == FLASH_PAGE_COUNT) { //page not found ! iResult = -2; goto lbExit; } pMap->miItemCount ++; if(pMap->miItemCount >= MAX_FRAGMENTS) { //file too fragmented ! iResult = -3; goto lbExit; } pMap->mTable[pMap->miItemCount-1].msOffset = (short)iPageIndx; pMap->mTable[pMap->miItemCount-1].msCount = 0; //assume fragment has more pages for(;;) { pMap->mTable[pMap->miItemCount-1].msCount++; pMap->miTotalLength += iLength; iPageIndx++; pTgt += FLASH_PAGE_SIZE; if(iLength < FLASH_PAGE_SIZE) { //this was the last page iResult = pMap->miTotalLength; goto lbExit; } iLength = Bfile_ReadFile_OS(hFile,cBuffer,FLASH_PAGE_SIZE,-1); if(iLength <= 0) { break; } if(Xmemcmp(pTgt,cBuffer,iLength)) { break; } } } if(iLength < 0) { iResult = -1; } else { if(pMap->miTotalLength >50000) { pMap->miTotalLength = 50000;//hack } iResult = pMap->miTotalLength; } lbExit: Bfile_CloseFile_OS(hFile); return iResult; }