.\" 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::InStream 3" .TH KinoSearch::Store::InStream 3 "2008-01-14" "perl v5.8.8" "User Contributed Perl Documentation" InStream* new(class, fh_sv, ...) char *class; \s-1SV\s0 *fh_sv; \&\s-1PREINIT:\s0 double offset = 0; double len = \-1; \&\s-1CODE:\s0 if (items > 2) { SV* offset_sv; offset_sv = \s-1\fIST\s0\fR\|(2); if (SvOK(offset_sv)) offset = SvNV(offset_sv); } if (items > 3) { \s-1SV\s0 *len_sv; len_sv = \s-1\fIST\s0\fR\|(3); if (SvOK(len_sv)) len = SvNV(len_sv); } \s-1RETVAL\s0 = Kino_InStream_new(class, fh_sv, offset, len); \&\s-1OUTPUT:\s0 \s-1RETVAL\s0 .PP SV* _set_or_get(instream, ...) InStream *instream; \&\s-1ALIAS:\s0 set_len = 1 get_len = 2 set_offset = 3 get_offset = 4 set_fh = 5 get_fh = 6 \&\s-1CODE:\s0 { \s-1KINO_START_SET_OR_GET_SWITCH\s0 .PP .Vb 4 \& case 1: instream->len = SvNV( ST(1) ); \& /* fall through */ \& case 2: RETVAL = newSVnv(instream->len); \& break; .Ve .PP .Vb 4 \& case 3: instream->offset = SvNV( ST(1) ); \& /* fall through */ \& case 4: RETVAL = newSVnv(instream->offset); \& break; .Ve .PP .Vb 4 \& case 5: Kino_confess("Can't set_fh"); \& /* fall through */ \& case 6: RETVAL = newSVsv(instream->fh_sv); \& break; .Ve .PP .Vb 3 \& KINO_END_SET_OR_GET_SWITCH \&} \&OUTPUT: RETVAL .Ve .PP void lu_read (instream, template_sv) InStream *instream; \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 */ char sym; /* the current symbol in the template */ char countsym; /* used when calculating repeat counts */ \s-1IV\s0 aIV; \s-1SV\s0 *aSV; char aChar; char* string; \s-1STRLEN\s0 len; \&\s-1PPCODE:\s0 { /* prepare template string pointers */ template = SvPV(template_sv, tpt_len); tpt_end = SvEND(template_sv); .PP .Vb 7 \& repeat_count = 0; \& while (1) { \& if (repeat_count == 0) { \& /* fast-forward past space characters */ \& while (*template == ' ' && template < tpt_end) { \& template++; \& } .Ve .PP .Vb 4 \& /* break out of the loop if we've exhausted the template */ \& if (template == tpt_end) { \& break; \& } .Ve .PP .Vb 3 \& /* derive the current symbol and a possible digit repeat sym */ \& sym = *template++; \& countsym = *template; .Ve .PP .Vb 21 \& if (template == tpt_end) { \& /* sym is last char in template, so process once */ \& 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 3 \& /* thwart potential infinite loop */ \& if (repeat_count < 1) \& Kino_confess( "invalid repeat_count: %d", repeat_count); .Ve .PP .Vb 1 \& switch(sym) { .Ve .PP .Vb 9 \& case 'a': /* arbitrary binary data */ \& len = repeat_count; \& repeat_count = 1; \& aSV = newSV(len + 1); \& SvCUR_set(aSV, len); \& SvPOK_on(aSV); \& string = SvPVX(aSV); \& instream->read_bytes(instream, string, len); \& break; .Ve .PP .Vb 9 \& case 'b': /* signed byte */ \& case 'B': /* unsigned byte */ \& aChar = instream->read_byte(instream); \& if (sym == 'b') \& aIV = aChar; \& else \& aIV = (unsigned char)aChar; \& aSV = newSViv(aIV); \& break; .Ve .PP .Vb 3 \& case 'i': /* signed 32-bit integer */ \& aSV = newSViv( (I32)instream->read_int(instream) ); \& break; .Ve .PP .Vb 3 \& case 'I': /* unsigned 32-bit integer */ \& aSV = newSVuv( instream->read_int(instream) ); \& break; .Ve .PP .Vb 3 \& case 'Q': /* unsigned "64-bit integer" */ \& aSV = newSVnv( instream->read_long(instream) ); \& break; .Ve .PP .Vb 8 \& case 'T': /* string */ \& len = instream->read_vint(instream); \& aSV = newSV(len + 1); \& SvCUR_set(aSV, len); \& SvPOK_on(aSV); \& string = SvPVX(aSV); \& instream->read_chars(instream, string, 0, len); \& break; .Ve .PP .Vb 3 \& case 'V': /* VInt */ \& aSV = newSVuv( instream->read_vint(instream) ); \& break; .Ve .PP .Vb 3 \& case 'W': /* VLong */ \& aSV = newSVnv( instream->read_vlong(instream) ); \& break; .Ve .PP .Vb 4 \& default: \& aSV = NULL; /* suppress unused var compiler warning */ \& Kino_confess("Invalid type in template: '%c'", sym); \& } .Ve .PP .Vb 5 \& /* Put a scalar on the stack, use up one symbol or repeater */ \& XPUSHs( sv_2mortal(aSV) ); \& repeat_count -= 1; \& } \&} .Ve .PP void \&\s-1DESTROY\s0(instream) InStream *instream; \&\s-1PPCODE:\s0 Kino_InStream_destroy(instream); .PP _\|_H_\|_ .PP #ifndef H_KINOSEARCH_STORE_INSTREAM #define H_KINOSEARCH_STORE_INSTREAM 1 .PP #include \*(L"\s-1EXTERN\s0.h\*(R" #include \*(L"perl.h\*(R" #include \*(L"\s-1XSUB\s0.h\*(R" #include \*(L"KinoSearchUtilCarp.h\*(R" #include \*(L"KinoSearchUtilMathUtils.h\*(R" .PP /* Detect whether we're on an \s-1ASCII\s0 or \s-1EBCDIC\s0 machine. */ #if '0' == 240 #define \s-1KINO_NUM_CHAR_OFFSET\s0 240 #else #define \s-1KINO_NUM_CHAR_OFFSET\s0 48 #endif .PP #define \s-1KINO_IO_STREAM_BUF_SIZE\s0 1024 .PP typedef struct instream { PerlIO *fh; \s-1SV\s0 *fh_sv; double offset; double len; char *buf; Off_t buf_start; /* file position of start of buffer */ int buf_len; /* number of valid bytes in the buffer */ int buf_pos; /* next byte to read */ void (*seek)(struct instream*, double); double (*tell)(struct instream*); char (*read_byte)(struct instream*); void (*read_bytes)(struct instream*, char*, \s-1STRLEN\s0); void (*read_chars)(struct instream*, char*, \s-1STRLEN\s0, \s-1STRLEN\s0); U32 (*read_int)(struct instream*); double (*read_long)(struct instream*); U32 (*read_vint)(struct instream*); double (*read_vlong)(struct instream*); } InStream; .PP InStream* Kino_InStream_new (char*, SV*, double, double); void Kino_InStream_seek (InStream*, double); double Kino_InStream_tell (InStream*); void Kino_InStream_refill (InStream*); char Kino_InStream_read_byte (InStream*); void Kino_InStream_read_bytes (InStream*, char*, \s-1STRLEN\s0); void Kino_InStream_read_chars (InStream*, char*, \s-1STRLEN\s0, \s-1STRLEN\s0); U32 Kino_InStream_read_int (InStream*); double Kino_InStream_read_long (InStream*); U32 Kino_InStream_decode_vint(char**); U32 Kino_InStream_read_vint (InStream*); double Kino_InStream_read_vlong (InStream*); void Kino_InStream_destroy (InStream*); .PP #endif /* include guard */ .PP _\|_C_\|_ .PP #include \*(L"KinoSearchStoreInStream.h\*(R" .PP InStream* Kino_InStream_new(char *class, \s-1SV\s0 *fh_sv, double offset, double len ) { InStream *instream; .PP .Vb 2 \& /* allocate */ \& Kino_New(0, instream, 1, InStream); .Ve .PP .Vb 4 \& /* assign */ \& instream->fh_sv = newSVsv(fh_sv); \& instream->fh = IoIFP( sv_2io(fh_sv) ); \& instream->offset = offset; .Ve .PP .Vb 5 \& /* init buffer */ \& instream->buf = NULL; \& instream->buf_start = 0; \& instream->buf_len = 0; \& instream->buf_pos = 0; .Ve .PP .Vb 4 \& /* seek */ \& if (offset != 0) { \& PerlIO_seek(instream->fh, offset, 0); \& } .Ve .PP .Vb 8 \& /* calculate len if an (intentionally) invalid value was supplied */ \& if (len < 0.0) { \& double bookmark = PerlIO_tell(instream->fh); \& PerlIO_seek(instream->fh, 0, 2); \& len = PerlIO_tell(instream->fh); \& PerlIO_seek(instream->fh, bookmark, 0); \& } \& instream->len = len; .Ve .PP .Vb 10 \& /* assign methods */ \& instream->seek = Kino_InStream_seek; \& instream->tell = Kino_InStream_tell; \& instream->read_byte = Kino_InStream_read_byte; \& instream->read_bytes = Kino_InStream_read_bytes; \& instream->read_chars = Kino_InStream_read_chars; \& instream->read_int = Kino_InStream_read_int; \& instream->read_long = Kino_InStream_read_long; \& instream->read_vint = Kino_InStream_read_vint; \& instream->read_vlong = Kino_InStream_read_vlong; .Ve .PP .Vb 2 \& return instream; \&} .Ve .PP void Kino_InStream_seek(InStream *instream, double target) { /* seek within buffer if possible */ if ( (target >= instream\->buf_start) && (target < (instream\->buf_start + instream\->buf_pos)) ) { instream\->buf_pos = target \- instream\->buf_start; } /* nope, not possible, so seek within file and prepare to refill */ else { instream\->buf_start = target; instream\->buf_pos = 0; instream\->buf_len = 0; PerlIO_seek(instream\->fh, target + instream\->offset, 0); } } .PP double Kino_InStream_tell(InStream *instream) { return instream\->buf_start + instream\->buf_pos; } .PP void Kino_InStream_refill(InStream *instream) { int check_val; .PP .Vb 3 \& /* wait to allocate buffer until it's needed */ \& if (instream->buf == NULL) \& Kino_New(0, instream->buf, KINO_IO_STREAM_BUF_SIZE, char); .Ve .PP .Vb 3 \& /* add bytes read to file position, reset */ \& instream->buf_start += instream->buf_pos; \& instream->buf_pos = 0; .Ve .PP .Vb 5 \& /* calculate the number of bytes to read */ \& if (KINO_IO_STREAM_BUF_SIZE < instream->len - instream->buf_start) \& instream->buf_len = KINO_IO_STREAM_BUF_SIZE; \& else \& instream->buf_len = instream->len - instream->buf_start; .Ve .PP .Vb 11 \& /* perform the file operations */ \& PerlIO_seek(instream->fh, 0, 1); \& check_val = PerlIO_seek(instream->fh, \& (instream->buf_start + instream->offset), 0); \& if (check_val == -1) \& Kino_confess("refill: PerlIO_seek failed: %d", errno); \& check_val = PerlIO_read(instream->fh, instream->buf, instream->buf_len); \& if (check_val != instream->buf_len) \& Kino_confess("refill: tried to read %d bytes, got %d: %d", \& instream->buf_len, check_val, errno); \&} .Ve .PP char Kino_InStream_read_byte(InStream *instream) { if (instream\->buf_pos >= instream\->buf_len) Kino_InStream_refill(instream); return instream\->buf[ instream\->buf_pos++ ]; } .PP void Kino_InStream_read_bytes (InStream *instream, char* buf, \s-1STRLEN\s0 len) { if (instream\->buf_pos + len < instream\->buf_len) { /* request is entirely within buffer, so copy */ Copy((instream\->buf + instream\->buf_pos), buf, len, char); instream\->buf_pos += len; } else { /* get the request from the file and reset buffer */ int check_val; Off_t start; start = instream\->tell(instream); check_val = PerlIO_seek(instream\->fh, (start + instream\->offset), 0); if (check_val == \-1) Kino_confess(\*(L"read_bytes: PerlIO_seek failed: \f(CW%d\fR\*(R", errno ); check_val = PerlIO_read(instream\->fh, buf, len); if (check_val < len) Kino_confess(\*(L"read_bytes: tried to read %\*(R"UVuf\*(L" bytes, got \f(CW%d\fR\*(R", (\s-1UV\s0)len, check_val); .PP .Vb 8 \& /* reset vars and refill if there's more in the file */ \& instream->buf_start = start + len; \& instream->buf_pos = 0; \& instream->buf_len = 0; \& if (instream->buf_start < instream->len) \& Kino_InStream_refill(instream); \& } \&} .Ve .PP /* This is just a wrapper for read_bytes, but that may change. It should * be used whenever Lucene character data is being read, typically after * read_vint as part of a String read. If and when a change does come, it will * be a lot easier to track down all the relevant code fragments if read_chars * gets used consistently. */ void Kino_InStream_read_chars(InStream *instream, char *buf, \s-1STRLEN\s0 start, \s-1STRLEN\s0 len) { buf += start; instream\->read_bytes(instream, buf, len); } .PP U32 Kino_InStream_read_int (InStream *instream) { unsigned char buf[4]; instream\->read_bytes(instream, (char*)buf, 4); return Kino_decode_bigend_U32(buf); } .PP double Kino_InStream_read_long (InStream *instream) { unsigned char buf[8]; double aDouble; .PP .Vb 2 \& /* get 8 bytes from the stream */ \& instream->read_bytes(instream, (char*)buf, 8); .Ve .PP .Vb 3 \& /* get high 4 bytes, multiply by 2**32 */ \& aDouble = Kino_decode_bigend_U32(buf); \& aDouble = aDouble * pow(2.0, 32.0); .Ve .PP .Vb 2 \& /* decode low four bytes as unsigned int and add to total */ \& aDouble += Kino_decode_bigend_U32(&buf[4]); .Ve .PP .Vb 2 \& return aDouble; \&} .Ve .PP /* read in a Variable INTeger, stored in 1\-5 bytes */ U32 Kino_InStream_read_vint (InStream *instream) { unsigned char aUChar; int bitshift; U32 aU32; .PP .Vb 3 \& /* start by reading one byte; use the lower 7 bits */ \& aUChar = (unsigned char)instream->read_byte(instream); \& aU32 = aUChar & 0x7f; .Ve .PP .Vb 7 \& /* keep reading and shifting as long as the high bit is set */ \& for (bitshift = 7; (aUChar & 0x80) != 0; bitshift += 7) { \& aUChar = (unsigned char)instream->read_byte(instream); \& aU32 |= (aUChar & 0x7f) << bitshift; \& } \& return aU32; \&} .Ve .PP U32 Kino_InStream_decode_vint(char **source_ptr) { char *source; int bitshift; U32 aU32; .PP .Vb 10 \& source = *source_ptr; \& aU32 = (unsigned char)*source & 0x7f; \& for (bitshift = 7; (*source & 0x80) != 0; bitshift += 7) { \& source++; \& aU32 |= ((unsigned char)*source & 0x7f) << bitshift; \& } \& source++; \& *source_ptr = source; \& return aU32; \&} .Ve .PP double Kino_InStream_read_vlong (InStream *instream) { unsigned char aUChar; int bitshift; double aDouble; .PP .Vb 8 \& aUChar = (unsigned char)instream->read_byte(instream); \& aDouble = aUChar & 0x7f; \& for (bitshift = 7; (aUChar & 0x80) != 0; bitshift += 7) { \& aUChar = (unsigned char)instream->read_byte(instream); \& aDouble += (aUChar & 0x7f) * pow(2, bitshift); \& } \& return aDouble; \&} .Ve .PP void Kino_InStream_destroy(InStream* instream) { SvREFCNT_dec(instream\->fh_sv); Kino_Safefree(instream\->buf); Kino_Safefree(instream); } .PP _\|_POD_\|_