1 /*********************************************************************
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 * Modified at: Sun Oct 06 10:00:00 2001
12 * Modified by: Ben Moore <ben@netjunki.org>
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 ********************************************************************/
47 #include <openobex/obex.h>
51 extern obex_t
*handle
;
52 int obex_protocol_type
= OBEX_PROTOCOL_GENERIC
;
55 // Get the filesize in a "portable" way
57 gint
get_filesize(const char *filename
)
62 fh
= CreateFile(filename
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
63 if(fh
== INVALID_HANDLE_VALUE
) {
64 g_print("Cannot open %s\n", filename
);
67 size
= GetFileSize(fh
, NULL
);
68 g_print("fize size was %d\n", size
);
74 /* Need to know the file length */
75 stat(filename
, &stats
);
76 return (gint
) stats
.st_size
;
82 // Read a file and alloc a buffer for it
84 guint8
* easy_readfile(const char *filename
, int *file_size
)
90 *file_size
= get_filesize(filename
);
91 g_print("name=%s, size=%d\n", filename
, *file_size
);
94 fd
= open(filename
, O_RDONLY
| O_BINARY
, 0);
96 fd
= open(filename
, O_RDONLY
, 0);
103 if(! (buf
= g_malloc(*file_size
)) ) {
107 actual
= read(fd
, buf
, *file_size
);
111 if(actual
!= *file_size
) {
125 obex_object_t
*build_object_from_file(obex_t
*handle
, const char *filename
)
127 obex_headerdata_t hdd
;
128 guint8 unicode_buf
[200];
130 obex_object_t
*object
;
137 buf
= easy_readfile(filename
, &file_size
);
141 /* Set Memopad as the default creator ID */
142 creator_id
= MEMO_PAD
;
144 /* Find the . in the filename */
145 name
= strchr(filename
, '.');
148 if (strcmp(name
, "vcf") == 0) {
149 printf( "This is a Address Book file\n");
150 creator_id
= ADDRESS_BOOK
;
151 } else if (strcmp(name
, "vcs") == 0) {
152 printf( "This is a Date Book file\n");
153 creator_id
= DATE_BOOK
;
154 } else if (strcmp(name
, "txt") == 0) {
155 printf("This is a Memo pad file\n");
156 creator_id
= MEMO_PAD
;
157 } else if (strcmp(name
, "prc") == 0) {
158 printf("This is a Pilot resource file\n");
159 creator_id
= PILOT_RESOURCE
;
163 object
= OBEX_ObjectNew(handle
, OBEX_CMD_PUT
);
165 namebuf_len
= OBEX_CharToUnicode(unicode_buf
, filename
, sizeof(unicode_buf
));
167 hdd
.bs
= unicode_buf
;
168 OBEX_ObjectAddHeader(handle
, object
, OBEX_HDR_NAME
,
169 hdd
, namebuf_len
, 0);
172 OBEX_ObjectAddHeader(handle
, object
, OBEX_HDR_LENGTH
,
173 hdd
, sizeof(guint32
), 0);
176 /* Optional header for win95 irxfer, allows date to be set on file */
177 OBEX_ObjectAddHeader(handle
, object
, OBEX_HDR_TIME2
,
178 (obex_headerdata_t
) (guint32
) stats
.st_mtime
,
181 if (obex_protocol_type
!= 1) {
182 /* Optional header for Palm Pilot */
183 /* win95 irxfer does not seem to like this one */
184 hdd
.bq4
= creator_id
;
185 OBEX_ObjectAddHeader(handle
, object
, HEADER_CREATOR_ID
,
186 hdd
, sizeof(guint32
), 0);
190 OBEX_ObjectAddHeader(handle
, object
, OBEX_HDR_BODY
,
199 * Function safe_save_file ()
201 * First remove path and add "/tmp/". Then save.
204 gint
safe_save_file(gchar
*name
, const guint8
*buf
, gint len
, gchar
*savedir
)
207 gchar filename
[255] = {0,};
212 g_print("Filename = %s\n", name
);
215 sprintf( filename
, "%s", savedir
);
217 s
= strrchr(name
, '/');
223 strncat(filename
, s
, 250);
225 fd
= open(filename
, O_RDWR
| O_CREAT
, 0);
227 fd
= open(filename
, O_RDWR
| O_CREAT
, DEFFILEMODE
);
235 actual
= write(fd
, buf
, len
);
238 g_print( "Wrote %s (%d bytes)\n", filename
, actual
);