.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "KinoSearch::Store::OutStream 3" .TH KinoSearch::Store::OutStream 3 "2008-01-14" "perl v5.8.8" "User Contributed Perl Documentation" void lu_write (outstream, template_sv, ...) OutStream *outstream; \s-1SV\s0 *template_sv; \&\s-1PREINIT:\s0 \s-1STRLEN\s0 tpt_len; /* bytelength of template */ char *template; /* ptr to a spot in the template */ char *tpt_end; /* ptr to the end of the template */ int repeat_count; /* number of times to repeat sym */ int item_count; /* current place in \f(CW@_\fR */ char sym; /* the current symbol in the template */ char countsym; /* used when calculating repeat counts */ I32 aI32; U32 aU32; double aDouble; \s-1SV\s0 *aSV; char *string; \s-1STRLEN\s0 string_len; \&\s-1PPCODE:\s0 { /* require an object, a template, and at least 1 item */ if (items < 2) { Kino_confess(\*(L"lu_write error: too few arguments\*(R"); } .PP .Vb 3 \& /* prepare the template and get pointers */ \& template = SvPV(template_sv, tpt_len); \& tpt_end = template + tpt_len; .Ve .PP .Vb 4 \& /* reject an empty template */ \& if (tpt_len == 0) { \& Kino_confess("lu_write error: TEMPLATE cannot be empty string"); \& } .Ve .PP .Vb 3 \& /* init counters */ \& repeat_count = 0; \& item_count = 2; .Ve .PP .Vb 7 \& while (1) { \& /* only process template if we're not in the midst of a repeat */ \& if (repeat_count == 0) { \& /* fast-forward past space characters */ \& while (*template == ' ' && template < tpt_end) { \& template++; \& } .Ve .PP .Vb 14 \& /* if we're done, return or throw error */ \& if (template == tpt_end || item_count == items) { \& if (item_count != items) { \& Kino_confess( \& "lu_write error: Too many ITEMS, not enough TEMPLATE"); \& } \& else if (template != tpt_end) { \& Kino_confess( \& "lu_write error: Too much TEMPLATE, not enough ITEMS"); \& } \& else { /* success! */ \& break; \& } \& } .Ve .PP .Vb 3 \& /* derive the current symbol and a possible digit repeat sym */ \& sym = *template++; \& countsym = *template; .Ve .PP .Vb 20 \& if (template == tpt_end) { /* sym is last char in template */ \& repeat_count = 1; \& } \& else if (countsym >= '0' && countsym <= '9') { \& /* calculate numerical repeat count */ \& repeat_count = countsym - KINO_NUM_CHAR_OFFSET; \& countsym = *(++template); \& while ( template <= tpt_end \& && countsym >= '0' \& && countsym <= '9' \& ) { \& repeat_count = (repeat_count * 10) \& + (countsym - KINO_NUM_CHAR_OFFSET); \& countsym = *(++template); \& } \& } \& else { /* no numeric repeat count, so process sym only once */ \& repeat_count = 1; \& } \& } .Ve .PP .Vb 1 \& switch(sym) { .Ve .PP .Vb 15 \& case 'a': /* arbitrary binary data */ \& aSV = ST(item_count); \& if (!SvOK(aSV)) { \& Kino_confess("Internal error: undef at lu_write 'a'"); \& } \& string = SvPV(aSV, string_len); \& if (repeat_count != string_len) { \& Kino_confess( \& "lu_write error: repeat_count != string_len: %d %d", \& repeat_count, string_len); \& } \& Kino_OutStream_write_bytes(outstream, string, string_len); \& /* trigger next sym */ \& repeat_count = 1; \& break; .Ve .PP .Vb 5 \& case 'b': /* signed byte */ \& case 'B': /* unsigned byte */ \& aI32 = SvIV( ST(item_count) ); \& Kino_OutStream_write_byte(outstream, (char)(aI32 & 0xff)); \& break; .Ve .PP .Vb 4 \& case 'i': /* signed 32-bit integer */ \& aI32 = SvIV( ST(item_count) ); \& Kino_OutStream_write_int(outstream, (U32)aI32); \& break; .Ve .PP .Vb 4 \& case 'I': /* unsigned 32-bit integer */ \& aU32 = SvUV( ST(item_count) ); \& Kino_OutStream_write_int(outstream, aU32); \& break; .Ve .PP .Vb 4 \& case 'Q': /* unsigned "64-bit" integer */ \& aDouble = SvNV( ST(item_count) ); \& Kino_OutStream_write_long(outstream, aDouble); \& break; .Ve .PP .Vb 4 \& case 'V': /* VInt */ \& aU32 = SvUV( ST(item_count) ); \& Kino_OutStream_write_vint(outstream, aU32); \& break; .Ve .PP .Vb 4 \& case 'W': /* VLong */ \& aDouble = SvNV( ST(item_count) ); \& Kino_OutStream_write_vlong(outstream, aDouble); \& break; .Ve .PP .Vb 5 \& case 'T': /* string */ \& aSV = ST(item_count); \& string = SvPV(aSV, string_len); \& Kino_OutStream_write_string(outstream, string, string_len); \& break; .Ve .PP .Vb 3 \& default: \& Kino_confess("Illegal character in template: %c", sym); \& } .Ve .PP .Vb 5 \& /* use up one repeat_count and one item from the stack */ \& repeat_count--; \& item_count++; \& } \&} .Ve .PP void \&\s-1DESTROY\s0(outstream) OutStream *outstream; \&\s-1PPCODE:\s0 Kino_OutStream_destroy(outstream); .PP _\|_H_\|_ .PP #ifndef H_KINOIO #define H_KINOIO 1 .PP #include \*(L"\s-1EXTERN\s0.h\*(R" #include \*(L"perl.h\*(R" #include \*(L"\s-1XSUB\s0.h\*(R" #include \*(L"KinoSearchStoreInStream.h\*(R" #include \*(L"KinoSearchUtilCarp.h\*(R" #include \*(L"KinoSearchUtilMathUtils.h\*(R" .PP typedef struct outstream { PerlIO *fh; \s-1SV\s0 *fh_sv; char *buf; Off_t buf_start; int buf_pos; void (*seek) (struct outstream*, double); double (*tell) (struct outstream*); void (*write_byte) (struct outstream*, char); void (*write_bytes) (struct outstream*, char*, \s-1STRLEN\s0); void (*write_int) (struct outstream*, U32); void (*write_long) (struct outstream*, double); void (*write_vint) (struct outstream*, U32); void (*write_vlong) (struct outstream*, double); void (*write_string)(struct outstream*, char*, \s-1STRLEN\s0); } OutStream; .PP OutStream* Kino_OutStream_new (char*, SV*); void Kino_OutStream_seek (OutStream*, double); double Kino_OutStream_tell (OutStream*); double Kino_OutStream_length (OutStream*); void Kino_OutStream_flush (OutStream*); void Kino_OutStream_absorb (OutStream*, InStream*); void Kino_OutStream_write_byte (OutStream*, char); void Kino_OutStream_write_bytes (OutStream*, char*, \s-1STRLEN\s0); void Kino_OutStream_write_int (OutStream*, U32); void Kino_OutStream_write_long (OutStream*, double); void Kino_OutStream_write_vint (OutStream*, U32); int Kino_OutStream_encode_vint (U32, char*); void Kino_OutStream_write_vlong (OutStream*, double); void Kino_OutStream_write_string (OutStream*, char*, \s-1STRLEN\s0); void Kino_OutStream_destroy (OutStream*); .PP #endif /* include guard */ .PP _\|_C_\|_ .PP #include \*(L"KinoSearchStoreOutStream.h\*(R" .PP OutStream* Kino_OutStream_new(char* class, SV* fh_sv) { OutStream *outstream; .PP .Vb 2 \& /* allocate */ \& Kino_New(0, outstream, 1, OutStream); .Ve .PP .Vb 3 \& /* assign */ \& outstream->fh_sv = newSVsv(fh_sv); \& outstream->fh = IoOFP( sv_2io(fh_sv) ); .Ve .PP .Vb 4 \& /* init buffer */ \& Kino_New(0, outstream->buf, KINO_IO_STREAM_BUF_SIZE, char); \& outstream->buf_start = 0; \& outstream->buf_pos = 0; .Ve .PP .Vb 10 \& /* assign methods */ \& outstream->seek = Kino_OutStream_seek; \& outstream->tell = Kino_OutStream_tell; \& outstream->write_byte = Kino_OutStream_write_byte; \& outstream->write_bytes = Kino_OutStream_write_bytes; \& outstream->write_int = Kino_OutStream_write_int; \& outstream->write_long = Kino_OutStream_write_long; \& outstream->write_vint = Kino_OutStream_write_vint; \& outstream->write_vlong = Kino_OutStream_write_vlong; \& outstream->write_string = Kino_OutStream_write_string; .Ve .PP .Vb 1 \& return outstream; .Ve .PP } .PP void Kino_OutStream_seek(OutStream *outstream, double target) { Kino_OutStream_flush(outstream); outstream\->buf_start = target; PerlIO_seek(outstream\->fh, target, 0); } .PP double Kino_OutStream_tell(OutStream *outstream) { return outstream\->buf_start + outstream\->buf_pos; } .PP double Kino_OutStream_length(OutStream *outstream) { double len; .PP .Vb 5 \& /* flush, go to end, note length, return to bookmark */ \& Kino_OutStream_flush(outstream); \& PerlIO_seek(outstream->fh, 0, 2); \& len = PerlIO_tell(outstream->fh); \& PerlIO_seek(outstream->fh, outstream->buf_start, 0); .Ve .PP .Vb 2 \& return len; \&} .Ve .PP void Kino_OutStream_flush(OutStream *outstream) { PerlIO_write(outstream\->fh, outstream\->buf, outstream\->buf_pos); outstream\->buf_start += outstream\->buf_pos; outstream\->buf_pos = 0; } .PP void Kino_OutStream_absorb(OutStream *outstream, InStream *instream) { double bytes_left, bytes_this_iter; char *buf; int check_val; .PP .Vb 3 \& /* flush, then "borrow" the buffer */ \& Kino_OutStream_flush(outstream); \& buf = outstream->buf; .Ve .PP .Vb 1 \& bytes_left = instream->len; .Ve .PP .Vb 14 \& while (bytes_left > 0) { \& bytes_this_iter = bytes_left < KINO_IO_STREAM_BUF_SIZE \& ? bytes_left \& : KINO_IO_STREAM_BUF_SIZE; \& instream->read_bytes(instream, buf, bytes_this_iter); \& check_val = PerlIO_write(outstream->fh, buf, bytes_this_iter); \& if (check_val != bytes_this_iter) { \& Kino_confess("outstream->absorb error: %"UVuf", %d", \& (UV)bytes_this_iter, check_val); \& } \& bytes_left -= bytes_this_iter; \& outstream->buf_start += bytes_this_iter; \& } \&} .Ve .PP void Kino_OutStream_write_byte(OutStream *outstream, char aChar) { if (outstream\->buf_pos >= \s-1KINO_IO_STREAM_BUF_SIZE\s0) Kino_OutStream_flush(outstream); outstream\->buf[ outstream\->buf_pos++ ] = aChar; } .PP void Kino_OutStream_write_bytes(OutStream *outstream, char *bytes, \s-1STRLEN\s0 len) { /* if this data is larger than the buffer size, flush and write */ if (len >= \s-1KINO_IO_STREAM_BUF_SIZE\s0) { int check_val; Kino_OutStream_flush(outstream); check_val = PerlIO_write(outstream\->fh, bytes, len); if (check_val != len) { Kino_confess(\*(L"Write error: tried to write %\*(R"UVuf\*(L", got \f(CW%d\fR\*(R", (\s-1UV\s0)len, check_val); } outstream\->buf_start += len; } /* if there's not enough room in the buffer, flush then add */ else if (outstream\->buf_pos + len >= \s-1KINO_IO_STREAM_BUF_SIZE\s0) { Kino_OutStream_flush(outstream); Copy(bytes, (outstream\->buf + outstream\->buf_pos), len, char); outstream\->buf_pos += len; } /* if there's room, just add these bytes to the buffer */ else { Copy(bytes, (outstream\->buf + outstream\->buf_pos), len, char); outstream\->buf_pos += len; } } .PP void Kino_OutStream_write_int(OutStream *outstream, U32 aU32) { unsigned char buf[4]; Kino_encode_bigend_U32(aU32, buf); outstream\->write_bytes(outstream, (char*)buf, 4); } .PP void Kino_OutStream_write_long(OutStream *outstream, double aDouble) { unsigned char buf[8]; U32 aU32; .PP .Vb 3 \& /* derive the upper 4 bytes by truncating a quotient */ \& aU32 = floor( ldexp( aDouble, -32 ) ); \& Kino_encode_bigend_U32(aU32, buf); .Ve .PP .Vb 3 \& /* derive the lower 4 bytes by taking a modulus against 2**32 */ \& aU32 = fmod(aDouble, (pow(2.0, 32.0))); \& Kino_encode_bigend_U32(aU32, &buf[4]); .Ve .PP .Vb 3 \& /* print encoded Long to the output handle */ \& outstream->write_bytes(outstream, (char*)buf, 8); \&} .Ve .PP void Kino_OutStream_write_vint(OutStream *outstream, U32 aU32) { char buf[5]; int num_bytes; num_bytes = Kino_OutStream_encode_vint(aU32, buf); outstream\->write_bytes(outstream, buf, num_bytes); } .PP /* Encode a VInt. buf must have room for at 5 bytes. */ int Kino_OutStream_encode_vint(U32 aU32, char *buf) { int num_bytes = 0; .PP .Vb 5 \& while ((aU32 & ~0x7f) != 0) { \& buf[num_bytes++] = ( (aU32 & 0x7f) | 0x80 ); \& aU32 >>= 7; \& } \& buf[num_bytes++] = aU32 & 0x7f; .Ve .PP .Vb 2 \& return num_bytes; \&} .Ve .PP void Kino_OutStream_write_vlong(OutStream *outstream, double aDouble) { unsigned char buf[10]; int num_bytes = 0; U32 aU32; .PP .Vb 8 \& while (aDouble > 127.0) { \& /* take modulus of num against 128 */ \& aU32 = fmod(aDouble, 128); \& buf[num_bytes++] = ( (aU32 & 0x7f) | 0x80 ); \& /* right shift for floating point! */ \& aDouble = floor( ldexp( aDouble, -7 ) ); \& } \& buf[num_bytes++] = aDouble; .Ve .PP .Vb 2 \& outstream->write_bytes(outstream, (char*)buf, num_bytes); \&} .Ve .PP void Kino_OutStream_write_string(OutStream *outstream, char *string, \s-1STRLEN\s0 len) { Kino_OutStream_write_vint(outstream, (U32)len); Kino_OutStream_write_bytes(outstream, string, len); } .PP void Kino_OutStream_destroy(OutStream *outstream) { Kino_OutStream_flush(outstream); SvREFCNT_dec(outstream\->fh_sv); Kino_Safefree(outstream\->buf); Kino_Safefree(outstream); } .PP _\|_POD_\|_