示例#1
0
END_TEST

START_TEST (initialize_using_lang_code)
{
  int rc;
  char *errMsg = NULL;
  varnam *handle;
  strbuf *tmp;

  rc = varnam_init_from_id ("ml", &handle, &errMsg);
  if (errMsg != NULL) {
    printf ("init_from_lang failed: %s\n", errMsg);
  }
  assert_success (rc);

  tmp = strbuf_init (20);
  strbuf_addf (tmp, "%s/ml.vst", strbuf_to_s (varnam_get_symbols_dir()));
  ck_assert_str_eq (strbuf_to_s(tmp), varnam_get_scheme_file (handle));
  strbuf_destroy (tmp);

  tmp = strbuf_init (10);
  strbuf_addf (tmp, "%s/.local/share/varnam/suggestions/ml.vst.learnings", getenv ("HOME"));
  ck_assert_str_eq (strbuf_to_s (tmp), varnam_get_suggestions_file (handle));

  strbuf_destroy (tmp);
  varnam_destroy (handle);
}
示例#2
0
文件: redefine.c 项目: Laurae2/Cubist
/*
 * This is called at the beginning of a cubist run to clear out
 * all "files" generated on the previous run and to prepare it
 * for the next run.
 */
void rbm_removeall()
{
    /* Check if there actually is anything to remove */
    if (strbufv != NULL) {
        /*
         * Destroy all STRBUF's in the hash table.
         * Note that this loop leaves the hash table full of
         * pointers to deallocated STRBUF's until ht_destroy
         * is called below.
         */
        ht_reset(strbufv);  /* just in case */
        while (1) {
            void *e = ht_next(strbufv);
            if (e == NULL)
                break;
            strbuf_destroy((STRBUF *) ht_value(e));
        }

        /* Destroy the hash table itself */
        ht_destroy(strbufv);
    }

    /* Create/recreate the hash table for subsequent use */
    strbufv = ht_new(HASH_LEN);
}
示例#3
0
文件: redefine.c 项目: Laurae2/Cubist
FILE *rbm_fopen(const char *filename, const char *mode)
{
    STRBUF *sb;
    STRBUF *id = ht_getvoid(strbufv, filename, NULL, NULL);

    /* Only the "w" mode is currently supported */
    if (strcmp(mode, "w") == 0) {
        // Rprintf("rbm_fopen: opening file to write: %s\n", filename);
        sb = strbuf_create_empty(STRBUF_LEN);
        if (id != NULL) {
            Rprintf("rbm_fopen: warning: destroying previous STRBUF: %s\n", filename);
            strbuf_destroy(id);
        }
        ht_setvoid(strbufv, filename, sb);
    } else {
        // Rprintf("rbm_fopen: opening file to read: %s\n", filename);
        sb = id;
        if (sb != NULL) {
            if (sb->open) {
                Rprintf("rbm_fopen: error: file already open: %s\n", filename);
                sb = NULL;  // XXX Is this right?
            } else {
                strbuf_open(sb);
                strbuf_rewind(sb);
            }
        } else {
            // Rprintf("rbm_fopen: no such file: %s\n", filename);
            sb = NULL;
        }
    }

    return (FILE *) sb;
}
示例#4
0
void error_destroy(errorstream_t *error) {
  if (error == NULL) {
    return;
  }

  strbuf_destroy(&error->message);
}
示例#5
0
文件: serialize.c 项目: ensc/ensc-lib
static void test2(struct unserialize_stream_mem *u, bool optimize)
{
	struct strbuf	buf = INIT_STRBUF(buf);
	char const	*str;
	size_t		str_len;

#define Xunserialize_bufa(_u, _dst, _exp)			\
	assert(unserialize_bufa(_u, _dst, optimize));		\
	assert((_dst)->len == sizeof _exp - 1);			\
	assert(memcmp((_dst)->b, _exp, (_dst)->len) == 0);	\

#define Xunserialize_stra(_u, _buf, _len, _exp)		\
	assert(unserialize_stra(_u, &(_buf), _len));	\
	assert(strcmp(_buf, _exp) == 0);		\
	assert(*(_len) == sizeof _exp - 1);		\
	freec(_buf);					\

	Xunserialize_bufa(&u->s, &buf, BUF0);
	Xunserialize_bufa(&u->s, &buf, BUF1);
	Xunserialize_bufa(&u->s, &buf, BUF2);
	Xunserialize_bufa(&u->s, &buf, BUF3);

	Xunserialize_stra(&u->s, str, &str_len, STR0);
	Xunserialize_stra(&u->s, str, &str_len, STR1);
	Xunserialize_stra(&u->s, str, &str_len, STR2);
	Xunserialize_stra(&u->s, str, &str_len, STR3);

#undef Xunserialize_str
#undef Xunserialize_buf

	assert(u->cnt == 0);

	strbuf_destroy(&buf);
}
示例#6
0
文件: generator.c 项目: asmblur/SynCE
void generator_destroy(Generator* self)/*{{{*/
{
  if (self)
  {
    s_hash_table_destroy(self->properties, free);
    strbuf_destroy(self->buffer, true);
    if (self->propvals)
      free(self->propvals);
    free(self);
  }
}/*}}}*/
示例#7
0
文件: redefine.c 项目: Laurae2/Cubist
int rbm_remove(const char *path)
{
    STRBUF *sb = rbm_lookup(path);

    if (sb == NULL) {
        return -1;
    }

    rbm_deregister(path);
    strbuf_destroy(sb);

    return 0;
}
示例#8
0
END_TEST

