LightgrepController::LightgrepController()
: ParsedPattern(lg_create_pattern()), Fsm(lg_create_fsm(1 << 20)), PatternInfo(lg_create_pattern_map(1000)), Prog(0), Scanners()
{
}
Example #2
0
void STest::init(const std::vector<Pattern>& pats) {
  std::unique_ptr<PatternHandle,void(*)(PatternHandle*)> pat(
    lg_create_pattern(),
    lg_destroy_pattern
  );

  PMap = std::unique_ptr<PatternMapHandle,void(*)(PatternMapHandle*)>(
    lg_create_pattern_map(pats.size()),
    lg_destroy_pattern_map
  );

  std::unique_ptr<FSMHandle,void(*)(FSMHandle*)> fsm(
    lg_create_fsm(0),
    lg_destroy_fsm
  );

  LG_KeyOptions keyOpts;

  size_t i = 0, numErrors = 0;
  for (const Pattern& p : pats) {
    LG_Error* err = nullptr;

    keyOpts.CaseInsensitive = p.CaseInsensitive;
    keyOpts.FixedString = p.FixedString;

    lg_parse_pattern(pat.get(), p.Expression.c_str(), &keyOpts, &err);

    if (!err) {
      lg_add_pattern(
        fsm.get(), PMap.get(), pat.get(), p.Encoding.c_str(), &err
      );

      if (!err) {
        // pack the user pattern number into the void*, oh the horror
        LG_PatternInfo* pinfo = lg_pattern_info(PMap.get(), i - numErrors);
        pinfo->UserData = reinterpret_cast<void*>(i);
      }
    }

    if (err) {
      lg_free_error(err);
      ++numErrors;
    }

    ++i;
  }

  LG_ProgramOptions progOpts{1};

  Prog = std::unique_ptr<ProgramHandle,void(*)(ProgramHandle*)>(
    lg_create_program(fsm.get(), &progOpts),
    lg_destroy_program
  );

  if (Prog) {
    LG_ContextOptions ctxOpts;

    Ctx = std::unique_ptr<ContextHandle,void(*)(ContextHandle*)>(
      lg_create_context(Prog.get(), &ctxOpts),
      lg_destroy_context
    );
  }
}