Beispiel #1
0
static void near addripfile(char *szFilename)
{
  if (ripfilebuf || (ripfilebuf=sb_new(STRBUFSZ))!=NULL)
  {
    if (!ripfilesent(szFilename))
    {
      if (sb_alloc(ripfilebuf,szFilename)==NULL)  /* Out of space */
      {
        strbuf *newsb=sb_realloc(ripfilebuf,STRBUFSZ,0);
        if (newsb==NULL)
          sb_alloc(ripfilebuf,szFilename);
      }
    }
  }
}
Beispiel #2
0
static int read_file_pfr_psb(char* filename, int do_write) {
	byte_reader_t* pbr = stdio_byte_reader_alloc();
	string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE);
	pbr->popen_func(pbr, NULL, filename);

	peek_file_reader_t* pfr = pfr_alloc(pbr, PEEK_BUF_LEN);

	parse_trie_t* ptrie = parse_trie_alloc();
	parse_trie_add_string(ptrie, "\n", IRS_STRIDX);
	parse_trie_add_string(ptrie, "\xff", EOF_STRIDX);
	parse_trie_add_string(ptrie, "\n\xff", IRSEOF_STRIDX);

	int bc = 0;

	while (TRUE) {
		char* line = read_line_pfr_psb(pfr, psb, ptrie);
		if (line == NULL)
			break;
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		bc += strlen(line);
		free(line);
	}
	sb_free(psb);
	pbr->pclose_func(pbr, NULL);
	return bc;
}
Beispiel #3
0
static size_t rgb_info(const ss_t *rgb, const struct RGB_Info *ri)
{
	if (rgb && ri) {
		const size_t ss = ss_size(rgb), ps = ri->bpp / 8;
		size_t cmax = ri->bpp == 32 ? 0xffffffff :
					      0xffffffff &
					      ((1 << ri->bpp) - 1),
		       uqp = 0, i;
		const char *p = ss_get_buffer_r(rgb);
		sb_t *bs = sb_alloc(0);
		sb_eval(&bs, cmax);
		switch (ps) {
		case 4: RGB_COUNT_LOOP(i, p, ss, ps, bs, cmax, uqp); break;
		case 3: RGB_COUNT_LOOP(i, p, ss, ps, bs, cmax, uqp); break;
		case 2: RGB_COUNT_LOOP(i, p, ss, ps, bs, cmax, uqp); break;
		case 1: RGB_COUNT_LOOP(i, p, ss, ps, bs, cmax, uqp); break;
		default:
			break;
		}
		unsigned pixels = (unsigned)(ri->width * ri->height);
		unsigned l = (uqp * 100) / pixels,
			 r = ((uqp * 10000) / pixels) % 100;
		printf("%ix%i %i bpp, %i chn; %u px; %u unique px"
		       " (%u.%u%%)", (int)ri->width, (int)ri->height,
		       (int)ri->bpp, (int)ri->chn, pixels,
		       (unsigned)uqp, l, r);
		if (ps == 3 || ps == 4) {
			size_t ur, ug, ub, ua, urg, ugb, urb;
			RGBC_COUNT_LOOP(i, p, ss, ps, bs, 0, ur);
			RGBC_COUNT_LOOP(i, p, ss, ps, bs, 0, ug);
			RGBC_COUNT_LOOP(i, p, ss, ps, bs, 0, ub);
			RGBC2_COUNT_LOOP(i, p, ss, ps, bs, 0, urg);
			RGBC2_COUNT_LOOP(i, p, ss, ps, bs, 1, ugb);
			RGBC2_COUNT_LOOP(i, p, ss, ps, bs, 2, urb);
			if (ps == 4) {
				RGBC_COUNT_LOOP(i, p, ss, ps, bs, 0, ua);
			} else {
				ua = 0;
			}
			printf("; %u ur; %u ug; %u ub; %u urg; %u ugb; %u urb;"
			       " %u ua", (unsigned)ur, (unsigned)ug,
			       (unsigned)ub, (unsigned)urg, (unsigned)ugb,
			       (unsigned)urb, (unsigned)ua);
		}
		printf("\n");
	} else {
		fprintf(stderr, "Error: no pixel data\n");
	}
	return ss_size(rgb);
}
Beispiel #4
0
// ----------------------------------------------------------------
lrec_reader_t* lrec_reader_mmap_csv_alloc(char* irs, char* ifs, int use_implicit_header) {
	lrec_reader_t* plrec_reader = mlr_malloc_or_die(sizeof(lrec_reader_t));

	lrec_reader_mmap_csv_state_t* pstate = mlr_malloc_or_die(sizeof(lrec_reader_mmap_csv_state_t));
	pstate->ilno          = 0LL;

	pstate->eof           = "\xff";
	pstate->irs           = irs;
	pstate->ifs           = ifs;
	pstate->ifs_eof       = mlr_paste_2_strings(pstate->ifs, "\xff");
	pstate->dquote        = "\"";

	pstate->dquote_irs    = mlr_paste_2_strings("\"", pstate->irs);
	pstate->dquote_ifs    = mlr_paste_2_strings("\"", pstate->ifs);
	pstate->dquote_eof    = "\"\xff";
	pstate->dquote_dquote = "\"\"";

	pstate->dquotelen     = strlen(pstate->dquote);

	pstate->pno_dquote_parse_trie = parse_trie_alloc();
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->irs,     IRS_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->ifs,     IFS_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->dquote,  DQUOTE_STRIDX);

	pstate->pdquote_parse_trie = parse_trie_alloc();
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_irs,    DQUOTE_IRS_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_ifs,    DQUOTE_IFS_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_dquote, DQUOTE_DQUOTE_STRIDX);

	pstate->pfields = rslls_alloc();
	pstate->psb = sb_alloc(STRING_BUILDER_INIT_SIZE);

	pstate->expect_header_line_next   = use_implicit_header ? FALSE : TRUE;
	pstate->use_implicit_header       = use_implicit_header;
	pstate->pheader_keeper            = NULL;
	pstate->pheader_keepers           = lhmslv_alloc();

	plrec_reader->pvstate       = (void*)pstate;
	plrec_reader->popen_func    = file_reader_mmap_vopen;
	plrec_reader->pclose_func   = file_reader_mmap_vclose;
	plrec_reader->pprocess_func = lrec_reader_mmap_csv_process;
	plrec_reader->psof_func     = lrec_reader_mmap_csv_sof;
	plrec_reader->pfree_func    = lrec_reader_mmap_csv_free;

	return plrec_reader;
}
Beispiel #5
0
rval_evaluator_t* rval_evaluator_alloc_from_x_sr_func(mv_binary_arg2_regex_func_t* pfunc,
	rval_evaluator_t* parg1, char* regex_string, int ignore_case)
{
	rval_evaluator_x_sr_state_t* pstate = mlr_malloc_or_die(sizeof(rval_evaluator_x_sr_state_t));
	pstate->pfunc = pfunc;
	pstate->parg1 = parg1;

	int cflags = ignore_case ? REG_ICASE : 0;
	regcomp_or_die(&pstate->regex, regex_string, cflags);
	pstate->psb = sb_alloc(MV_SB_ALLOC_LENGTH);

	rval_evaluator_t* pevaluator = mlr_malloc_or_die(sizeof(rval_evaluator_t));
	pevaluator->pvstate = pstate;
	pevaluator->pprocess_func = rval_evaluator_x_sr_func;
	pevaluator->pfree_func = rval_evaluator_x_sr_free;

	return pevaluator;
}
Beispiel #6
0
sb_string_t sb_string_create_n(const sb_char_t *s, sb_size_t n)
{
    sb_strreal_t *sr;

    sr = (sb_strreal_t *) sb_alloc(sizeof(sb_strreal_t) + n);
    if (!sr) {
        return nullptr;
    }

    sr->length = n;
    sr->free   = 0;

    if (n && s) {
        sb_memcopy(sr->buffer, s, n);
    }

    sr->buffer[n] = '\0';

    return (sb_string_t) sr->buffer;
}
Beispiel #7
0
static int read_file_mmap_psb(char* filename, int do_write) {
	file_reader_mmap_state_t* ph = file_reader_mmap_open(NULL, filename);
	string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE);
	char* irs = "\n";
	int bc = 0;

	while (TRUE) {
		char* line = read_line_mmap_psb(ph, psb, irs);
		if (line == NULL)
			break;
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		bc += strlen(line);
	}
	sb_free(psb);
	file_reader_mmap_close(ph, NULL);
	return bc;
}
Beispiel #8
0
char* lrec_sprint(lrec_t* prec, char* ors, char* ofs, char* ops) {
	string_builder_t* psb = sb_alloc(SB_ALLOC_LENGTH);
	if (prec == NULL) {
		sb_append_string(psb, "NULL");
	} else {
		int nf = 0;
		for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) {
			if (nf > 0)
				sb_append_string(psb, ofs);
			sb_append_string(psb, pe->key);
			sb_append_string(psb, ops);
			sb_append_string(psb, pe->value);
			nf++;
		}
		sb_append_string(psb, ors);
	}
	char* rv = sb_finish(psb);
	sb_free(psb);
	return rv;
}
Beispiel #9
0
static int read_file_fgetc_psb(char* filename, int do_write) {
	FILE* fp = fopen_or_die(filename);
	string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE);
	char* irs = "\n";
	int bc = 0;

	while (TRUE) {
		char* line = read_line_fgetc_psb(fp, psb, irs);
		if (line == NULL)
			break;
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		bc += strlen(line);
		free(line);
	}
	sb_free(psb);
	fclose(fp);
	return bc;
}
Beispiel #10
0
int main(int argc, const char **argv)
{
	if (argc < 3)
		return syntax_error(argv, 5);
	int csize = atoi(argv[1]), climit0 = atoi(argv[2]);
	if (csize < 1 || csize > 4)
		return syntax_error(argv, 6);
	if (climit0 < 0)
		return syntax_error(argv, 7);
	int exit_code = 0;
	size_t count = 0;
	size_t cmax = csize == 4 ? 0xffffffff :
				   0xffffffff & ((1 << (csize * 8)) - 1);
	size_t climit = climit0 ? S_MIN((size_t)climit0, cmax) : cmax;
#ifdef COUNTER_USE_BITSET
	#define COUNTER_SET(val) sb_set(&bs, val)
	#define COUNTER_POPCOUNT sb_popcount(bs)
	sb_t *bs = sb_alloc(0);
	sb_eval(&bs, cmax);
#else
	#define COUNTER_SET(val) sm_uu32_insert(&m, val, 1)
	#define COUNTER_POPCOUNT sm_size(m)
	sm_t *m = sm_alloc(SM_U32U32, 0);
#endif
	unsigned char buf[3 * 4 * 128];
	int i;
	ssize_t l;
	for (;;) {
		l = read(0, buf, sizeof(buf));
		l = (l / csize) * csize;
		if (l <= 0)
			break;
		#define CNTLOOP(inc, val)			\
			for (i = 0; i < l; i += inc) {		\
				COUNTER_SET(val);		\
				count++;			\
				if (COUNTER_POPCOUNT >= climit)	\
					goto done;		\
			}
		switch (csize) {
		case 1:	CNTLOOP(1, buf[i]);
			break;
		case 2:	CNTLOOP(2, (size_t)(buf[i] << 8 | buf[i + 1]));
			break;
		case 3:	CNTLOOP(3, (size_t)(buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2]));
			break;
		case 4:	CNTLOOP(4, (size_t)buf[i] << 24 |
				   (size_t)buf[i + 1] << 16 |
				   (size_t)buf[i + 2] << 8 |
				   (size_t)buf[i + 3]);
			break;
		default:
			goto done;
		}
		#undef CNTLOOP
	}
