diff options
| author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2008-08-02 20:39:03 +0000 |
|---|---|---|
| committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2008-08-02 20:39:03 +0000 |
| commit | 02103a2fa701954e42c8081fccf75eea26f52ce8 (patch) | |
| tree | 8055c7c0b7e3fa710e07a19928ec8c46d6a08d1d /apps/misc.c | |
| parent | 6485d6d3ba999e8cacde267a30c8415959fcfc79 (diff) | |
| download | rockbox-02103a2fa701954e42c8081fccf75eea26f52ce8.zip rockbox-02103a2fa701954e42c8081fccf75eea26f52ce8.tar.gz rockbox-02103a2fa701954e42c8081fccf75eea26f52ce8.tar.bz2 rockbox-02103a2fa701954e42c8081fccf75eea26f52ce8.tar.xz | |
Unify opening of utf-8 files (FS#6203). This also adds ignoring the BOM in several places it has been missing (as FS#6071).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18185 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/misc.c')
| -rw-r--r-- | apps/misc.c | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/apps/misc.c b/apps/misc.c index 60955b9..f1f5c4a 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1154,7 +1154,7 @@ char *strip_extension(char* buffer, int buffer_size, const char *filename) { char *dot = strrchr(filename, '.'); int len; - + if (buffer_size <= 0) { return NULL; @@ -1180,6 +1180,35 @@ char *strip_extension(char* buffer, int buffer_size, const char *filename) } #endif /* !defined(__PCTOOL__) */ +/** Open a UTF-8 file and set file descriptor to first byte after BOM. + * If no BOM is present this behaves like open(). + * If the file is opened for writing and O_TRUNC is set, write a BOM to + * the opened file and leave the file pointer set after the BOM. + */ +int open_utf8(const char* pathname, int flags) +{ + int fd; + unsigned char bom[BOM_SIZE]; + + fd = open(pathname, flags); + if(fd < 0) + return fd; + + if(flags & (O_TRUNC | O_WRONLY)) + { + write(fd, BOM, BOM_SIZE); + } + else + { + read(fd, bom, BOM_SIZE); + /* check for BOM */ + if(memcmp(bom, BOM, BOM_SIZE)) + lseek(fd, 0, SEEK_SET); + } + return fd; +} + + #ifdef HAVE_LCD_COLOR /* * Helper function to convert a string of 6 hex digits to a native colour |