START_TEST (words_with_repeating_characters_will_not_be_learned)
{
    int rc;
    strbuf *string;
    const char *word_to_learn = "കകകകകകക";

    rc = varnam_learn (varnam_instance, word_to_learn);
    assert_error (rc);
    string = strbuf_init (50);
    strbuf_addf (string, "'%s' looks incorrect. Not learning anything", word_to_learn);
    ck_assert_str_eq (varnam_get_last_error (varnam_instance), strbuf_to_s (string));
    strbuf_destroy (string);
}
示例#9
0
文件: varnam.c 项目: sdqali/libvarnam
int
varnam_destroy(varnam *handle)
{
    struct varnam_internal *vi;
    int rc;

    if (handle == NULL)
        return VARNAM_ARGS_ERROR;

    vi = handle->internal;

    xfree(vi->message);
    strbuf_destroy (vi->output);
    strbuf_destroy (vi->rtl_output);
    strbuf_destroy (vi->lookup);
    strbuf_destroy (vi->last_error);

    strbuf_destroy (vi->scheme_language_code);
    strbuf_destroy (vi->scheme_identifier);
    strbuf_destroy (vi->scheme_display_name);
    strbuf_destroy (vi->scheme_author);
    strbuf_destroy (vi->scheme_compiled_date);

    xfree(vi->last_token);
    xfree(vi->current_token);
    xfree(vi->last_rtl_token);
    xfree(vi->current_rtl_token);
    rc = sqlite3_close(handle->internal->db);
    if (rc != SQLITE_OK) {
        return VARNAM_ERROR;
    }
    xfree(handle->internal);
    xfree(handle->scheme_file);
    xfree(handle);
    return VARNAM_SUCCESS;
}
示例#10
0
END_TEST

START_TEST (numbers_will_be_ignored_while_learning)
{
    int rc;
    strbuf *string;

    rc = varnam_learn (varnam_instance, "01");
    assert_error (rc);
    string = strbuf_init (50);
    strbuf_add (string, "Can't process '0'. One or more characters in '01' are not known");
    ck_assert_str_eq (varnam_get_last_error (varnam_instance), strbuf_to_s (string));

    rc = varnam_learn (varnam_instance, "१०१");
    assert_error (rc);
    strbuf_clear (string);
    strbuf_add (string, "Nothing to learn from '१०१'");
    ck_assert_str_eq (varnam_get_last_error (varnam_instance), strbuf_to_s (string));
    strbuf_destroy (string);
}
示例#11
0
END_TEST

