Plugin/file type association system. Patch #879411 by Henrik Backe
[kugel-rb.git] / apps / misc.c
blobbe1c3202fb8fae098d556d42d3a52f37e1924515
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "string.h"
20 #include "config.h"
21 #include "file.h"
22 #include "lcd.h"
23 #include "sprintf.h"
24 #include "errno.h"
25 #include "system.h"
27 #define ONE_KILOBYTE 1024
28 #define ONE_MEGABYTE (1024*1024)
30 /* The point of this function would be to return a string of the input data,
31 but never longer than 5 columns. Add suffix k and M when suitable...
32 Make sure to have space for 6 bytes in the buffer. 5 letters plus the
33 terminating zero byte. */
34 char *num2max5(unsigned int bytes, char *max5)
36 if(bytes < 100000) {
37 snprintf(max5, 6, "%5d", bytes);
38 return max5;
40 if(bytes < (9999*ONE_KILOBYTE)) {
41 snprintf(max5, 6, "%4dk", bytes/ONE_KILOBYTE);
42 return max5;
44 if(bytes < (100*ONE_MEGABYTE)) {
45 /* 'XX.XM' is good as long as we're less than 100 megs */
46 snprintf(max5, 6, "%2d.%0dM",
47 bytes/ONE_MEGABYTE,
48 (bytes%ONE_MEGABYTE)/(ONE_MEGABYTE/10) );
49 return max5;
51 snprintf(max5, 6, "%4dM", bytes/ONE_MEGABYTE);
52 return max5;
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.
61 int read_line(int fd, char* buffer, int buffer_size)
63 int count = 0;
64 int num_read = 0;
66 errno = 0;
68 while (count < buffer_size)
70 unsigned char c;
72 if (1 != read(fd, &c, 1))
73 break;
75 num_read++;
77 if ( c == '\n' )
78 break;
80 if ( c == '\r' )
81 continue;
83 buffer[count++] = c;
86 buffer[MIN(count, buffer_size - 1)] = 0;
88 return errno ? -1 : num_read;
91 #ifdef TEST_MAX5
92 int main(int argc, char **argv)
94 char buffer[32];
95 if(argc>1) {
96 printf("%d => %s\n",
97 atoi(argv[1]),
98 num2max5(atoi(argv[1]), buffer));
100 return 0;
103 #endif
105 #ifdef SCREENDUMP
106 extern unsigned char lcd_framebuffer[LCD_HEIGHT/8][LCD_WIDTH];
107 static unsigned char bmpheader[] =
109 0x42, 0x4d, 0x3e, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
110 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x40, 0x00,
111 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
112 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xd0, 0x80, 0x00, 0x00, 0x00,
114 0x00, 0x00
117 static unsigned char buf[112*8];
118 static unsigned char buf2[112*8];
119 static char dummy[2] = {0, 0};
120 static int fileindex = 0;
122 void screen_dump(void)
124 int f;
125 int i, shift;
126 int x, y;
127 char filename[MAX_PATH];
129 i = 0;
130 for(y = 0;y < LCD_HEIGHT/8;y++)
132 for(x = 0;x < LCD_WIDTH;x++)
134 buf[i++] = lcd_framebuffer[y][x];
138 memset(buf2, 0, sizeof(buf2));
140 for(y = 0;y < 64;y++)
142 shift = y & 7;
144 for(x = 0;x < 112/8;x++)
146 for(i = 0;i < 8;i++)
148 buf2[y*112/8+x] |= ((buf[y/8*112+x*8+i] >> shift)
149 & 0x01) << (7-i);
154 snprintf(filename, MAX_PATH, "/dump%03d.bmp", fileindex++);
155 f = creat(filename, O_WRONLY);
156 if(f >= 0)
158 write(f, bmpheader, sizeof(bmpheader));
160 for(i = 63;i >= 0;i--)
162 write(f, &buf2[i*14], 14);
163 write(f, dummy, 2);
166 close(f);
169 #endif