diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/misc.c | 39 | ||||
-rw-r--r-- | apps/misc.h | 8 | ||||
-rw-r--r-- | apps/settings.c | 37 |
3 files changed, 48 insertions, 36 deletions
diff --git a/apps/misc.c b/apps/misc.c index a49739f92d..69cb7d3174 100644 --- a/apps/misc.c +++ b/apps/misc.c | |||
@@ -21,6 +21,9 @@ | |||
21 | #include "file.h" | 21 | #include "file.h" |
22 | #include "lcd.h" | 22 | #include "lcd.h" |
23 | #include "sprintf.h" | 23 | #include "sprintf.h" |
24 | #include "errno.h" | ||
25 | #include "system.h" | ||
26 | |||
24 | #define ONE_KILOBYTE 1024 | 27 | #define ONE_KILOBYTE 1024 |
25 | #define ONE_MEGABYTE (1024*1024) | 28 | #define ONE_MEGABYTE (1024*1024) |
26 | 29 | ||
@@ -49,6 +52,42 @@ char *num2max5(unsigned int bytes, char *max5) | |||
49 | return max5; | 52 | return max5; |
50 | } | 53 | } |
51 | 54 | ||
55 | /* Read (up to) a line of text from fd into buffer and return number of bytes | ||
56 | * read (which may be larger than the number of bytes stored in buffer). If | ||
57 | * an error occurs, -1 is returned (and buffer contains whatever could be | ||
58 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | ||
59 | * stored in buffer. | ||
60 | */ | ||
61 | int read_line(int fd, char* buffer, int buffer_size) | ||
62 | { | ||
63 | int count = 0; | ||
64 | int num_read = 0; | ||
65 | |||
66 | errno = 0; | ||
67 | |||
68 | while (count < buffer_size) | ||
69 | { | ||
70 | unsigned char c; | ||
71 | |||
72 | if (1 != read(fd, &c, 1)) | ||
73 | break; | ||
74 | |||
75 | num_read++; | ||
76 | |||
77 | if ( c == '\n' ) | ||
78 | break; | ||
79 | |||
80 | if ( c == '\r' ) | ||
81 | continue; | ||
82 | |||
83 | buffer[count++] = c; | ||
84 | } | ||
85 | |||
86 | buffer[MIN(count, buffer_size - 1)] = 0; | ||
87 | |||
88 | return errno ? -1 : num_read; | ||
89 | } | ||
90 | |||
52 | #ifdef TEST_MAX5 | 91 | #ifdef TEST_MAX5 |
53 | int main(int argc, char **argv) | 92 | int main(int argc, char **argv) |
54 | { | 93 | { |
diff --git a/apps/misc.h b/apps/misc.h index b5cf896b28..c8aa266d5f 100644 --- a/apps/misc.h +++ b/apps/misc.h | |||
@@ -22,3 +22,11 @@ | |||
22 | Make sure to have space for 6 bytes in the buffer. 5 letters plus the | 22 | Make sure to have space for 6 bytes in the buffer. 5 letters plus the |
23 | terminating zero byte. */ | 23 | terminating zero byte. */ |
24 | char *num2max5(unsigned int bytes, char *max5); | 24 | char *num2max5(unsigned int bytes, char *max5); |
25 | |||
26 | /* Read (up to) a line of text from fd into buffer and return number of bytes | ||
27 | * read (which may be larger than the number of bytes stored in buffer). If | ||
28 | * an error occurs, -1 is returned (and buffer contains whatever could be | ||
29 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | ||
30 | * stored in buffer. | ||
31 | */ | ||
32 | int read_line(int fd, char* buffer, int buffer_size); | ||
diff --git a/apps/settings.c b/apps/settings.c index 63ba04a356..81e4d12f69 100644 --- a/apps/settings.c +++ b/apps/settings.c | |||
@@ -44,6 +44,7 @@ | |||
44 | #include "file.h" | 44 | #include "file.h" |
45 | #include "errno.h" | 45 | #include "errno.h" |
46 | #include "system.h" | 46 | #include "system.h" |
47 | #include "misc.h" | ||
47 | #ifdef HAVE_LCD_BITMAP | 48 | #ifdef HAVE_LCD_BITMAP |
48 | #include "icons.h" | 49 | #include "icons.h" |
49 | #include "font.h" | 50 | #include "font.h" |
@@ -707,42 +708,6 @@ void settings_load(void) | |||
707 | settings_apply(); | 708 | settings_apply(); |
708 | } | 709 | } |
709 | 710 | ||
710 | /* Read (up to) a line of text from fd into buffer and return number of bytes | ||
711 | * read (which may be larger than the number of bytes stored in buffer). If | ||
712 | * an error occurs, -1 is returned (and buffer contains whatever could be | ||
713 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | ||
714 | * stored in buffer. | ||
715 | */ | ||
716 | static int read_line(int fd, char* buffer, int buffer_size) | ||
717 | { | ||
718 | int count = 0; | ||
719 | int num_read = 0; | ||
720 | |||
721 | errno = 0; | ||
722 | |||
723 | while (count < buffer_size) | ||
724 | { | ||
725 | unsigned char c; | ||
726 | |||
727 | if (1 != read(fd, &c, 1)) | ||
728 | break; | ||
729 | |||
730 | num_read++; | ||
731 | |||
732 | if ( c == '\n' ) | ||
733 | break; | ||
734 | |||
735 | if ( c == '\r' ) | ||
736 | continue; | ||
737 | |||
738 | buffer[count++] = c; | ||
739 | } | ||
740 | |||
741 | buffer[MIN(count, buffer_size - 1)] = 0; | ||
742 | |||
743 | return errno ? -1 : num_read; | ||
744 | } | ||
745 | |||
746 | /* parse a line from a configuration file. the line format is: | 711 | /* parse a line from a configuration file. the line format is: |
747 | 712 | ||
748 | setting name: setting value | 713 | setting name: setting value |