File: wnwtokp.c

package info (click to toggle)
libwn6 6.0-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,996 kB
  • ctags: 3,938
  • sloc: ansic: 45,083; makefile: 926; csh: 274; sh: 12
file content (165 lines) | stat: -rw-r--r-- 2,982 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************************

wn_write_blanks_pretty(stream,num)
wn_write_blanks_pretty_until_column(stream,col)

wn_write_literal_pretty(stream,literal)
wn_write_id_pretty(stream,id)

wn_write_int_pretty(stream,i)
wn_write_double_pretty(stream,f)

wn_write_quoted_string_pretty(stream,start_char,string,fin_char)

******************************************************************************/
/****************************************************************************

COPYRIGHT NOTICE:

  The source code in this file is provided free of charge
  to the author's consulting clients.  It is in the
  public domain and therefore may be used by anybody for
  any purpose.

AUTHOR:

  Will Naylor

****************************************************************************/

#include <stdio.h>
#include <string.h>

#include "wnlib.h"
#include "wnmax.h"

#include "wncstr.h"



local void write_pretty_linefeed_if_needed(wn_cstream stream,int len)
{
  if(
      (stream->pretty_block_level == 0)
        &&
      (
        (wn_current_mark(stream)+len - stream->last_linefeed_mark)
          >
        stream->line_length_limit
      )
    )
  {
    (*(stream->ppretty_linefeed_routine))(stream);
  }
}


void wn_write_blanks_pretty(wn_cstream stream,int num)
{
  if(stream->pretty_block_level != 0)
  {
    wn_write_blanks(stream,num);
  }
  else
  {
    if(
        (wn_current_mark(stream)+num - stream->last_linefeed_mark)
          >
        stream->line_length_limit
      )
    {
      (*(stream->ppretty_linefeed_routine))(stream);
    }
    else
    {
      wn_write_blanks(stream,num);
    }
  }
}


void wn_write_blanks_pretty_until_column(wn_cstream stream,int col)
{
  int tab_blanks;

  tab_blanks = col - wn_column_of_current_mark(stream);

  wn_write_blanks_pretty(stream,wn_max(0,tab_blanks));
}


void wn_write_literal_pretty(wn_cstream stream,char *literal)
{
  int len;

  len = strlen(literal);

  write_pretty_linefeed_if_needed(stream,len);

  wn_write_block(stream,literal,len);
}


void wn_write_id_pretty(wn_cstream stream,char *id)
{
  int len;

  len = strlen(id);

  write_pretty_linefeed_if_needed(stream,len);

  wn_write_block(stream,id,len);
}


void wn_write_int_pretty(wn_cstream stream,int i)
{
  int len;
  char buf[50];

  sprintf(buf,"%d",i);

  len = strlen(buf);

  write_pretty_linefeed_if_needed(stream,len);

  wn_write_block(stream,buf,len);
}


void wn_write_double_pretty(wn_cstream stream,double f)
{
  int len;
  char buf[50];

  sprintf(buf,"%g",(float)f);

  len = strlen(buf);

  write_pretty_linefeed_if_needed(stream,len);

  wn_write_block(stream,buf,len);
}


void wn_write_quoted_string_pretty
(
  wn_cstream stream,
  char start_char,
  char *string,
  char fin_char
)
{
  int len;

  len = strlen(string);

  write_pretty_linefeed_if_needed(stream,len+2);

  wn_write_char(stream,start_char);
  wn_write_block(stream,string,len);
  wn_write_char(stream,fin_char);
}