00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CUFO_STREAM_H
00019 #define CUFO_STREAM_H
00020
00021 #include <cufo/fwd.h>
00022 #include <cutext/fwd.h>
00023 #include <cu/va_ref.h>
00024 #include <cu/inherit.h>
00025 #include <cu/buffer.h>
00026 #include <cu/wchar.h>
00027 #include <cu/thread.h>
00028 #include <cuoo/fwd.h>
00029 #include <cucon/hzmap.h>
00030 #include <iconv.h>
00031
00032 CU_BEGIN_DECLARATIONS
00033
00034 #define CUFOP_FLUSH_MUST_CLEAR 1
00035 #define CUFOP_FLUSH_PROPAGATE 2
00036
00037 void cufoP_set_wide(cufo_stream_t fos, cu_bool_t be_wide);
00038 void cufoP_flush(cufo_stream_t fos, unsigned int flags);
00039 void *cufoP_stream_produce(cufo_stream_t fos, size_t len);
00040
00041
00042
00043
00044
00045
00046
00047 #define CUFO_PRIFLAG_PLUS 1
00048 #define CUFO_PRIFLAG_MINUS 2
00049 #define CUFO_PRIFLAG_SPACE 4
00050 #define CUFO_PRIFLAG_HASH 8
00051 #define CUFO_PRIFLAG_ZERO 16
00052
00053 struct cufo_prispec
00054 {
00055 unsigned int flags;
00056 int width;
00057 int precision;
00058 };
00059 typedef void (*cufo_print_fn_t)(cufo_stream_t fos, cufo_prispec_t spec,
00060 cu_va_ref_t va_ref);
00061
00062
00063
00064 typedef void (*cufo_print_ptr_fn_t)(cufo_stream_t fos, cufo_prispec_t spec,
00065 void *ptr);
00066
00067 #define CUFO_SFLAG_SHOW_TYPE_IF_UNPRINTABLE (1u << 0)
00068 #define CUFO_SFLAG_HAVE_ERROR (1u << 16)
00069
00070 struct cufo_convinfo
00071 {
00072 iconv_t cd;
00073 unsigned int wr_scale;
00074 };
00075
00076 struct cufo_stream
00077 {
00078 cu_inherit (cu_buffer);
00079 cutext_sink_t target;
00080 cu_bool_least_t is_wide;
00081 char lastchar;
00082 unsigned int flags;
00083 struct cufo_convinfo convinfo[2];
00084 #ifdef CUCONF_DEBUG_CLIENT
00085 struct cufoP_tag_stack *tag_stack;
00086 #endif
00087 struct cucon_hzmap clientstate_map;
00088 cu_mutex_t mutex;
00089 };
00090
00091 cu_bool_t cufo_stream_init(cufo_stream_t fos, char const *encoding,
00092 cutext_sink_t target);
00093
00094 cu_bool_t cufo_stream_clientstate(cufo_stream_t fos, void const *key,
00095 size_t state_size, cu_ptr_ptr_t state_out);
00096
00097 cu_bool_t cufo_stream_drop_clientstate(cufo_stream_t fos, void const *key);
00098
00099
00100
00101
00102
00103 char cufo_stream_lastchar(cufo_stream_t fos);
00104
00105 CU_SINLINE void *
00106 cufo_stream_produce(cufo_stream_t fos, size_t len)
00107 {
00108 if (len > cu_buffer_freecap(cu_to(cu_buffer, fos)))
00109 return cufoP_stream_produce(fos, len);
00110 else {
00111 void *res = cu_buffer_content_end(cu_to(cu_buffer, fos));
00112 cu_buffer_set_content_end(cu_to(cu_buffer, fos), cu_ptr_add(res, len));
00113 return res;
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122 extern cufo_stream_t cufo_stdout;
00123
00124
00125 extern cufo_stream_t cufo_stderr;
00126
00127 cufo_stream_t cufo_open_sink(cutext_sink_t sink);
00128
00129 cufo_stream_t cufo_open_strip_sink(cutext_sink_t sink);
00130
00131 cufo_stream_t cufo_open_strip_fd(char const *encoding,
00132 int fd, cu_bool_t close_fd);
00133
00134 cufo_stream_t cufo_open_strip_file(char const *encoding, char const *path);
00135
00136 cufo_stream_t cufo_open_strip_str(void);
00137
00138 cufo_stream_t cufo_open_strip_wstring(void);
00139
00140
00141 cufo_stream_t cufo_open_text_sink(cufo_textstyle_t style, cutext_sink_t sink);
00142
00143
00144
00145
00146 cufo_stream_t cufo_open_text_fd(char const *encoding, cufo_textstyle_t style,
00147 int fd, cu_bool_t close_fd);
00148
00149 cufo_stream_t cufo_open_text_file(char const *encoding, cufo_textstyle_t style,
00150 char const *path);
00151
00152 cufo_stream_t cufo_open_text_str(cufo_textstyle_t style);
00153
00154 cufo_stream_t cufo_open_text_wstring(cufo_textstyle_t style);
00155
00156 cufo_stream_t cufo_open_xml(cutext_sink_t target_sink);
00157
00158
00159
00160
00161
00162 cufo_stream_t cufo_open_term_fd(char const *encoding, char const *term,
00163 int fd, cu_bool_t close_fd);
00164
00165
00166
00167
00168 cufo_stream_t cufo_open_auto_fd(int fd, cu_bool_t close_fd);
00169
00170
00171
00172
00173 cu_box_t cufo_close(cufo_stream_t fos);
00174
00175
00176
00177 void cufo_close_discard(cufo_stream_t fos);
00178
00179
00180
00181
00182
00183
00184
00185 CU_SINLINE void cufo_lock(cufo_stream_t fos)
00186 { cu_mutex_lock(&fos->mutex); }
00187
00188
00189 CU_SINLINE void cufo_unlock(cufo_stream_t fos)
00190 { cu_mutex_unlock(&fos->mutex); }
00191
00192
00193 CU_SINLINE void cufo_flush(cufo_stream_t fos)
00194 { cufoP_flush(fos, CUFOP_FLUSH_PROPAGATE); }
00195
00196 CU_SINLINE void
00197 cufo_set_wide(cufo_stream_t fos, cu_bool_t be_wide)
00198 {
00199 if (fos->is_wide != be_wide)
00200 cufoP_set_wide(fos, be_wide);
00201 }
00202
00203 CU_SINLINE void
00204 cufo_flag_error(cufo_stream_t fos)
00205 { fos->flags |= CUFO_SFLAG_HAVE_ERROR; }
00206
00207 CU_SINLINE cu_bool_t
00208 cufo_have_error(cufo_stream_t fos)
00209 { return fos->flags & CUFO_SFLAG_HAVE_ERROR; }
00210
00211
00212
00213
00214
00215 CU_SINLINE void
00216 cufo_fast_putc(cufo_stream_t fos, char ch)
00217 {
00218 char *s;
00219 cu_debug_assert(!fos->is_wide);
00220 s = (char *)cufo_stream_produce(fos, 1);
00221 *s = ch;
00222 }
00223
00224 CU_SINLINE void
00225 cufo_fast_putwc(cufo_stream_t fos, cu_wchar_t wc)
00226 {
00227 cu_wchar_t *s;
00228 cu_debug_assert(fos->is_wide);
00229 s = (cu_wchar_t *)cufo_stream_produce(fos, sizeof(cu_wchar_t));
00230 *s = wc;
00231 }
00232
00233
00234
00235 void cufo_putc(cufo_stream_t fos, char ch);
00236
00237
00238 void cufo_putwc(cufo_stream_t fos, cu_wchar_t wc);
00239
00240
00241 void cufo_fillc(cufo_stream_t fos, char ch, int n);
00242
00243
00244 void cufo_fillwc(cufo_stream_t fos, cu_wchar_t wc, int n);
00245
00246
00247 void cufo_puts(cufo_stream_t fos, char const *cs);
00248
00249
00250 void cufo_putws(cufo_stream_t fos, cu_wchar_t *wcs);
00251
00252
00253 void cufo_newline(cufo_stream_t fos);
00254
00255
00256
00257 void cufo_space(cufo_stream_t fos);
00258
00259
00260 void cufo_print_charr(cufo_stream_t fos, char const *cs, size_t cs_len);
00261
00262
00263 void cufo_print_wcarr(cufo_stream_t fos, cu_wchar_t const *arr, size_t len);
00264
00265
00266 void cufo_print_wstring(cufo_stream_t fos, cu_wstring_t ws);
00267
00268
00269 void cufo_print_str(cufo_stream_t fos, cu_str_t str);
00270
00271
00272 void cufo_print_location(cufo_stream_t fos, cu_location_t loc);
00273
00274
00275 void cufo_print_ex(cufo_stream_t fos, cuex_t e);
00276
00277 void cufo_printsp_ex(cufo_stream_t fos, cufo_prispec_t spec, cuex_t e);
00278
00279
00280
00281
00282
00283 #ifndef CU_IN_DOXYGEN
00284 cu_bool_t cufoP_entera(cufo_stream_t fos, cufo_tag_t tag, ...);
00285 #endif
00286
00287 cu_bool_t cufo_entera_va(cufo_stream_t fos, cufo_tag_t tag, va_list va);
00288
00289
00290 CU_SINLINE cu_bool_t
00291 cufo_enter(cufo_stream_t fos, cufo_tag_t tag)
00292 { return cufoP_entera(fos, tag, NULL); }
00293
00294
00295
00296
00297 #define cufo_entera(fos, tag, ...) cufoP_entera(fos, tag, __VA_ARGS__, NULL)
00298
00299
00300
00301 void cufo_leave(cufo_stream_t fos, cufo_tag_t tag);
00302
00303 void cufo_leaveln(cufo_stream_t fos, cufo_tag_t tag);
00304
00305 void cufo_empty(cufo_stream_t fos, cufo_tag_t tag, ...);
00306
00307
00308
00309 void cufo_tagputc(cufo_stream_t fos, cufo_tag_t tag, char ch);
00310
00311
00312
00313 void cufo_tagputs(cufo_stream_t fos, cufo_tag_t tag, char const *s);
00314
00315
00316
00317
00318
00319
00320
00321 int cufo_vprintf(cufo_stream_t fos, char const *fmt, va_list va);
00322
00323
00324
00325 int cufo_printf(cufo_stream_t fos, char const *fmt, ...);
00326
00327
00328 int cufo_lprintf(cufo_stream_t fos, char const *fmt, ...);
00329
00330
00331
00332 int cufo_oprintf(char const *fmt, ...);
00333
00334
00335
00336 int cufo_eprintf(char const *fmt, ...);
00337
00338 int cufo_printfln(cufo_stream_t fos, char const *fmt, ...);
00339
00340
00341
00342
00343 void cufo_logf(cufo_stream_t fos, cu_log_facility_t facility,
00344 char const *fmt, ...);
00345
00346
00347 void cufo_vlogf(cufo_stream_t fos, cu_log_facility_t facility,
00348 char const *fmt, va_list va);
00349
00350
00351
00352
00353 void cufo_vlogf_at(cufo_stream_t fos, cu_log_facility_t facility,
00354 cu_location_t loc, char const *fmt, va_list va);
00355
00356
00357
00358
00359
00360 void cufo_register_ptr_format(char const *key, cufo_print_ptr_fn_t fn);
00361
00362 void cufo_register_va_format(char const *key, cufo_print_fn_t fn);
00363
00364
00365
00366 CU_END_DECLARATIONS
00367
00368 #endif