本文共 5294 字,大约阅读时间需要 17 分钟。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #ifdef _WIN32
- #include <io.h>
- #include <windows.h>
- #else
- #include <sys stat.h="">
- #include <unistd.h>
- #endif /*_WIN32 */
-
- #include <fcntl.h>
- #include <string.h>
-
-
- #include <openobex obex.h="">
-
- #include "obex_io.h"
-
- extern obex_t *handle;
- int obex_protocol_type = OBEX_PROTOCOL_GENERIC;
-
-
-
-
- int get_filesize(const char *filename)
- {
- #ifdef _WIN32
- HANDLE fh;
- int size;
- fh = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
- if(fh == INVALID_HANDLE_VALUE) {
- printf("Cannot open %s/n", filename);
- return -1;
- }
- size = GetFileSize(fh, NULL);
- printf("fize size was %d/n", size);
- CloseHandle(fh);
- return size;
-
- #else
- struct stat stats;
-
- stat(filename, &stats);
- return (int) stats.st_size;
- #endif
- }
-
-
-
-
-
- uint8_t* easy_readfile(const char *filename, int *file_size)
- {
- int actual;
- int fd;
- uint8_t *buf;
-
- *file_size = get_filesize(filename);
- printf("name=%s, size=%d/n", filename, *file_size);
-
- #ifdef _WIN32
- fd = open(filename, O_RDONLY | O_BINARY, 0);
- #else
- fd = open(filename, O_RDONLY, 0);
- #endif
-
- if (fd == -1) {
- return NULL;
- }
-
- if(! (buf = malloc(*file_size)) ) {
- return NULL;
- }
-
- actual = read(fd, buf, *file_size);
- close(fd);
-
- #ifdef _WIN32
- if(actual != *file_size) {
- free(buf);
- buf = NULL;
- }
- #else
- *file_size = actual;
- #endif
- return buf;
- }
-
-
-
-
-
- obex_object_t *build_object_from_file(obex_t *handle, const char *filename)
- {
- obex_headerdata_t hdd;
- uint8_t unicode_buf[200];
- int namebuf_len;
- obex_object_t *object;
- uint32_t creator_id;
- int file_size;
- char *name = NULL;
- uint8_t *buf;
-
-
- buf = easy_readfile(filename, &file_size);
- if(buf == NULL)
- return NULL;
-
-
- creator_id = MEMO_PAD;
-
-
-
-
-
-
-
- name = strchr(filename, '.');
- if (name) {
- name++;
- if (strcmp(name, "vcf") == 0) {
- printf( "This is a Address Book file/n");
- creator_id = ADDRESS_BOOK;
- } else if (strcmp(name, "vcs") == 0) {
- printf( "This is a Date Book file/n");
- creator_id = DATE_BOOK;
- } else if (strcmp(name, "txt") == 0) {
- printf("This is a Memo pad file/n");
- creator_id = MEMO_PAD;
- } else if (strcmp(name, "prc") == 0) {
- printf("This is a Pilot resource file/n");
- creator_id = PILOT_RESOURCE;
- }
- }
-
- object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
-
- namebuf_len = OBEX_CharToUnicode(unicode_buf, (uint8_t *) filename, sizeof(unicode_buf));
-
- hdd.bs = unicode_buf;
- OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME,
- hdd, namebuf_len, 0);
-
- hdd.bq4 = file_size;
- OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH,
- hdd, sizeof(uint32_t), 0);
-
- #if 0
-
- OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME2,
- (obex_headerdata_t) (uint32_t) stats.st_mtime,
- sizeof(uint32_t), 0);
- #endif
- if (obex_protocol_type != 1) {
-
-
- hdd.bq4 = creator_id;
- OBEX_ObjectAddHeader(handle, object, HEADER_CREATOR_ID,
- hdd, sizeof(uint32_t), 0);
- }
-
- hdd.bs = buf;
- OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,
- hdd, file_size, 0);
-
- free(buf);
- return object;
- }
-
-
-
-
-
-
-
-
- int safe_save_file(char *name, const uint8_t *buf, int len)
- {
- char *s = NULL;
- char filename[255] = {0,};
- int fd;
- int actual;
-
-
- printf("Filename = %s/n", name);
-
- s = strrchr(name, '/');
- if (s == NULL)
- s = name;
- else
- s++;
-
- strncat(filename, s, 250);
- #ifdef _WIN32
- fd = open(filename, O_RDWR | O_CREAT, 0);
- #else
- fd = open(filename, O_RDWR | O_CREAT, DEFFILEMODE);
- #endif
-
- if ( fd < 0) {
- perror( filename);
- return -1;
- }
-
- actual = write(fd, buf, len);
- close(fd);
-
- printf( "Wrote %s (%d bytes)/n", filename, actual);
-
- return actual;
- }
- </string.h></openobex></string.h></fcntl.h></unistd.h></sys></windows.h></io.h></stdlib.h></stdio.h></config.h></pontus.fuchs@tactel.se></pontus.fuchs@tactel.se>
转载地址:http://eicdi.baihongyu.com/