FreeBASIC  0.91.0
io_writestr.c
Go to the documentation of this file.
1 /* write [#] functions */
2 
3 #include "fb.h"
4 
5 static void hWriteStrEx( FB_FILE *handle, const char *s, size_t len, int mask )
6 {
7  const char *buff;
8  ssize_t bufflen;
9 
10  /* close quote + new-line or comma */
11  if( mask & FB_PRINT_BIN_NEWLINE )
12  {
13  buff = "\"" FB_BINARY_NEWLINE;
14  bufflen = strlen( "\"" FB_BINARY_NEWLINE );
15  }
16  else if( mask & FB_PRINT_NEWLINE )
17  {
18  buff = "\"" FB_NEWLINE;
19  bufflen = strlen( "\"" FB_NEWLINE );
20  }
21  else
22  {
23  buff = "\",";
24  bufflen = 2;
25  }
26 
27  FB_LOCK( );
28 
29  /* open quote */
30  fb_hFilePrintBufferEx( handle, "\"", 1 );
31 
32  if( len != 0 )
33  FB_PRINT_EX( handle, s, len, 0 );
34 
35  fb_hFilePrintBufferEx( handle, buff, bufflen );
36 
37  FB_UNLOCK( );
38 }
39 
40 FBCALL void fb_WriteString ( int fnum, FBSTRING *s, int mask )
41 {
42  FB_FILE *handle = FB_FILE_TO_HANDLE( fnum );
43 
44  if( (s != NULL) && (s->data != NULL) )
45  hWriteStrEx( handle, s->data, FB_STRSIZE(s), mask );
46  else
47  {
48  if( mask & FB_PRINT_BIN_NEWLINE )
49  fb_hFilePrintBufferEx( handle, "\"\"" FB_BINARY_NEWLINE, 1+1+sizeof(FB_BINARY_NEWLINE)-1 );
50  else if( mask & FB_PRINT_NEWLINE )
51  fb_hFilePrintBufferEx( handle, "\"\"" FB_NEWLINE, 1+1+sizeof(FB_NEWLINE)-1 );
52  else
53  fb_hFilePrintBufferEx( handle, "\"\",", 1+1+1 );
54  }
55 
56  /* del if temp */
57  fb_hStrDelTemp( s );
58 }
59 
60 FBCALL void fb_WriteFixString ( int fnum, char *s, int mask )
61 {
62  FB_FILE *handle = FB_FILE_TO_HANDLE( fnum );
63 
64  if( s != NULL )
65  hWriteStrEx( handle, s, strlen( s ), mask );
66  else
67  {
68  if( mask & FB_PRINT_BIN_NEWLINE )
69  fb_hFilePrintBufferEx( handle, "\"\"" FB_BINARY_NEWLINE, 1+1+sizeof(FB_BINARY_NEWLINE)-1 );
70  else if( mask & FB_PRINT_NEWLINE )
71  fb_hFilePrintBufferEx( handle, "\"\"" FB_NEWLINE, 1+1+sizeof(FB_NEWLINE)-1 );
72  else
73  fb_hFilePrintBufferEx( handle, "\"\",", 1+1+1 );
74  }
75 }