reader_txt.c File Reference


Detailed Description

FAT 12/16/32 Text reader service.

This file defines a useful set of functions for the text file accesses on AVR32 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file reader_txt.c.

#include "conf_explorer.h"
#include <string.h>
#include "file.h"
#include "navigation.h"
#include "reader_txt.h"
#include "unicode.h"

Go to the source code of this file.

Functions

void reader_txt_beg (void)
 This function goes at the beginning of file and read the text header.
void reader_txt_close (void)
 Close text file.
U16 reader_txt_get_line (Bool b_unicode, FS_STRING string, U16 u16_str_size)
 This function fills an ASCII buffer with the current text line of opened file.
Bool reader_txt_jump_line (U16 nb_line)
 This function jumps text lines.
Bool reader_txt_new (const FS_STRING sz_name, U8 u8_txt_format)
 Create a new text file.
Bool reader_txt_open (Bool b_readonly)
 Open a selected text file in the current navigator.
void reader_txt_select_format (U8 u8_txt_format)
 This function allow to select the text format to decode.


Function Documentation

void reader_txt_beg ( void   ) 

This function goes at the beginning of file and read the text header.

Definition at line 103 of file reader_txt.c.

References file_read_buf(), file_seek(), fs_g_nav_entry, FS_SEEK_SET, Fs_management_entry::u8_txt_format, UNI_MAX_HEADER_SIZE, unicode_header_get(), and unicode_header_scan().

Referenced by pl_main_open(), pl_nav_readentry(), pl_nav_setpos(), and reader_txt_open().

00104 {
00105    // Table to store the header of text file
00106    U8    string_header[UNI_MAX_HEADER_SIZE];
00107 
00108    file_seek( 0, FS_SEEK_SET );  // go to at the beginning of file
00109 
00110    // Read the header to detect the text format (ASCII, UNICODE LE, UNICODE BE, UTF8)
00111    memset( string_header, 0, UNI_MAX_HEADER_SIZE );     // By default ASCII
00112    file_read_buf( string_header , UNI_MAX_HEADER_SIZE );
00113    fs_g_nav_entry.u8_txt_format = unicode_header_scan( string_header );
00114    // Set cursor after header
00115    file_seek( unicode_header_get( string_header, fs_g_nav_entry.u8_txt_format ), FS_SEEK_SET );
00116 }

void reader_txt_close ( void   ) 

Close text file.

Definition at line 271 of file reader_txt.c.

References file_close().

Referenced by pl_main_new().

00272 {
00273    file_close();
00274 }

U16 reader_txt_get_line ( Bool  b_unicode,
FS_STRING  string,
U16  u16_str_size 
)

This function fills an ASCII buffer with the current text line of opened file.

Parameters:
b_unicode TRUE to return a unicode string (UTF16), ASCII otherwise over there
string string used to store text line
u16_str_size maximum size of string (unit character, 1B for ASCII, 2B for unicode)
Returns:
size of the line (may be < or > at u16_str_size)

0, in case of error

//! This routine remove the character '\n' '\r'
//! 

Definition at line 142 of file reader_txt.c.

References file_eof(), file_getc(), file_read_buf(), file_seek(), FS_EOF, FS_ERR_EOF, fs_g_nav_entry, fs_g_status, FS_SEEK_CUR_RE, nav_checkdisk_disable(), nav_checkdisk_enable(), Fs_management_entry::u8_txt_format, UNI_MAX_UTF8_SIZE, UNI_TYPE_UTF16BE, UNI_TYPE_UTF16LE, UNI_TYPE_UTF8, and utf8_to_unicode().

Referenced by pl_main_readline(), and reader_txt_jump_line().

