void run_cg(std::istream& is, std::ostream& os, const hfst::HfstTransducer& t) { std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv; // Simple debug function std::ostringstream ss; for (std::string line; std::getline(is, line);) { os << line << std::endl; std::match_results<const char*> result; std::regex_match(line.c_str(), result, CG_LINE); if(!result.empty() && result[3].length() != 0) { const auto& sugg = proc_line(t, line); if(sugg.suggest) { const auto& ana = sugg.ana; const auto& formv = sugg.sforms; if(formv.empty()) { os << ana << "\t" << "?" << std::endl; } else { os << ana << "\t" << u16join(formv) << std::endl; } } else { const auto& errtype = sugg.errtype; if(!errtype.empty()) { os << utf16conv.to_bytes(errtype) << std::endl; } } } } }
int main(int argc, char *argv[]) { static char dflt_fmt[] = "%Y-%m-%d\n\0H:%M:%S %Z\n"; yuck_t argi[1U]; char *outfmt = dflt_fmt; char **infmt; size_t ninfmt; char **input; size_t ninput; int quietp; int res = 0; if (yuck_parse(argi, argc, argv)) { res = 1; goto out; } if (argi->format_arg) { outfmt = argi->format_arg; /* unescape sequences, maybe */ if (argi->backslash_escapes_flag) { dt_io_unescape(outfmt); } } else if (argi->time_flag) { outfmt[8] = ' '; outfmt[9] = '%'; } if (!argi->input_format_nargs) { infmt = argi->args; ninfmt = argi->nargs; input = NULL; ninput = 0; } else { infmt = argi->input_format_args; ninfmt = argi->input_format_nargs; input = argi->args; ninput = argi->nargs; } /* get quiet predicate */ quietp = argi->quiet_flag; /* get lines one by one, apply format string and print date/time */ if (ninput == 0) { /* read from stdin */ proc_lines((const char*const*)infmt, ninfmt, outfmt, quietp); } else { const char *const *cinfmt = (const char*const*)infmt; for (size_t i = 0; i < ninput; i++) { proc_line(input[i], cinfmt, ninfmt, outfmt, quietp); } } out: yuck_free(argi); return res; }
int main(int argc, const char * argv[]) { FILE *file = fopen(argv[1], "r"); char line[1024]; while (fgets(line, 1024, file)) { proc_line(line); } return 0; }
static void mass_add_dur(const struct mass_add_clo_s *clo) { /* read lines from stdin * interpret as dates * add to reference duration * output */ size_t lno = 0; for (char *line; prchunk_haslinep(clo->pctx); lno++) { size_t llen = prchunk_getline(clo->pctx, &line); proc_line(clo, line, llen); } return; }
static void proc_lines(const char *const *fmt, size_t nfmt, const char *ofmt, int quietp) { size_t lno = 0; void *pctx; /* using the prchunk reader now */ if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) { serror("Error: could not open stdin"); return; } while (prchunk_fill(pctx) >= 0) { for (char *line; prchunk_haslinep(pctx); lno++) { (void)prchunk_getline(pctx, &line); /* check if line matches */ proc_line(line, fmt, nfmt, ofmt, quietp); } } /* get rid of resources */ free_prchunk(pctx); return; }
int main(int argc, char *argv[]) { static char dflt_fmt[] = "%Y-%m-%d\n\0H:%M:%S %Z\n"; yuck_t argi[1U]; char *outfmt = dflt_fmt; char **infmt; size_t ninfmt; char **input; size_t ninput; int quietp; int res = 0; if (yuck_parse(argi, argc, argv)) { res = 1; goto out; } if (argi->format_arg) { outfmt = argi->format_arg; /* unescape sequences, maybe */ if (argi->backslash_escapes_flag) { dt_io_unescape(outfmt); } } else if (argi->time_flag) { outfmt[8] = ' '; outfmt[9] = '%'; } if (!argi->input_format_nargs) { infmt = argi->args; ninfmt = argi->nargs; input = NULL; ninput = 0; } else { infmt = argi->input_format_args; ninfmt = argi->input_format_nargs; input = argi->args; ninput = argi->nargs; } /* get quiet predicate */ quietp = argi->quiet_flag; /* set locale specific/independent behaviour */ with (const char *loc) { if (!argi->locale_flag) { loc = "C"; /* we need to null out TZ for UTC */ setenv("TZ", "", 1); } else { loc = ""; } /* actually set our findings in stone */ setlocale(LC_TIME, loc); tzset(); } /* get lines one by one, apply format string and print date/time */ if (ninput == 0) { /* read from stdin */ proc_lines((const char*const*)infmt, ninfmt, outfmt, quietp); } else { const char *const *cinfmt = (const char*const*)infmt; for (size_t i = 0; i < ninput; i++) { proc_line(input[i], cinfmt, ninfmt, outfmt, quietp); } } out: yuck_free(argi); return res; }
int main(int argc, char *argv[]) { struct gengetopt_args_info argi[1]; const char *ofmt; char **fmt; size_t nfmt; int res = 0; zif_t fromz = NULL; zif_t z = NULL; if (cmdline_parser(argc, argv, argi)) { res = 1; goto out; } /* init and unescape sequences, maybe */ ofmt = argi->format_arg; fmt = argi->input_format_arg; nfmt = argi->input_format_given; if (argi->backslash_escapes_given) { dt_io_unescape(argi->format_arg); for (size_t i = 0; i < nfmt; i++) { dt_io_unescape(fmt[i]); } } /* try and read the from and to time zones */ if (argi->from_zone_given) { fromz = zif_open(argi->from_zone_arg); } if (argi->zone_given) { z = zif_open(argi->zone_arg); } if (argi->default_given) { struct dt_dt_s dflt = dt_strpdt(argi->default_arg, NULL, NULL); dt_set_default(dflt); } if (argi->inputs_num) { for (size_t i = 0; i < argi->inputs_num; i++) { const char *inp = argi->inputs[i]; struct dt_dt_s d = dt_io_strpdt(inp, fmt, nfmt, fromz); if (!dt_unk_p(d)) { dt_io_write(d, ofmt, z, '\n'); } else if (!argi->quiet_given) { dt_io_warn_strpdt(inp); } } } else { /* read from stdin */ size_t lno = 0; struct grep_atom_s __nstk[16], *needle = __nstk; size_t nneedle = countof(__nstk); struct grep_atom_soa_s ndlsoa; void *pctx; struct prln_ctx_s prln = { .ndl = &ndlsoa, .ofmt = ofmt, .fromz = fromz, .outz = z, .sed_mode_p = argi->sed_mode_given, .quietp = argi->quiet_given, }; /* no threads reading this stream */ __io_setlocking_bycaller(stdout); /* lest we overflow the stack */ if (nfmt >= nneedle) { /* round to the nearest 8-multiple */ nneedle = (nfmt | 7) + 1; needle = calloc(nneedle, sizeof(*needle)); } /* and now build the needles */ ndlsoa = build_needle(needle, nneedle, fmt, nfmt); /* using the prchunk reader now */ if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) { error(errno, "Error: could not open stdin"); goto ndl_free; } while (prchunk_fill(pctx) >= 0) { for (char *line; prchunk_haslinep(pctx); lno++) { size_t llen = prchunk_getline(pctx, &line); proc_line(prln, line, llen); } } /* get rid of resources */ free_prchunk(pctx); ndl_free: if (needle != __nstk) { free(needle); } } if (argi->from_zone_given) { zif_close(fromz); } if (argi->zone_given) { zif_close(z); } out: cmdline_parser_free(argi); return res; }
void run_json(std::istream& is, std::ostream& os, const hfst::HfstTransducer& t, const msgmap& msgs) { json::sanity_test(); int pos = 0; std::u16string errtype = u"default"; bool first_err = true; LineType prevtype = BlankL; bool is_addcohort = true; std::ostringstream text; Cohort c; // TODO: could use http://utfcpp.sourceforge.net, but it's not in macports; // and ICU seems overkill just for iterating codepoints std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv; os << "{" << json::key(u"errs") << "["; for (std::string line; std::getline(is, line);) { std::match_results<const char*> result; std::regex_match(line.c_str(), result, CG_LINE); bool appendsugg = is_addcohort && prevtype != WordformL && !c.err.empty(); if (!result.empty() && ((result[2].length() != 0 && !appendsugg) || result[6].length() != 0)) { proc_cohort(pos, first_err, c, text, os, t, msgs, utf16conv); c = Cohort(); } if (!result.empty() && result[2].length() != 0) { if(appendsugg) { c.err = sugg_append(utf16conv.from_bytes(result[2]), c.err); } is_addcohort = true; c.form = utf16conv.from_bytes(result[2]); prevtype = WordformL; } else if(!result.empty() && result[3].length() != 0) { // TODO: doesn't do anything with subreadings yet; needs to keep track of previous line(s) for that const auto& sugg = proc_line(t, line); if(!sugg.errtype.empty()) { errtype = sugg.errtype; } if(sugg.suggest) { c.err[errtype].insert(sugg.sforms.begin(), sugg.sforms.end()); } else { is_addcohort = false; // Seen at least one non-suggestion reading } prevtype = ReadingL; } else if(!result.empty() && result[6].length() != 0) { const auto blank = clean_blank(result[6]); pos += utf16conv.from_bytes(blank).size(); text << blank; prevtype = BlankL; } else { // Blank lines without the prefix don't go into text output! prevtype = BlankL; } } proc_cohort(pos, first_err, c, text, os, t, msgs, utf16conv); os << "]" << "," << json::key(u"text") << json::str(utf16conv.from_bytes(text.str())) << "}"; }