现在的位置: 首页 > 综合 > 正文

obex_io.c

2013年12月10日 ⁄ 综合 ⁄ 共 6444字 ⁄ 字号 评论关闭
 
  1. ********************************************************************* 
  2.  *                 
  3.  * Filename:      obex_io.c 
  4.  * Version:       0.3 
  5.  * Description:   Some useful disk-IO functions 
  6.  * Status:        Experimental. 
  7.  * Author:        Pontus Fuchs <pontus.fuchs@tactel.se> 
  8.  * Created at:    Mon Aug 07 19:32:14 2000 
  9.  * Modified at:   Mon Aug 07 19:32:14 2000 
  10.  * Modified by:   Pontus Fuchs <pontus.fuchs@tactel.se> 
  11.  *  
  12.  *     Copyright (c) 1999 Dag Brattli, All Rights Reserved. 
  13.  *      
  14.  *     This program is free software; you can redistribute it and/or  
  15.  *     modify it under the terms of the GNU General Public License as  
  16.  *     published by the Free Software Foundation; either version 2 of  
  17.  *     the License, or (at your option) any later version. 
  18.  *  
  19.  *     This program is distributed in the hope that it will be useful, 
  20.  *     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  21.  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  22.  *     GNU General Public License for more details. 
  23.  *  
  24.  *     You should have received a copy of the GNU General Public License  
  25.  *     along with this program; if not, write to the Free Software  
  26.  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,  
  27.  *     MA 02111-1307 USA 
  28.  *      
  29.  ********************************************************************/  
  30.   
  31. #ifdef HAVE_CONFIG_H  
  32. #include <config.h>  
  33. #endif  
  34.   
  35. #include <stdio.h>  
  36. #include <stdlib.h>  
  37.   
  38. #ifdef _WIN32  
  39. #include <io.h>  
  40. #include <windows.h>  
  41. #else  
  42. #include <sys stat.h="">  
  43. #include <unistd.h>  
  44. #endif /*_WIN32 */  
  45.   
  46. #include <fcntl.h>  
  47. #include <string.h>  
  48.   
  49.   
  50. #include <openobex obex.h="">  
  51.   
  52. #include "obex_io.h"  
  53.   
  54. extern obex_t *handle;  
  55. int obex_protocol_type = OBEX_PROTOCOL_GENERIC;  
  56.   
  57. //  
  58. // Get the filesize in a "portable" way  
  59. //  
  60. int get_filesize(const char *filename)  
  61. {  
  62. #ifdef _WIN32  
  63.     HANDLE fh;  
  64.     int size;  
  65.     fh = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);  
  66.     if(fh == INVALID_HANDLE_VALUE) {  
  67.         printf("Cannot open %s/n", filename);  
  68.         return -1;    
  69.     }  
  70.     size = GetFileSize(fh, NULL);  
  71.     printf("fize size was %d/n", size);  
  72.     CloseHandle(fh);  
  73.     return size;  
  74.   
  75. #else  
  76.     struct stat stats;  
  77.     /*  Need to know the file length */  
  78.     stat(filename, &stats);  
  79.     return (int) stats.st_size;  
  80. #endif  
  81. }  
  82.   
  83.   
  84. //  
  85. // Read a file and alloc a buffer for it  
  86. //  
  87. uint8_t* easy_readfile(const char *filename, int *file_size)  
  88. {  
  89.     int actual;  
  90.     int fd;  
  91.     uint8_t *buf;  
  92.   
  93.     *file_size = get_filesize(filename);  
  94.     printf("name=%s, size=%d/n", filename, *file_size);  
  95.   
  96. #ifdef _WIN32  
  97.     fd = open(filename, O_RDONLY | O_BINARY, 0);  
  98. #else  
  99.     fd = open(filename, O_RDONLY, 0);  
  100. #endif  
  101.   
  102.     if (fd == -1) {  
  103.         return NULL;  
  104.     }  
  105.       
  106.     if(! (buf = malloc(*file_size)) )   {  
  107.         return NULL;  
  108.     }  
  109.   
  110.     actual = read(fd, buf, *file_size);  
  111.     close(fd);   
  112.   
  113. #ifdef _WIN32  
  114.     if(actual != *file_size) {  
  115.         free(buf);  
  116.         buf = NULL;  
  117.     }  
  118. #else  
  119.     *file_size = actual;  
  120. #endif  
  121.     return buf;  
  122. }  
  123.   
  124.   
  125. //  
  126. //  
  127. //  
  128. obex_object_t *build_object_from_file(obex_t *handle, const char *filename)  
  129. {  
  130.     obex_headerdata_t hdd;  
  131.     uint8_t unicode_buf[200];  
  132.     int namebuf_len;  
  133.     obex_object_t *object;  
  134.     uint32_t creator_id;  
  135.     int file_size;  
  136.     char *name = NULL;  
  137.     uint8_t *buf;  
  138.   
  139.   
  140.     buf = easy_readfile(filename, &file_size);  
  141.     if(buf == NULL)  
  142.         return NULL;  
  143.   
  144.     /* Set Memopad as the default creator ID */  
  145.     creator_id = MEMO_PAD;    
  146.   
  147.     /* Find the . in the filename  
  148.            原型:extern char *strchr(char *s,char c); 
  149.            用法:#include <string.h> 
  150.            功能:查找字符串s中首次出现字符c的位置 
  151.            说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL 
  152.       */  
  153.     name = strchr(filename, '.');  
  154.     if (name) {  
  155.         name++;  
  156.         if (strcmp(name, "vcf") == 0) {  
  157.             printf( "This is a Address Book file/n");  
  158.             creator_id = ADDRESS_BOOK;  
  159.         } else if (strcmp(name, "vcs") == 0) {  
  160.             printf( "This is a Date Book file/n");  
  161.             creator_id = DATE_BOOK;  
  162.         } else if (strcmp(name, "txt") == 0) {  
  163.             printf("This is a Memo pad file/n");  
  164.             creator_id = MEMO_PAD;  
  165.         } else if (strcmp(name, "prc") == 0) {  
  166.             printf("This is a Pilot resource file/n");  
  167.             creator_id = PILOT_RESOURCE;  
  168.         }  
  169.     }  
  170.     /* Build object */  
  171.     object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);  
  172.   
  173.     namebuf_len = OBEX_CharToUnicode(unicode_buf, (uint8_t *) filename, sizeof(unicode_buf));  
  174.   
  175.     hdd.bs = unicode_buf;  
  176.     OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME,  
  177.                 hdd, namebuf_len, 0);  
  178.   
  179.     hdd.bq4 = file_size;  
  180.     OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH,  
  181.                 hdd, sizeof(uint32_t), 0);  
  182.   
  183. #if 0  
  184.     /* Optional header for win95 irxfer, allows date to be set on file */  
  185.     OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME2,  
  186.                 (obex_headerdata_t) (uint32_t) stats.st_mtime,  
  187.                 sizeof(uint32_t), 0);  
  188. #endif  
  189.     if (obex_protocol_type != 1) {  
  190.         /* Optional header for Palm Pilot */  
  191.         /* win95 irxfer does not seem to like this one */  
  192.         hdd.bq4 = creator_id;  
  193.         OBEX_ObjectAddHeader(handle, object, HEADER_CREATOR_ID,  
  194.                     hdd, sizeof(uint32_t), 0);  
  195.     }  
  196.   
  197.     hdd.bs = buf;  
  198.     OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,  
  199.                 hdd, file_size, 0);  
  200.   
  201.     free(buf);  
  202.     return object;  
  203. }  
  204.   
  205.   
  206. /* 
  207.  * Function safe_save_file () 
  208.  * 
  209.  *    First remove path and add "/tmp/". Then save. 
  210.  * 
  211.  */  
  212. int safe_save_file(char *name, const uint8_t *buf, int len)  
  213. {  
  214.     char *s = NULL;  
  215.     char filename[255] = {0,};  
  216.     int fd;  
  217.     int actual;  
  218.   
  219.   
  220.     printf("Filename = %s/n", name);  
  221.   
  222.     s = strrchr(name, '/');  
  223.     if (s == NULL)  
  224.         s = name;  
  225.     else  
  226.         s++;  
  227.   
  228.     strncat(filename, s, 250);  
  229. #ifdef _WIN32  
  230.     fd = open(filename, O_RDWR | O_CREAT, 0);  
  231. #else  
  232.     fd = open(filename, O_RDWR | O_CREAT, DEFFILEMODE);  
  233. #endif  
  234.   
  235.     if ( fd < 0) {  
  236.         perror( filename);  
  237.         return -1;  
  238.     }  
  239.       
  240.     actual = write(fd, buf, len);  
  241.     close(fd);  
  242.   
  243.     printf( "Wrote %s (%d bytes)/n", filename, actual);  
  244.   
  245.     return actual;  
  246. }  
  247. </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> 

抱歉!评论已关闭.