FreeBASIC  0.91.0
file_put.c
Go to the documentation of this file.
1 /* put # function */
2 
3 #include "fb.h"
4 
6  (
8  fb_off_t pos,
9  const void *data,
10  size_t length,
11  int adjust_rec_pos,
12  int checknewline,
13  int is_unicode
14  )
15 {
16  int res;
17 
18  if( !FB_HANDLE_USED(handle) )
20 
21  if( pos < 0 )
23 
24  FB_LOCK();
25 
27 
28  /* clear put back buffer for every modifying non-read operation */
29  handle->putback_size = 0;
30 
31  /* seek to newpos */
32  if( pos > 0 )
33  res = fb_FileSeekEx( handle, pos );
34 
35  if (res==FB_RTERROR_OK)
36  {
37  /* do write */
38  if( !is_unicode )
39  {
40  if( handle->hooks->pfnWrite != NULL )
41  res = handle->hooks->pfnWrite( handle, data, length );
42  else
44  }
45  else
46  {
47  if( handle->hooks->pfnWriteWstr != NULL )
48  res = handle->hooks->pfnWriteWstr( handle, (FB_WCHAR *)data, length );
49  else
51  }
52 
53  }
54 
55  if( handle->mode == FB_FILE_MODE_RANDOM &&
56  res==FB_RTERROR_OK &&
57  adjust_rec_pos &&
58  handle->len!=0 &&
59  handle->hooks->pfnSeek!=NULL )
60  {
61  /* if in random mode, writes must be of reclen.
62  * The device must also support the SEEK method and the length
63  * must be non-null */
64 
65  if( length != handle->len )
67 
68  size_t skip_size = (handle->len -
69  ((!is_unicode? length: length*sizeof( FB_WCHAR )) % handle->len)) % handle->len;
70  if (skip_size != 0)
71  {
72  /* devices that don't support seek should simulate it
73  with write or never allow to be opened for random access */
74  handle->hooks->pfnSeek( handle, skip_size, SEEK_CUR );
75  }
76  }
77 
78 #ifndef FB_NATIVE_TAB
79  if( checknewline )
80  if ( res == FB_RTERROR_OK )
81  {
82  size_t i = length;
83  if( !is_unicode )
84  {
85  const char *pachText = (const char *) data;
86 
87  /* search for last printed CR or LF */
88  while (i--)
89  {
90  char ch = pachText[i];
91  if (ch=='\n' || ch=='\r')
92  break;
93  }
94  }
95  else
96  {
97  const FB_WCHAR *pachText = (const FB_WCHAR *) data;
98 
99  /* search for last printed CR or LF */
100  while (i--)
101  {
102  FB_WCHAR ch = pachText[i];
103  if (ch == _LC('\n') || ch== _LC('\r') )
104  break;
105  }
106 
107  }
108 
109  handle = FB_HANDLE_DEREF(handle);
110  ++i;
111  if (i==0)
112  handle->line_length += length;
113  else
114  handle->line_length = length - i;
115 
116  {
117  int iWidth = FB_HANDLE_DEREF(handle)->width;
118  if( iWidth!=0 ) {
119  handle->line_length %= iWidth;
120  }
121  }
122  }
123 #endif
124 
125  FB_UNLOCK();
126 
127  /* set the error code again - handle->hooks->pfnSeek() may have reset it */
128  return fb_ErrorSetNum( res );
129 }
130 
131 int fb_FilePutData
132  (
133  int fnum,
134  fb_off_t pos,
135  const void *data,
136  size_t length,
137  int adjust_rec_pos,
138  int checknewline
139  )
140 {
141  return fb_FilePutDataEx( FB_FILE_TO_HANDLE(fnum),
142  pos, data, length, adjust_rec_pos, checknewline, FALSE );
143 }
144 
145 FBCALL int fb_FilePut
146  (
147  int fnum,
148  int pos,
149  void* value,
150  size_t valuelen
151  )
152 {
153  return fb_FilePutData( fnum, pos, value, valuelen, TRUE, FALSE );
154 }
155 
157  (
158  int fnum,
159  long long pos,
160  void *value,
161  size_t valuelen
162  )
163 {
164  return fb_FilePutData( fnum, pos, value, valuelen, TRUE, FALSE );
165 }