//! //! This function reads from a file. //! //! @param fd file descriptor. //! @param buf pointer for data that are read. //! @param count amount of bytes to read //! //! @return ssize_t : amount of data read (-1 if error) //! ssize_t read(int fd, void *buf, size_t count) { ssize_t ReadCount = -1; if (fd < 0) { return (-1); } // take the mutex for nav access fsaccess_take_mutex(); nav_select( fd ); if ( file_eof() ) { // give the mutex for nav access fsaccess_give_mutex(); return(-1); } ReadCount = (ssize_t)file_read_buf((uint8_t *)buf , (uint16_t)count); // give the mutex for nav access fsaccess_give_mutex(); return(ReadCount); }
/* * clipboard_file_to_data - ファイルからデータを作成 */ HANDLE clipboard_file_to_data(const TCHAR *file_name, TCHAR *format_name, DWORD *ret_size, TCHAR *err_str) { HANDLE ret = NULL; BYTE *data; BYTE *mem; DWORD size; switch (clipboard_get_format(0, format_name)) { case CF_OWNERDISPLAY: return NULL; case CF_DSPMETAFILEPICT: case CF_METAFILEPICT: // メタファイル if ((ret = GlobalAlloc(GHND, sizeof(METAFILEPICT))) == NULL) { message_get_error(GetLastError(), err_str); return NULL; } if ((mem = GlobalLock(ret)) == NULL) { message_get_error(GetLastError(), err_str); GlobalFree(ret); return NULL; } if ((((METAFILEPICT *)mem)->hMF = GetMetaFile(file_name)) == NULL) { message_get_error(GetLastError(), err_str); GlobalUnlock(ret); GlobalFree(ret); return NULL; } size = GetMetaFileBitsEx(((METAFILEPICT *)mem)->hMF, 0, NULL); GlobalUnlock(ret); break; case CF_DSPENHMETAFILE: case CF_ENHMETAFILE: if ((ret = GetEnhMetaFile(file_name)) == NULL) { message_get_error(GetLastError(), err_str); } size = GetEnhMetaFileBits(ret, 0, NULL); break; default: // その他 // ファイルの読み込み if ((data = file_read_buf(file_name, &size, err_str)) == NULL) { return NULL; } // バイト列をデータに変換 ret = clipboard_bytes_to_data(format_name, data, &size); mem_free(&data); break; } if (ret_size != NULL) { *ret_size = size; } return ret; }
void bm_get_info(bm_file_header_t *bf, bm_info_header_t *bi) { // read header file_read_buf((void *) bf, sizeof(bm_file_header_t)); file_read_buf((void *) bi, sizeof(bm_info_header_t)); /* correct endianess for the stuff we need * only raw image data size and offset in file */ //swap_endian_blk((unsigned char *) &bm_file_header.bfType, 2); //swap_endian_blk((unsigned char *) &bm_file_header.bfSize, 4); swap_endian_blk((unsigned char *) bf + offsetof(bm_file_header_t, bfOffBits), 4); swap_endian_blk((unsigned char *) bi + offsetof(bm_info_header_t, biWidth), 4); swap_endian_blk((unsigned char *) bi + offsetof(bm_info_header_t, biHeight), 4); swap_endian_blk((unsigned char *) bi + offsetof(bm_info_header_t, biBitCount), 2); swap_endian_blk((unsigned char *) bi + offsetof(bm_info_header_t, biSizeImage), 4); //swap_endian_blk((unsigned char *) &bm_info_header.biCompression, 4); }
//! This function goes at the beginning of file and read the text header //! void reader_txt_beg( void ) { // Table to store the header of text file uint8_t string_header[UNI_MAX_HEADER_SIZE]; file_seek( 0, FS_SEEK_SET ); // go to at the beginning of file // Read the header to detect the text format (ASCII, UNICODE LE, UNICODE BE, UTF8) memset( string_header, 0, UNI_MAX_HEADER_SIZE ); // By default ASCII file_read_buf( string_header , UNI_MAX_HEADER_SIZE ); fs_g_nav_entry.u8_txt_format = unicode_header_scan( string_header ); // Set cursor after header file_seek( unicode_header_get( string_header, fs_g_nav_entry.u8_txt_format ), FS_SEEK_SET ); }
//! This function fills an ASCII buffer with the current text line of opened file //! //! @param b_unicode true to return a unicode string (UTF16), ASCII otherwise over there //! @param string string used to store text line //! @param u16_str_size maximum size of string (unit character, 1B for ASCII, 2B for unicode) //! //! @return size of the line (may be < or > at u16_str_size) //! @return 0, in case of error //! //! @verbatim //! This routine remove the character '\n' '\r' //! @endverbatim //! uint16_t reader_txt_get_line( bool b_unicode, FS_STRING string , uint16_t u16_str_size ) { uint8_t utf8[UNI_MAX_UTF8_SIZE]; uint8_t size_utf8_buf=0; uint8_t size_utf8_dec=0; uint16_t u16_size_line = 1; // 1 to compute null terminator uint16_t u16_unicode; bool b_error = false; nav_checkdisk_disable(); // To optimize speed while( 0 == file_eof() && !b_error ) { // Get a unicode value switch( fs_g_nav_entry.u8_txt_format ) { case UNI_TYPE_UTF8: // Remove UTF8 codes used if( 0 != size_utf8_dec ) { uint8_t u8_i; for( u8_i=0; u8_i<(UNI_MAX_UTF8_SIZE-size_utf8_dec); u8_i++ ) { utf8[u8_i]=utf8[u8_i+size_utf8_dec]; } size_utf8_buf -= size_utf8_dec; size_utf8_dec =0; } // Complete UTF8 array size_utf8_buf += file_read_buf( &utf8[size_utf8_buf], (UNI_MAX_UTF8_SIZE-size_utf8_buf) ); // Decode UTF8 to unicode size_utf8_dec = utf8_to_unicode( &utf8[0], &u16_unicode ); break; case UNI_TYPE_UTF16BE: MSB(u16_unicode) = file_getc(); LSB(u16_unicode) = file_getc(); if (LSB(u16_unicode) == (FS_EOF & 0xFF) && fs_g_status != FS_ERR_EOF) b_error = true; break; case UNI_TYPE_UTF16LE: LSB(u16_unicode) = file_getc(); MSB(u16_unicode) = file_getc(); if (MSB(u16_unicode) == (FS_EOF & 0xFF) && fs_g_status != FS_ERR_EOF) b_error = true; break; default: // ASCII or other u16_unicode = file_getc(); if (u16_unicode == FS_EOF && fs_g_status != FS_ERR_EOF) b_error = true; break; } // Check character to remove if( '\r' == u16_unicode ) continue; // Ignore character // Check end of line if(( 0 == u16_unicode ) || ('\n' == u16_unicode ) ) break; // End of line u16_size_line++; // Fill string if( 1 < u16_str_size ) { if( b_unicode ) { ((FS_STR_UNICODE)string)[0] = u16_unicode; } else { string[0] = u16_unicode; } string += (b_unicode? 2 : 1 ); u16_str_size--; } } // HERE, the line is read if( UNI_TYPE_UTF8 == fs_g_nav_entry.u8_txt_format ) { // Re position cursor on next UTF8 file_seek( (size_utf8_buf-size_utf8_dec) , FS_SEEK_CUR_RE ); } // Handle error cases if (b_error) { nav_checkdisk_enable(); return 0; } // Add Null terminate if( 0 != u16_str_size ) { if( b_unicode ) { ((FS_STR_UNICODE)string)[0] = 0; } else { string[0] = 0; } } nav_checkdisk_enable(); return u16_size_line; }