Example #1
0
void do_test() {
Start:
   if (isatty(fileno(stdin)))
      printf("Enter a regular expression:\n\n");
   if (fgets(buf, BUFFER, stdin) == NULL)
      return;
   trim(buf);
   if ((shre = shre_compile(buf)) == NULL) {
      printf("error: %s\n", shre_strerror(shre_er));
      goto Start;
   }
   if (isatty(fileno(stdin)))
      printf("\nEnter text to test the regular expression:\n\n");
   while (fgets(buf, BUFFER, stdin) != NULL) {
      trim(buf);
      if (strcmp(buf, "NEW") == 0) {
         goto Start;
      }
      printf("Pattern:  '%s'\n", shre_expression(shre));
      printf("String:   '%s'\n", buf);
      printf("Match:    ");
      if ((match = shre_search(shre, buf)) == NULL) {
         printf(" None\n\n");
      } else {
         printf("'%s'\n", match_get(match));
         for (int i = 1; i < match_num_groups(match); ++i) {
            printf("Group %2d: ", i);
            char* str = match_group(match, i);
            if (!str) {
               printf(" NULL\n");
            } else {
               printf("'%s'\n", str);
               free(str);
            }
         }
         printf("\n");
         match_free(match);
      }
   }
}
Example #2
0
/**
 ** rockse <path_to_tbl> <path_to_log>
 **/
int main(int argc, char **argv) {
  rockse * ptr;

  ETERM *from_erl;
  ETERM *cmd;
  ETERM *key;
  ETERM *value;

  char *ret;
  ETERM *to_send;

  byte buf[1024];
  int length;

  log = fopen(argv[2], "a+");

  erl_init(NULL, 0);
  fprintf(log, "*** rockse opening table %s\n", argv[1]);
  ptr = open(argv[1]);

  while (read_cmd(buf) > 0) {

    from_erl = erl_decode(buf);
    fprintf(log, "  rockse command type %d\n", ERL_TYPE(from_erl));
 
    if (match_close(from_erl)) {
      send_bok();
      fprintf(log, "   rockse command +close+ sent\n");
      break; 
    }

    if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 3) {
      /* can only be a {put, key, val} */
      cmd = ERL_TUPLE_ELEMENT(from_erl, 0);

      if (match_put(cmd)) {
	key = ERL_TUPLE_ELEMENT(from_erl, 1);
	value = ERL_TUPLE_ELEMENT(from_erl, 2);

	fprintf(log, "   rockse command +put+ %s %s sent\n", erl_iolist_to_string(key), erl_iolist_to_string(value));

	put(ptr, erl_iolist_to_string(key), erl_iolist_to_string(value));
	send_put_ok();
      }
      erl_free_term(from_erl);
      erl_free_term(cmd);
      erl_free_term(key);
      erl_free_term(value);
    }
    if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 2) {
      /* can only be a {get, key} or {delete, key} */
      cmd = ERL_TUPLE_ELEMENT(from_erl, 0);
      if (match_get(cmd)) {
	key = ERL_TUPLE_ELEMENT(from_erl, 1);

	fprintf(log, "   rockse command +get+ %s sent\n", erl_iolist_to_string(key));

	ret = get(ptr, erl_iolist_to_string(key));
	if (ret == NULL) {
	  send_nok();
	}
	else {
	  length = strlen(ret);

	  fprintf(log, "   rockse command +get+ found %s of size %d\n", ret, length);

	  to_send = erl_format("{ok, get, ~s}", ret);
	  send_get_result(to_send);
	  free(ret);
	}
      }
      else if (match_delete(cmd)) {
	key = ERL_TUPLE_ELEMENT(from_erl, 1);

	fprintf(log, "   rockse command +delete+ %s sent\n", erl_iolist_to_string(key));

	delete(ptr, erl_iolist_to_string(key));
	send_delete_ok();
      }

      erl_free_term(from_erl);
      erl_free_term(cmd);
      erl_free_term(key);
    }


    fflush(log);
  }

  close_and_destroy(ptr);
  fprintf(log, "*** rockse closing table %s\n", argv[1]);

  erl_free_term(from_erl);

  fclose(log);

  return 0;
}