summaryrefslogtreecommitdiff
path: root/examples/detect.c
diff options
context:
space:
mode:
authorLinus Walleij <triad@df.lth.se>2006-05-05 14:22:51 +0000
committerLinus Walleij <triad@df.lth.se>2006-05-05 14:22:51 +0000
commit91405593d4d354c9dab81e74236ebc57d5a6fbe5 (patch)
tree40d72d54e064e355d0cdde0fe2d0e578ed7bad56 /examples/detect.c
parentafe6143a6c767bc3b7c69c740c9d1e4951cd6ee7 (diff)
downloadlibmtp-91405593d4d354c9dab81e74236ebc57d5a6fbe5.tar.gz
More niceing up...
Diffstat (limited to 'examples/detect.c')
-rw-r--r--examples/detect.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/examples/detect.c b/examples/detect.c
index 1f81699..e39693a 100644
--- a/examples/detect.c
+++ b/examples/detect.c
@@ -4,6 +4,35 @@
#include <stdio.h>
#include <string.h>
+#define XML_BUFSIZE 0x10000
+
+static void dump_xml_fragment(uint8_t *buf, uint32_t len)
+{
+ static int endianness = 0; // 0 = LE, 1 = BE
+ uint32_t bp = 0;
+
+ while (bp < len) {
+ if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
+ endianness = 0;
+ } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
+ endianness = 1;
+ } else {
+ uint16_t tmp;
+
+ if (endianness == 0) {
+ tmp = buf[bp+1] << 8 | buf[bp+0];
+ } else {
+ tmp = buf[bp+0] << 8 | buf[bp+1];
+ }
+ // Fix this some day, we only print ISO 8859-1 correctly here,
+ // should atleast support UTF-8.
+ printf("%c", (uint8_t) tmp);
+ }
+ bp += 2;
+ }
+ printf("\n");
+}
+
int main (int argc, char **argv)
{
LIBMTP_mtpdevice_t *device;
@@ -92,29 +121,21 @@ int main (int argc, char **argv)
if (tmpfile != -1) {
int ret = LIBMTP_Get_Track_To_File_Descriptor(device, xmlfileid, tmpfile, NULL, NULL);
if (ret == 0) {
- uint8_t buf[2];
- int endianness = 0; // 0 = LE, 1 = BE
+ uint8_t *buf = NULL;
+ uint32_t readbytes;
- printf("\nDevice description WMPInfo.xml file:\n");
+ buf = malloc(XML_BUFSIZE);
+ if (buf == NULL) {
+ printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
+ exit(1);
+ }
lseek(tmpfile, 0, SEEK_SET);
- while (read(tmpfile, (void*) buf, 2) == 2) {
- if (buf[0] == 0xFF && buf[1] == 0xFE) {
- endianness = 0;
- } else if (buf[0] == 0xFE && buf[1] == 0xff) {
- endianness = 1;
- } else {
- uint16_t tmp;
-
- if (endianness == 0) {
- tmp = buf[1] << 8 | buf[0];
- } else {
- tmp = buf[0] << 8 | buf[1];
- }
- // Fix this some day.
- printf("%c", (uint8_t) tmp);
- }
+ readbytes = read(tmpfile, (void*) buf, XML_BUFSIZE);
+
+ if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
+ printf("\nDevice description WMPInfo.xml file:\n");
+ dump_xml_fragment(buf, readbytes);
}
- printf("\n");
}
close(tmpfile);
}