Chem-Structure-Parser

 view release on metacpan or  search on metacpan

Parser.xs  view on Meta::CPAN

	} else {
		(void)hv_stores(out, "bbox",   newSV(0));
		(void)hv_stores(out, "center", newSV(0));
	}

	for (i = 0; i < NCOL; i++) {
		if (i == C_LINENO) { SvREFCNT_dec((SV *)col[i]); continue; }
		(void)hv_store(out, col_name[i], (I32)strlen(col_name[i]),
		               newRV_noinc((SV *)col[i]), 0);
	}
	(void)hv_stores(out, "res_first",     newRV_noinc((SV *)st.res_first));
	(void)hv_stores(out, "res_apart",     newRV_noinc((SV *)st.res_apart));
	SvREFCNT_dec((SV *)st.res_seen);
	SvREFCNT_dec((SV *)st.models_seen);
	if (st.build_atoms) filer_done(aTHX_ &st.filer, out);
	(void)hv_stores(out, "res_last",      newRV_noinc((SV *)st.res_last));
	//TER is a PDB record and has no mmCIF counterpart; the key is here so that
	//a caller reading it does not have to know which format the file was
	(void)hv_stores(out, "ter",           newRV_noinc((SV *)newAV()));
	(void)hv_stores(out, "meta",          newRV_noinc((SV *)meta));
	(void)hv_stores(out, "cif",           newRV_noinc((SV *)cif));
	(void)hv_stores(out, "cif_loops",     newRV_noinc((SV *)loops));
	(void)hv_stores(out, "data_block",    block ? block : newSV(0));
	(void)hv_stores(out, "model_numbers", newRV_noinc((SV *)st.model_nums));
	(void)hv_stores(out, "n_atoms",       newSVuv(st.n_atom));
	(void)hv_stores(out, "n_residues",    newSVuv((UV)(av_len(st.res_first) + 1)));
	(void)hv_stores(out, "n_models",      newSVuv(st.n_models ? st.n_models : 1));
	(void)hv_stores(out, "n_anisou",      newSVuv(st.n_anisou));
	(void)hv_stores(out, "n_skipped",     newSVuv(st.n_skipped));
	(void)hv_stores(out, "n_atom_records",   newSVuv(st.n_atom_rec));
	(void)hv_stores(out, "n_hetatm_records", newSVuv(st.n_het_rec));
	(void)hv_stores(out, "n_lines",       newSVuv(lineno));
	(void)hv_stores(out, "format",        newSVpvs("mmcif"));
	return out;
}

/*slurp() -- read the whole file.  Chunked rather than stat-then-read so that
a named pipe or /dev/stdin works the same as a file on disk.

Read through PerlIO, not stdio, so the file goes through the same layer perl's
own opens do on whatever this was built for.  The mode is "rb": PerlIOBase_pushed()
clears PERLIO_F_CRLF for a mode with a `b' in it, which is what keeps a Windows
build from translating line endings under the fixed-column reader.  The buffered
layer copies the file once more than glibc's fread() does, which on 2wy2 -- the
33 MB largest entry in PDBbind v2020 -- is 1 ms of a 23 ms read (best of seven,
warm cache) and nothing measurable on an ordinary entry.

A directory is refused before the open, because what happens when one is read is
not the same on two systems: glibc opens it and fails the first fread() with
EISDIR, Windows will not open it at all, and NetBSD hands back the raw directory
blocks, which parse as a structure with nothing in it -- a silent empty answer
for what is always a mistake.  Both of the others came back as t/errors.t
failures from 0.031 smokers (Strawberry 5.42.0 on Win2012, and perl 5.42.3 on
NetBSD 11).  structure_info() rejects a directory in Perl before it ever gets
here; this is the same complaint for a caller that reached past it.*/
static char *slurp(pTHX_ const char *CSP_RESTRICT path, STRLEN *CSP_RESTRICT lenp){
	//no CSP_RESTRICT on fh: it is perl-managed, and the layers below it alias it
	PerlIO *fh;
	char *CSP_RESTRICT buf;
	Stat_t st;
	/*1 MB to start and doubling from there, for what stat() cannot size: a
	pipe, /dev/stdin.  A regular file is allocated at its size plus one byte,
	the byte being room for the read that comes back 0 at the end, so the loop
	below never grows the buffer at all.  Doubling would have left the 33 MB
	largest entry in PDBbind v2020 in a 64 MB block, reallocated six times on
	the way, and that block lives for the whole parse.*/
	STRLEN cap = 1 << 20, len = 0;
	const bool have_stat = PerlLIO_stat(path, &st) == 0;
	if (have_stat && S_ISDIR(st.st_mode))
		croak("Chem::Structure::Parser: cannot read '%s': it is a directory", path);
	//the comparison is the test that the size fits a STRLEN at all
	if (have_stat && S_ISREG(st.st_mode) && st.st_size > 0
	    && (Off_t)(STRLEN)st.st_size == st.st_size && (STRLEN)st.st_size < (STRLEN)-1)
		cap = (STRLEN)st.st_size + 1;
	fh = PerlIO_open(path, "rb");
	if (!fh) croak("Chem::Structure::Parser: cannot read '%s': %s", path, Strerror(errno));
	Newx(buf, cap, char);
	for (;;) {
		SSize_t got;
		if (len == cap) {
			/*Only a stream reaches here, and only one past half the address
			space would wrap the doubling to a small cap -- 2 GB on a 32-bit
			perl, where that is a pipe and not unthinkable.*/
			if (cap > (STRLEN)-1 / 2) {
				Safefree(buf);
				PerlIO_close(fh);
				croak("Chem::Structure::Parser: '%s' is too large to read into memory", path);
			}
			cap *= 2;
			Renew(buf, cap, char);
		}
		got = PerlIO_read(fh, buf + len, cap - len);
		if (got <= 0) break;// 0 is end of file, -1 an error PerlIO_error() reports
		len += (STRLEN)got;
	}
	if (PerlIO_error(fh)) {
		int e = errno;
		Safefree(buf);
		PerlIO_close(fh);
		croak("Chem::Structure::Parser: error reading '%s': %s", path, Strerror(e));
	}
	PerlIO_close(fh);
	*lenp = len;
	return buf;
}

/*text_bytes() -- the bytes of a structure handed over as a string, the same
bytes whichever way perl happens to be holding it.

SvPV() hands back a string's buffer as it is stored, and a string perl has
upgraded to UTF-8 stores an author's name with one accented letter in it as a
byte longer than the same string not upgraded -- equal strings, eq says so,
that parsed differently: the upgraded one's author came back as those bytes,
the accented letter printing as two, and a character past ASCII in an ATOM
line put every column after it one out.  A caller who decoded a file before
handing it over got that.  So a string holding UTF-8 is read through a copy
downgraded to one byte per character, which every string whose characters fit
in a byte has, and that is every structure file there is.  A string with a
character beyond U+00FF cannot be downgraded and is read as the UTF-8 it
holds, which is what reading the file it came from would have given.

The string is fetched once, so a tied one is read once, and the flag is asked
after the fetch, which is when it is true of what was fetched.  The copy is
mortal and lives until the XSUB that asked for it has returned, and is only
read, as the whole-file buffer is.*/
//text is not CSP_RESTRICT: it is a perl SV, and perl may alias it
static const char *text_bytes(pTHX_ SV *text, STRLEN *CSP_RESTRICT len){
	const char *p = SvPV_const(text, *len);



( run in 0.849 second using v1.01-cache-2.11-cpan-eb134014566 )