Beispiel #1
0
int String_split(String *s, int pos){
    if(s->num < pos) return 1;

    String *back;
    int size;

    back = String_create();
    if(back == NULL) return 1;

    size = s->num - pos;
    mpc_memcpy(back->string, &s->string[pos], size);
    back->num = size;
    back->line_num = back->num / WindowWidth + 1;

    mpc_memset(&s->string[pos], 0, size);
    s->num -= size;
    s->line_num = s->num / WindowWidth + 1;

    back->next = s->next;
    if(back->next->prev != NULL)
        back->next->prev = back;
        
    s->next = back;
    back->prev = s;

    return 0;
}
char *test_set_value() {
  spec_describe("Setting and getting value");

  HashMap *hash_map = HashMap_create(10);
  String *key = String_create("key");
  String *value = String_create("value");

  hash_map_set(hash_map, key, value);

  assert_equal(hash_map_get(hash_map, key), value, "value same");

  string_free(key);
  string_free(value);
  hash_map_free(hash_map);

  return NULL;
}
char *test_reset_value() {
  spec_describe("Setting and getting value");

  HashMap *hash_map = HashMap_create(10);
  String *key = String_create("key");
  String *value_1 = String_create("value");
  String *value_2 = String_create("another value");

  hash_map_set(hash_map, key, value_1);
  hash_map_set(hash_map, key, value_2);

  assert_equal(hash_map_get(hash_map, key), value_2, "value same");
  int index = hash_map_index_for_key(hash_map, key);
  assert_ints_equal(list_length((List *)hash_map_list_at_index(hash_map, index)), 1, "no duplicates for key in list");

  string_free(key);
  string_free(value_1);
  string_free(value_2);
  hash_map_free(hash_map);

  return NULL;
}
char *test_get_value_from_empty() {
  spec_describe("Getting values from empty hash_map");

  HashMap *hash_map = HashMap_create(10);
  String *key = String_create("key");

  assert_equal(hash_map_get(hash_map, key), NULL, "return NULL");

  string_free(key);
  hash_map_free(hash_map);

  return NULL;
}
Beispiel #5
0
int main()
{
	String * myKey, *myKey2, *myValue, *tmpString;
	List * keys; 
	ListIterator * iterator;
	HashTable * table = HashTable_create(500);

	myKey = String_create("Mia");
	myKey2 = String_create("Russell");
	myValue = String_create("shrug");

	HashTable_setValue(table, myKey, String_copy(myValue));
	HashTable_setValue(table, myKey2, String_copy(myValue));

	String_destroy(myValue);

	keys = HashTable_getKeys(table);

	iterator = ListIterator_create(keys);
	tmpString = ListIterator_seekToFirst(iterator);

	while(tmpString != NULL)
	{
		myValue = HashTable_removeValue(table, tmpString);
		printf("Removed -> %s\n", String_c(myValue));
		String_destroy(myValue);
		tmpString = ListIterator_seekToFirst(iterator);
	}

	ListIterator_destroy(iterator);

	String_destroy(myKey);
	String_destroy(myKey2);

	HashTable_destroy(table);
	return 0;
}
Beispiel #6
0
int File_open(char *name){
    char buf[128];
    int fd;
    int ret;
    String *str;
    int start, end;
    head = String_create();
    str = head;

    mpc_strcpy(filename, name);

    fd = sys_open(name, O_RDONLY);
    if(fd < 0) return 1;

    for(;;){
        ret = sys_read(fd, buf, 127);
        if(ret < 0) return 1;
        if(ret == 0) break;

        buf[ret] = 0;

        start = 0;
        for(;;){
            for(end=start;end<ret;end++)
                if(buf[end] == '\n') break;
            buf[end] = 0;
        

            String_insert_str(str, str->num, &buf[start], mpc_strlen(&buf[start]));
            if( end == ret ) break;
            String_split(str, str->num);
            str = str->next;
            start = end + 1;
        }
    }
    sys_close(fd);

    return 0;
}
Beispiel #7
0
/**
 * Print out the constituent tree.
 * mode 1: treebank-style constituent tree
 * mode 2: flat, bracketed tree [A like [B this B] A]
 * mode 3: flat, treebank-style tree (A like (B this) )
 */