START_TEST (varnam_export_full)
{
    int rc, pcnt, wcnt, i;
    float filecnt;
    strbuf* f; strbuf* error;

    f = strbuf_init (20);
    pcnt = execute_query_int (varnam_instance->internal->known_words, "select count(*) from patterns_content;");
    wcnt = execute_query_int (varnam_instance->internal->known_words, "select count(*) from words;");

    rc = varnam_export_words (varnam_instance, 2, "output/", VARNAM_EXPORT_FULL, NULL);
    assert_success (rc);

    filecnt = pcnt / 2;
    for (i = 0; i < (int) ceil (filecnt); i++) {
        strbuf_clear (f);
        strbuf_addf (f, "output/%d.patterns.txt", i);
        if (!file_exist (strbuf_to_s (f))) {
            error = strbuf_init (10);
            strbuf_addf (error, "Failed to find file: %s\n", strbuf_to_s (f));
            ck_abort_msg (strbuf_to_s (error));
        }
    }

    filecnt = wcnt / 2;
    for (i = 0; i < (int) ceil (filecnt); i++) {
        strbuf_clear (f);
        strbuf_addf (f, "output/%d.words.txt", i);
        if (!file_exist (strbuf_to_s (f))) {
            error = strbuf_init (10);
            strbuf_addf (error, "Failed to find file: %s\n", strbuf_to_s (f));
            ck_abort_msg (strbuf_to_s (error));
        }
    }

    strbuf_destroy (f);
}
示例#12
0
TEST test_collect(void) {
    FILE *f = fopen("test", "w+");

    static const char S[] = "a\0b\0c";
    rewind(f);
    fwrite(S, sizeof(S[0]), ASIZE(S), f);

    strbuf_t *sb;
    strbuf_init(&sb, 16);

    rewind(f);
    collect(&sb, f);

    ASSERT(sb->pos == 6);
    ASSERT_STR_EQ(&sb->buf[0], "a");
    ASSERT_STR_EQ(&sb->buf[2], "b");
    ASSERT_STR_EQ(&sb->buf[4], "c");

    strbuf_destroy(sb);
    fclose(f);

    PASS();
}
示例#13
0
static bool recurrence_initialize_rrule_vcal(const char* str, RRule* rrule)
{
  int i;

  /** TODO - check for other whitespace, and multiple whitespace */
  char** strv = strsplit(str, ' ');

  rrule->interval = 1;

  if (strv[0][0] == 'D')
  {
    rrule->freq = RRuleDaily;
    rrule->interval = atoi(strv[0]+1);
    if (strv[1])
    {
      if (strv[1][0] == '#')
	rrule->count = atoi(strv[1]+1);
      else
	replace_string_with_copy(&rrule->until, strv[1]);
    }
  }
  else if (strv[0][0] == 'W')
  {
    rrule->freq = RRuleWeekly;
    rrule->interval = atoi(strv[0]+1);

    StrBuf *days_list = strbuf_new(NULL);
    for (i = 1; strv[i]; i++)
    {
      if (strv[i][0] == '#')
      {
	rrule->count = atoi(strv[i]+1);
	break;
      }
      else if (isdigit(strv[i][0]))
      {
	replace_string_with_copy(&rrule->until, strv[i]);
	break;
      }
      else
      {
	if (days_list->length > 0)
	  days_list = strbuf_append(days_list,",");
	days_list = strbuf_append(days_list,strv[i]);
      }
    }
    if (days_list->length > 0)
      replace_string_with_copy(&rrule->byday, days_list->buffer);
    strbuf_destroy(days_list,1);
  }
  else if (strv[0][0] == 'M')
  {
    rrule->freq = RRuleMonthly;
    if (strv[0][1] == 'D') /* by day in month, ie date */
    {
      rrule->interval = atoi(strv[0]+2);

      int days_found = 0;
      for (i = 1; strv[i]; i++)
      {
	if (strv[i][0] == '#')
	{
	  rrule->count = atoi(strv[i]+1);
	  break;
	}
	else if (isdigit(strv[i][0]) || STR_EQUAL(strv[i],"LD"))
	{
	  if (strlen(strv[i]) > 2)
	  {
	    replace_string_with_copy(&rrule->until, strv[i]);
	    break;
	  }

	  if (days_found == 0)
	  {
	    if (STR_EQUAL(strv[i],"LD"))
	      rrule->bymonthday = -1;
	    else
	      rrule->bymonthday = atoi(strv[i]);
	  }
	  days_found++;
	}
      }

      if (days_found > 1)
	synce_warning("Monthly by Day (MD) can only handle one day in RRA format, in RRULE '%s'", str);
    }
    else if (strv[0][1] == 'P') /* by position, ie the complicated one */
    {
      rrule->interval = atoi(strv[0]+2);

      StrBuf *days_list = strbuf_new(NULL);
      int occurence = 0;
      for (i = 1; strv[i]; i++)
      {
	if (strv[i][0] == '#')
	{
	  rrule->count = atoi(strv[i]+1);
	  break;
	}
	else if (isdigit(strv[i][0]) && strlen(strv[i]) > 2)
	{
	  replace_string_with_copy(&rrule->until, strv[i]);
	  break;
	}
	else if (isdigit(strv[i][0]))
	{
	  if (occurence != 0)
	    /* Any days following this shoukd be bound by this subsequent
	       occurence modifier, but cant be. We'll allow them to go with
	       the first occurrence modifier for now, but maybe we should
	       drop them entirely */
	    synce_info("Multiple occurence values cannot be represented by RRA in Monthly by Position recurrence");
	  else
	    occurence = atoi(strv[i]);
	}
	else
	{
	  if (days_list->length > 0)
	    days_list = strbuf_append(days_list,",");
	  days_list = strbuf_append(days_list,strv[i]);
	}
      }
      rrule->bysetpos = occurence;

      if (days_list->length > 0)
	replace_string_with_copy(&rrule->byday, days_list->buffer);
      strbuf_destroy(days_list,1);
    }
    else
      synce_error("Unexpected frequency in RRULE '%s'", str);
  }
  else if (strv[0][0] == 'Y')
  {
    rrule->freq = RRuleYearly;
    rrule->interval = atoi(strv[0]+2);

    if (strv[0][1] == 'M') /* by month number */
    {
      int months_found = 0;
      for (i = 1; strv[i]; i++)
      {
	if (strv[i][0] == '#')
	{
	  rrule->count = atoi(strv[i]+1);
	  break;
	}
	else if (isdigit(strv[i][0]))
	{
	  if (strlen(strv[i]) > 2)
	  {
	    replace_string_with_copy(&rrule->until, strv[i]);
	    break;
	  }

	  if (months_found == 0)
	  {
	    rrule->bymonth = atoi(strv[i]);
	  }
	  months_found++;
	}
      }

    }
    else if (strv[0][1] == 'D') /* by day in year */
    {
      int days_found = 0;
      for (i = 1; strv[i]; i++)
      {
	if (strv[i][0] == '#')
	{
	  rrule->count = atoi(strv[i]+1);
	  break;
	}
	else if (isdigit(strv[i][0]))
	{
	  if (strlen(strv[i]) > 2)
	  {
	    replace_string_with_copy(&rrule->until, strv[i]);
	    break;
	  }

	  if (days_found == 0)
	  {
	    rrule->bymonthday = atoi(strv[i]);
	  }
	  days_found++;
	}
      }

      if (days_found > 1)
	synce_warning("Yearly by Day (YD) (converted to monthly) can only handle one day in RRA format, in RRULE '%s'", str);

    }
    else
      synce_error("Unexpected frequency in RRULE '%s'", str);
  }
  else
  {
    synce_error("Unexpected frequency in RRULE '%s'", str);
  }
  
  strv_free(strv);
  return true;
}