00143 {
00144    U8    utf8[UNI_MAX_UTF8_SIZE];
00145    U8    size_utf8_buf=0;
00146    U8    size_utf8_dec=0;
00147    U16   u16_size_line = 1;   // 1 to compute null terminator
00148    U16   u16_unicode;
00149    Bool  b_error = FALSE;
00150 
00151    nav_checkdisk_disable();   // To optimize speed
00152    
00153    while( 0 == file_eof() && !b_error )
00154    {
00155       // Get a unicode value
00156       switch( fs_g_nav_entry.u8_txt_format )
00157       {
00158          case UNI_TYPE_UTF8:
00159          // Remove UTF8 codes used
00160          if( 0 != size_utf8_dec )
00161          {
00162             U8 u8_i;
00163             for( u8_i=0; u8_i<(UNI_MAX_UTF8_SIZE-size_utf8_dec); u8_i++ )
00164             {
00165                utf8[u8_i]=utf8[u8_i+size_utf8_dec];
00166             }
00167             size_utf8_buf -= size_utf8_dec;
00168             size_utf8_dec =0;
00169          }
00170          // Complet UTF8 array
00171          size_utf8_buf += file_read_buf( &utf8[size_utf8_buf], (UNI_MAX_UTF8_SIZE-size_utf8_buf) );
00172          // Decode UTF8 to unicode
00173          size_utf8_dec = utf8_to_unicode( &utf8[0], &u16_unicode );
00174          break;
00175       
00176          case UNI_TYPE_UTF16BE:
00177          MSB(u16_unicode) = file_getc();
00178          LSB(u16_unicode) = file_getc();
00179          if (LSB(u16_unicode) == (FS_EOF & 0xFF) && fs_g_status != FS_ERR_EOF)
00180            b_error = TRUE;
00181          break;
00182 
00183          case UNI_TYPE_UTF16LE:
00184          LSB(u16_unicode) = file_getc();
00185          MSB(u16_unicode) = file_getc();
00186          if (MSB(u16_unicode) == (FS_EOF & 0xFF) && fs_g_status != FS_ERR_EOF)
00187            b_error = TRUE;
00188          break;
00189 
00190          default: // ASCII or other
00191          u16_unicode = file_getc();
00192          if (u16_unicode == FS_EOF && fs_g_status != FS_ERR_EOF)
00193            b_error = TRUE;
00194          break;
00195       }
00196 
00197       // Check character to remove
00198       if( '\r' == u16_unicode )
00199          continue;   // Ignore character
00200       
00201       // Check end of line
00202       if((  0  == u16_unicode )
00203       || ('\n' == u16_unicode ) )
00204          break;      // End of line
00205 
00206       u16_size_line++;
00207       // Fill string
00208       if( 1 < u16_str_size )
00209       {
00210          if( b_unicode )
00211          {
00212             ((FS_STR_UNICODE)string)[0] = u16_unicode;
00213          }else{
00214             string[0] = u16_unicode;
00215          }
00216          string += (b_unicode? 2 : 1 );
00217          u16_str_size--;
00218       }
00219    }
00220    // HERE, the line is readed
00221    
00222    if( UNI_TYPE_UTF8 == fs_g_nav_entry.u8_txt_format )
00223    {
00224       // Re position cursor on next UTF8
00225       file_seek( (size_utf8_buf-size_utf8_dec) , FS_SEEK_CUR_RE );
00226    }
00227 
00228    // Handle error cases
00229    if (b_error)
00230    {
00231      nav_checkdisk_enable();
00232      return 0;
00233    }
00234 
00235    // Add Null terminate
00236    if( 0 != u16_str_size )
00237    {
00238       if( b_unicode )
00239       {
00240          ((FS_STR_UNICODE)string)[0] = 0;
00241       }else{
00242          string[0] = 0;
00243       }
00244    }
00245    nav_checkdisk_enable();
00246    return u16_size_line;
00247 }

Bool reader_txt_jump_line ( U16  nb_line  ) 

This function jumps text lines.

Parameters:
nb_line number of lines to jump
Returns:
FALSE in case of error

Definition at line 256 of file reader_txt.c.

References file_eof(), and reader_txt_get_line().

00257 {
00258    while( 0 != nb_line )
00259    {
00260       if( 0 != file_eof() )
00261          return FALSE;
00262       reader_txt_get_line( FALSE, NULL, 0 );
00263       nb_line--;
00264    }
00265    return TRUE;
00266 }

Bool reader_txt_new ( const FS_STRING  sz_name,
U8  u8_txt_format 
)

Create a new text file.

Parameters:
sz_name contains the file name (ASCII or UNICODE )
u8_txt_format text format to use (UNI_TYPE_UTF8, UNI_TYPE_UTF16BE, UNI_TYPE_UTF16LE, UNI_TYPE_ASCII)
Returns:
FALSE in case of error, see global value "fs_g_status" for more detail

TRUE otherwise

Definition at line 82 of file reader_txt.c.

References file_write_buf(), fs_g_nav_entry, nav_file_create(), Fs_management_entry::u8_txt_format, UNI_MAX_HEADER_SIZE, and unicode_header_get().

Referenced by pl_main_new().

00083 {
00084    // Table to store the header of text file
00085    U8    string_header[UNI_MAX_HEADER_SIZE];
00086    U8    u8_header_size;
00087 
00088    // Create empty file
00089    if ( !nav_file_create( sz_name ))
00090       return FALSE;
00091 
00092    fs_g_nav_entry.u8_txt_format = u8_txt_format;
00093 
00094    // Write at the beginning of file the text header
00095    u8_header_size = unicode_header_get( string_header, fs_g_nav_entry.u8_txt_format );
00096    file_write_buf( string_header , u8_header_size );
00097    return TRUE;
00098 }

Bool reader_txt_open ( Bool  b_readonly  ) 

Open a selected text file in the current navigator.

Parameters:
b_readonly TRUE to open the text file in read only access
Returns:
FALSE in case of error, see global value "fs_g_status" for more detail

TRUE otherwise

Definition at line 65 of file reader_txt.c.

References file_open(), FOPEN_MODE_R, FOPEN_MODE_R_PLUS, and reader_txt_beg().

Referenced by pl_main_open().

00066 {
00067    if( !file_open( (b_readonly)? FOPEN_MODE_R : FOPEN_MODE_R_PLUS ) )
00068       return FALSE; // Open error
00069    reader_txt_beg();
00070    return TRUE;
00071 }

void reader_txt_select_format ( U8  u8_txt_format  ) 

This function allow to select the text format to decode.

Parameters:
u8_txt_format text format selected (UNI_TYPE_UTF8, UNI_TYPE_UTF16BE, UNI_TYPE_UTF16LE, UNI_TYPE_ASCII)

Definition at line 123 of file reader_txt.c.

References fs_g_nav_entry, and Fs_management_entry::u8_txt_format.

00124 {
00125    fs_g_nav_entry.u8_txt_format = u8_txt_format;
00126 }


Generated on Fri Feb 19 02:29:01 2010 for AVR32 - FAT Services by  doxygen 1.5.5