/* return a new strng, made from a slice of an old strng. Should obey python-style slicing semantics, eg: start & end are positions between characters. start & end can be negative, then they count from the end of the string. start & end can be STRNG_OPEN, then they count up to the end (or start). */ jt_strng* jt_strng_slice(jt_strng* in, int start, int end) { int length = jt_strng_len(in), i; jt_strng* newstrng = 0; if (start==STRNG_OPEN) start = 0; if (end==STRNG_OPEN) end = length; if (start<0) start += length; if (end<0) end += length; if (start<0) start = 0; if (end>length) end = length; if (end <= start) { /* empty */ return jt_strng_new(""); } newstrng = jt_buffer_new(1+(end-start)); for (i=start; i<end; i++) { ((char*)newstrng->buffer)[i-start] = ((char*)in->buffer)[i]; } /* zero-terminate */ ((char*)newstrng->buffer)[end-start] = '\0'; newstrng->length = 1+(end-start); return newstrng; }
jt_strng* jt_strng_new(const char* val) { int len = strlen(val)+1; jt_strng* newstring = jt_buffer_new(len); strcpy(newstring->buffer, val); newstring->length = len; return newstring; }
uint5 loadsection(section* sec, FILE* f) { sec->content = jt_buffer_new(8); if (sec->header.sh_size > 0) { int expectedsize = pad(sec->header.sh_size); if (expectedsize > 300000000) { fprintf(stderr, "Very large section! %d bytes!\n", expectedsize); exit(1); } jt_buffer_extend(sec->content, expectedsize); return fread(sec->content->buffer, expectedsize, 1, f) * expectedsize; } return 0; }