char * linkage_print_constituent_tree(Linkage linkage, int mode)
{
	String * cs;
	CNode * root;
	char * p;

	if ((mode == 0) || (linkage->sent->dict->constituent_pp == NULL))
	{
		return NULL;
	}
	else if (mode == 1 || mode == 3)
	{
		cs = String_create();
		root = linkage_constituent_tree(linkage);
		print_tree(cs, (mode==1), root, 0, 0);
		linkage_free_constituent_tree(root);
		append_string(cs, "\n");
		p = exalloc(strlen(cs->p)+1);
		strcpy(p, cs->p);
		exfree(cs->p, sizeof(char)*cs->allocated);
		exfree(cs, sizeof(String));
		return p;
	}
	else if (mode == 2)
	{
		char * str;
		con_context_t *ctxt;

		ctxt = (con_context_t *) malloc(sizeof(con_context_t));
		str = print_flat_constituents(ctxt, linkage);
		free(ctxt);

		return str;
	}
	assert(0, "Illegal mode in linkage_print_constituent_tree");
	return NULL;
}
Beispiel #8
0
String StringClass_obtain(TypeClass self, Oid typeId)
{
	return String_create(self, typeId);
}
Beispiel #9
0
static char * exprint_constituent_structure(con_context_t *ctxt, Linkage linkage, int numcon_total)
{
	int c, w;
	int leftdone[MAXCONSTITUENTS];
	int rightdone[MAXCONSTITUENTS];
	int best, bestright, bestleft;
	Sentence sent;
	char s[100], * p;
	String * cs = String_create();

	assert (numcon_total < MAXCONSTITUENTS, "Too many constituents");
	sent = linkage_get_sentence(linkage);

	for(c=0; c<numcon_total; c++) {
		leftdone[c]=0;
		rightdone[c]=0;
	}

	if(verbosity>=2)
		printf("\n");			

	for(w=1; w<linkage->num_words; w++) {	
		/* Skip left wall; don't skip right wall, since it may
		   have constituent boundaries */

		while(1) {
			best = -1;
			bestright = -1;
			for(c=0; c<numcon_total; c++) {
				if ((ctxt->constituent[c].left==w) &&
					(leftdone[c]==0) && (ctxt->constituent[c].valid==1) &&
					(ctxt->constituent[c].right >= bestright)) {
					best = c;
					bestright = ctxt->constituent[c].right;
				}
			}
			if (best==-1)
				break;
			leftdone[best]=1;
			if(ctxt->constituent[best].aux==1) continue;
			append_string(cs, "%c%s ", OPEN_BRACKET, ctxt->constituent[best].type);
		}

		if (w<linkage->num_words-1) {
			/* Don't print out right wall */
			strcpy(s, sent->word[w].string);

			/* Now, if the first character of the word was
			   originally uppercase, we put it back that way */
			if (sent->word[w].firstupper ==1 )
				upcase_utf8_str(s, s, MAX_WORD);
			append_string(cs, "%s ", s);
		}

		while(1) {
			best = -1;
			bestleft = -1;
			for(c=0; c<numcon_total; c++) {
				if ((ctxt->constituent[c].right==w) &&
					(rightdone[c]==0) && (ctxt->constituent[c].valid==1) &&
					(ctxt->constituent[c].left > bestleft)) {
					best = c;
					bestleft = ctxt->constituent[c].left;
				}
			}
			if (best==-1)
				break;
			rightdone[best]=1;
			if (ctxt->constituent[best].aux==1)
				continue;
			append_string(cs, "%s%c ", ctxt->constituent[best].type, CLOSE_BRACKET);
		}
	}

	append_string(cs, "\n");
	p = exalloc(strlen(cs->p)+1);
	strcpy(p, cs->p);
	exfree(cs->p, sizeof(char)*cs->allocated);
	exfree(cs, sizeof(String));
	return p;
}
Beispiel #10
0
Token *lex_get_next_lexeme(ParseState *state) {
  String *word = String_create("");

  int starting_index = 0; // used to track progress against comments
  int column = 0;
  int line =   0;
  char c =     lex_state_current_char(state);
  Boolean should_continue = true;

  while ( lex_state_in_progress(state) && should_continue ) {
    // strings, comments, regex, etc ...
    if ( string_empty(word) && lex_state_opens_at_current(state) ) {
      starting_index = lex_state_current(state);
      lex_state_lexical_bookend(state) = lex_state_closer(state);
    }

    if ( lex_state_current_is_significant(state) ) {
      starting_index = starting_index ? starting_index : (lex_state_current(state));
      column = column ? column : (lex_state_column(state));
      line =   line   ? line   : (lex_state_line(state));
      string_push(word, c);
    }

    // update lex state for new line
    if ( char_is_line_end(c) ) {
      lex_state_start_new_line(state);
    }

    // check for termination of strings and other bookends that may contain spaces
    if ( lex_state_is_open(state) && starting_index < lex_state_current(state) ) {
      // regexes are special, because there can be characters after the ending '/'
      // so we have to switch the state
      lex_state_transition_regex_if_needed(state);

      if ( lex_state_will_close(state) ) {
        lex_state_close(state);
        should_continue = false;
      }
    } else if ( lex_state_current_is_significant(state) && (
        char_is_line_end(c) || char_is_statement_end(c) ||         // line ends usually significant of a statement end
        lex_state_end_of_word(state) ||                        // end of normal word sequence
        word_is_method_selector(word, c)  ||                       // '.'
        char_is_syntax(c) ||                                       // '(' ')' ','
        char_is_colon(lex_state_next_char(state)) ||           // : appearing after first char breaks the word
        lex_state_will_end_word_by_dot(state, word))       ) { // next char is a dot, and word is not a number
      should_continue = false;
    }

    // move to next character
    lex_state_advance(state);
    c = lex_state_current_char(state);
  }

  if ( string_empty(word) ) {
    string_free(word);
    return NULL;
  }

  Token *lexeme = Token_create(word, line, column);
  check_mem(lexeme);

  return lexeme;
error:
  return NULL;
}
Beispiel #11
0
Token *token_from_lexeme(Token *lexeme) {
  char *word = string_value(lexeme_value(lexeme));
  void *value = NULL;
  char first_char = word[0];

  if ( char_is_line_end(first_char) ) {
    token_type(lexeme) = FX_TOKEN_LINE_END;
    string_free(token_value(lexeme));
  } else if ( char_is_statement_end(first_char) ) {
    token_type(lexeme) = FX_TOKEN_STATEMENT_END;
    string_free(token_value(lexeme));
  } else if ( char_is_method_selector(first_char) ) {
    token_type(lexeme) = FX_TOKEN_ATTRIBUTE_SELECTOR;
    string_free(token_value(lexeme));
  } else if ( char_opens_group(first_char) ) {
    token_type(lexeme) = FX_TOKEN_GROUP_START;
    string_free(token_value(lexeme));
  } else if ( char_closes_group(first_char) ) {
    token_type(lexeme) = FX_TOKEN_GROUP_END;
    string_free(token_value(lexeme));
  } else if ( char_is_separator(first_char) ) {
    token_type(lexeme) = FX_TOKEN_COMMA;
    string_free(token_value(lexeme));
  } else if ( char_opens_block(first_char) ) {
    token_type(lexeme) = FX_TOKEN_BLOCK_START;
    string_free(token_value(lexeme));
  } else if ( char_closes_block(first_char) ) {
    token_type(lexeme) = FX_TOKEN_BLOCK_END;
    string_free(token_value(lexeme));
  } else if ( char_is_setter(first_char) && lexeme_length(lexeme) == 1 ) {
    token_type(lexeme) = FX_TOKEN_LOCAL_SETTER;
    string_free(token_value(lexeme));
  } else if ( char_is_colon(first_char) && lexeme_length(lexeme) == 1 ) {
    token_type(lexeme) = FX_TOKEN_ATTRIBUTE_SETTER;
    string_free(token_value(lexeme));
  } else if ( char_is_deferred_arg(first_char) && lexeme_length(lexeme) == 1 ) {
    token_type(lexeme) = FX_TOKEN_DEFERRED_ARGUMENT;
    string_free(token_value(lexeme));
  } else if ( word_is_block_declaration(word) ) {
    token_type(lexeme) = FX_TOKEN_BLOCK_DECLARATION;
    string_free(token_value(lexeme));
  } else if ( char_is_regex_bookend(first_char) ) {
    token_type(lexeme) = FX_TOKEN_REGEX;
  } else if ( char_is_string_bookend(first_char) ) {
    token_type(lexeme) = FX_TOKEN_STRING;
    word[lexeme_length(lexeme) - 1] = '\0'; // shortening string contents to remove the quotation marks
    value = String_create((word+1));
    check(value, "token string value is NULL");
    string_free(token_value(lexeme));
    token_value(lexeme) = value;
  } else if ( lexed_word_is_number(word) ) {
    token_type(lexeme) = FX_TOKEN_NUMBER;
    value = Number_create(word);
    check(value, "token number is NULL");
    string_free(token_value(lexeme));
    token_value(lexeme) = value;
  } else if ( char_is_capitalized(first_char) ) {
    token_type(lexeme) = FX_TOKEN_GLOBAL_ID;
  } else if ( char_is_colon(first_char) ) {
    token_type(lexeme) = FX_TOKEN_ATOM;
    value = String_create((word+1));
    check(value, "token string value is NULL");
    string_free(token_value(lexeme));
    token_value(lexeme) = value;
  } else {
    token_type(lexeme) = FX_TOKEN_ID;
  }

  return lexeme;
error:
  return NULL;
}