done:
	printf(FMT_ZU ", " FMT_ZU, count, COUNTER_POPCOUNT);
#ifdef COUNTER_USE_BITSET
	sb_free(&bs);
#else
	sm_free(&m);
#endif
	return exit_code;
}
Beispiel #11
0
// ----------------------------------------------------------------
lrec_reader_t* lrec_reader_stdio_csv_alloc(char* irs, char* ifs, int use_implicit_header,
	comment_handling_t comment_handling, char* comment_string)
{
	lrec_reader_t* plrec_reader = mlr_malloc_or_die(sizeof(lrec_reader_t));

	lrec_reader_stdio_csv_state_t* pstate = mlr_malloc_or_die(sizeof(lrec_reader_stdio_csv_state_t));
	pstate->ilno          = 0LL;

	pstate->do_auto_line_term = FALSE;
	if (streq(irs, "auto")) {
		irs = "\n";
		pstate->do_auto_line_term = TRUE;
	}

	pstate->comment_handling = comment_handling;
	pstate->comment_string   = comment_string;
	pstate->comment_string_length = comment_string == NULL ? 0 : strlen(comment_string);

	pstate->eof           = "\xff";
	pstate->irs           = irs;
	pstate->ifs           = ifs;
	pstate->ifs_eof       = mlr_paste_2_strings(pstate->ifs, "\xff");
	pstate->dquote        = "\"";

	pstate->dquote_ifs    = mlr_paste_2_strings("\"", pstate->ifs);
	pstate->dquote_eof    = "\"\xff";
	pstate->dquote_dquote = "\"\"";

	pstate->dquotelen     = strlen(pstate->dquote);


	// Parse trie for UTF-8 BOM
	pstate->putf8_bom_parse_trie = parse_trie_alloc();
	parse_trie_add_string(pstate->putf8_bom_parse_trie, UTF8_BOM, UTF8_BOM_STRIDX);

	// Parse trie for non-double-quoted fields
	pstate->pno_dquote_parse_trie = parse_trie_alloc();
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->eof,     EOF_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->irs,     IRS_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->ifs_eof, IFS_EOF_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->ifs,     IFS_STRIDX);
	parse_trie_add_string(pstate->pno_dquote_parse_trie, pstate->dquote,  DQUOTE_STRIDX);

	// Parse trie for double-quoted fields
	pstate->pdquote_parse_trie = parse_trie_alloc();
	if (pstate->do_auto_line_term) {
		pstate->dquote_irs  = mlr_paste_2_strings("\"", "\n");
		pstate->dquote_irs2 = mlr_paste_2_strings("\"", "\r\n");
		parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_irs,  DQUOTE_IRS_STRIDX);
		parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_irs2, DQUOTE_IRS2_STRIDX);
	} else {
		pstate->dquote_irs  = mlr_paste_2_strings("\"", pstate->irs);
		pstate->dquote_irs2 = NULL;
		parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_irs, DQUOTE_IRS_STRIDX);
	}
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->eof,           EOF_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_irs,    DQUOTE_IRS_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_ifs,    DQUOTE_IFS_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_eof,    DQUOTE_EOF_STRIDX);
	parse_trie_add_string(pstate->pdquote_parse_trie, pstate->dquote_dquote, DQUOTE_DQUOTE_STRIDX);


	pstate->pfields = rslls_alloc();
	pstate->psb = sb_alloc(STRING_BUILDER_INIT_SIZE);
	pstate->pbr = stdio_byte_reader_alloc();
	pstate->pfr = pfr_alloc(pstate->pbr, mlr_imax3(
		pstate->putf8_bom_parse_trie->maxlen,
		pstate->pno_dquote_parse_trie->maxlen,
		pstate->pdquote_parse_trie->maxlen));

	pstate->expect_header_line_next   = use_implicit_header ? FALSE : TRUE;
	pstate->use_implicit_header       = use_implicit_header;
	pstate->pheader_keeper            = NULL;
	pstate->pheader_keepers           = lhmslv_alloc();

	plrec_reader->pvstate       = (void*)pstate;
	plrec_reader->popen_func    = lrec_reader_stdio_csv_open;
	plrec_reader->pclose_func   = lrec_reader_stdio_csv_close;
	plrec_reader->pprocess_func = lrec_reader_stdio_csv_process;
	plrec_reader->psof_func     = lrec_reader_stdio_csv_sof;
	plrec_reader->pfree_func    = lrec_reader_stdio_csv_free;

	return plrec_reader;
}
Beispiel #12
0
void process_block(const char * block, int block_size) {
	static enum {
		HTTP_HEADER,
		HEADER_NAME,
		HEADER_VALUE,
		BODY
	} state = HTTP_HEADER;

	static string_buffer buffer;
	static int sb_initialized = 0;

	if(!sb_initialized) {
		sb_initialized = 1;
		buffer = sb_alloc();
	}

	if(block == NULL) {
		state = HTTP_HEADER;
	}

	int i = -1;

	switch(state) {
		case HTTP_HEADER: goto _0;
		case HEADER_NAME: goto _1;
		case HEADER_VALUE: goto _2;
		case BODY: goto _3;
	}

_0:
	while(++i < block_size) {
		if(block[i] == '\n') {
			state = HEADER_NAME;
			printf("Got HTTP header: \"%s\"\n", sb_tostring(buffer));
			sb_free(buffer);
			buffer = sb_alloc();
			goto _1;
		}

		if(block[i] != '\r') /* Hm... */
			sb_append(&buffer, block[i]);
	}

	return;

_1:
	while(++i < block_size) {
		if(block[i] == '\n') {
			/* TODO: Check that current header name is empty */
			state = BODY;
			goto _3;
		} else if(block[i] == ':') {
			state = HEADER_VALUE;
			printf("Got header name: \"%s\"\n", sb_tostring(buffer));
			sb_free(buffer);
			buffer = sb_alloc();
			goto _2;
		}

		if(block[i] != '\r') /* Hm... */
			sb_append(&buffer, block[i]);
	}

	return;

_2:
	while(++i < block_size) {
		if(block[i] == '\n') {
			state = HEADER_NAME;
			printf("Got header value: \"%s\"\n", sb_tostring(buffer));
			sb_free(buffer);
			buffer = sb_alloc();
			goto _1;
		}

		if(block[i] != '\r') /* Hm... */
			sb_append(&buffer, block[i]);
	}

	return;

_3:
	while(++i < block_size) {
		sb_append(&buffer, block[i]);
	}

	printf("Got request body: \"%s\"\n", sb_tostring(buffer));
	sb_free(buffer);
	buffer = sb_alloc();
}