mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-14 02:27:39 -04:00
Moved read_line() to misc.c
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3647 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
93e47b922f
commit
75b41a88f6
3 changed files with 48 additions and 36 deletions
39
apps/misc.c
39
apps/misc.c
|
@ -21,6 +21,9 @@
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "lcd.h"
|
#include "lcd.h"
|
||||||
#include "sprintf.h"
|
#include "sprintf.h"
|
||||||
|
#include "errno.h"
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
#define ONE_KILOBYTE 1024
|
#define ONE_KILOBYTE 1024
|
||||||
#define ONE_MEGABYTE (1024*1024)
|
#define ONE_MEGABYTE (1024*1024)
|
||||||
|
|
||||||
|
@ -49,6 +52,42 @@ char *num2max5(unsigned int bytes, char *max5)
|
||||||
return max5;
|
return max5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Read (up to) a line of text from fd into buffer and return number of bytes
|
||||||
|
* read (which may be larger than the number of bytes stored in buffer). If
|
||||||
|
* an error occurs, -1 is returned (and buffer contains whatever could be
|
||||||
|
* read). A line is terminated by a LF char. Neither LF nor CR chars are
|
||||||
|
* stored in buffer.
|
||||||
|
*/
|
||||||
|
int read_line(int fd, char* buffer, int buffer_size)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
int num_read = 0;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
|
while (count < buffer_size)
|
||||||
|
{
|
||||||
|
unsigned char c;
|
||||||
|
|
||||||
|
if (1 != read(fd, &c, 1))
|
||||||
|
break;
|
||||||
|
|
||||||
|
num_read++;
|
||||||
|
|
||||||
|
if ( c == '\n' )
|
||||||
|
break;
|
||||||
|
|
||||||
|
if ( c == '\r' )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
buffer[count++] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[MIN(count, buffer_size - 1)] = 0;
|
||||||
|
|
||||||
|
return errno ? -1 : num_read;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef TEST_MAX5
|
#ifdef TEST_MAX5
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,3 +22,11 @@
|
||||||
Make sure to have space for 6 bytes in the buffer. 5 letters plus the
|
Make sure to have space for 6 bytes in the buffer. 5 letters plus the
|
||||||
terminating zero byte. */
|
terminating zero byte. */
|
||||||
char *num2max5(unsigned int bytes, char *max5);
|
char *num2max5(unsigned int bytes, char *max5);
|
||||||
|
|
||||||
|
/* Read (up to) a line of text from fd into buffer and return number of bytes
|
||||||
|
* read (which may be larger than the number of bytes stored in buffer). If
|
||||||
|
* an error occurs, -1 is returned (and buffer contains whatever could be
|
||||||
|
* read). A line is terminated by a LF char. Neither LF nor CR chars are
|
||||||
|
* stored in buffer.
|
||||||
|
*/
|
||||||
|
int read_line(int fd, char* buffer, int buffer_size);
|
||||||
|
|
|
@ -44,6 +44,7 @@
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
#include "misc.h"
|
||||||
#ifdef HAVE_LCD_BITMAP
|
#ifdef HAVE_LCD_BITMAP
|
||||||
#include "icons.h"
|
#include "icons.h"
|
||||||
#include "font.h"
|
#include "font.h"
|
||||||
|
@ -707,42 +708,6 @@ void settings_load(void)
|
||||||
settings_apply();
|
settings_apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read (up to) a line of text from fd into buffer and return number of bytes
|
|
||||||
* read (which may be larger than the number of bytes stored in buffer). If
|
|
||||||
* an error occurs, -1 is returned (and buffer contains whatever could be
|
|
||||||
* read). A line is terminated by a LF char. Neither LF nor CR chars are
|
|
||||||
* stored in buffer.
|
|
||||||
*/
|
|
||||||
static int read_line(int fd, char* buffer, int buffer_size)
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
int num_read = 0;
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
while (count < buffer_size)
|
|
||||||
{
|
|
||||||
unsigned char c;
|
|
||||||
|
|
||||||
if (1 != read(fd, &c, 1))
|
|
||||||
break;
|
|
||||||
|
|
||||||
num_read++;
|
|
||||||
|
|
||||||
if ( c == '\n' )
|
|
||||||
break;
|
|
||||||
|
|
||||||
if ( c == '\r' )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
buffer[count++] = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[MIN(count, buffer_size - 1)] = 0;
|
|
||||||
|
|
||||||
return errno ? -1 : num_read;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* parse a line from a configuration file. the line format is:
|
/* parse a line from a configuration file. the line format is:
|
||||||
|
|
||||||
setting name: setting value
|
setting name: setting value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue