void parse_file(FILE *f) {
	int length, i, is_new_prod = 0;
	char c;
	char delims[] = " ";
	char to_be_removed[] = "; \t\n";
	char * tokens, * non_term, * clean_token = NULL;
	char s[MAX_STRING_SIZE], s2[MAX_STRING_SIZE];

    do {
    	c = fgetc(f);
        if(c == '{')
        {
            fseek(f, 1, SEEK_CUR);
     
			fgets(s, MAX_STRING_SIZE, f);

			length = strlen(s) - 1;
			strncpy(s2, s, length);
			
			for(i=length; i < MAX_STRING_SIZE; i++)
				s2[i] = '\0';
	
			non_term = strdup(s2);
			non_term = remove_chars(non_term, to_be_removed);			

			/* Extract the root non terminal symbol */
			grammar_nodeT new_node = create_g_node(non_term);
	
			is_new_prod = 0;

	        do {
				fgets(s, 5 * MAX_STRING_SIZE, f);
				tokens = (char *) strtok(s, delims);

				if(strchr(tokens, '}'))
					continue;

				while (tokens != NULL) { 
					clean_token = remove_chars(tokens, to_be_removed);

					if(strlen(clean_token)==0)
						break;

					/* Get terminal and non terminal symbols for the specific production */
					if(is_new_prod)
						add_element_to_g_node(&new_node, clean_token, NEW_PRODUCTION);
					else						
						add_element_to_g_node(&new_node, clean_token, KEEP_PRODUCTION);
    				tokens = (char *) strtok( NULL, delims );
					is_new_prod = 0;
				}
				is_new_prod = 1;
			} while (strcmp(s, "}") <= 0);

			/* Insert new node to grammar */
			insert_to_grammar(new_node);
 		}
    } while (c != EOF);
}
Esempio n. 2
0
string ItemContent::get_token_value(const string &str) {
    string local_str(str);
    local_str = remove_chars(local_str, "\"");
    vector<string> key_value = split(local_str, ":");
    local_str = key_value[1];
    return local_str;
}
Esempio n. 3
0
int main(void) {
	printf("Enter input string, followed by the string of characters to remove\n");

	while (scanf("%s %s", input_str, rem_str) == 2) {
		printf("Before removal: %s\n", input_str);
		remove_chars(input_str, rem_str);
		printf("After removal: %s\n", input_str);
	}

	return 0;
}
Esempio n. 4
0
static int linenoiseEdit(struct current *current) {
    int history_index = 0;

    /* The latest history entry is always our current buffer, that
     * initially is just an empty string. */
    linenoiseHistoryAdd("");

    set_current(current, "");
    refreshLine(current->prompt, current);

    while(1) {
        int dir = -1;
        int c = fd_read(current);

#ifndef NO_COMPLETION
        /* Only autocomplete when the callback is set. It returns < 0 when
         * there was an error reading from fd. Otherwise it will return the
         * character that should be handled next. */
        if (c == '\t' && current->pos == current->chars && completionCallback != NULL) {
            c = completeLine(current);
            /* Return on errors */
            if (c < 0) return current->len;
            /* Read next character when 0 */
            if (c == 0) continue;
        }
#endif

process_char:
        if (c == -1) return current->len;
#ifdef USE_TERMIOS
        if (c == 27) {   /* escape sequence */
            c = check_special(current->fd);
        }
#endif
        switch(c) {
        case '\r':    /* enter */
            history_len--;
            free(history[history_len]);
            return current->len;
        case ctrl('C'):     /* ctrl-c */
            errno = EAGAIN;
            return -1;
        case 127:   /* backspace */
        case ctrl('H'):
            if (remove_char(current, current->pos - 1) == 1) {
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('D'):     /* ctrl-d */
            if (current->len == 0) {
                /* Empty line, so EOF */
                history_len--;
                free(history[history_len]);
                return -1;
            }
            /* Otherwise fall through to delete char to right of cursor */
        case SPECIAL_DELETE:
            if (remove_char(current, current->pos) == 1) {
                refreshLine(current->prompt, current);
            }
            break;
        case SPECIAL_INSERT:
            /* Ignore. Expansion Hook.
             * Future possibility: Toggle Insert/Overwrite Modes
             */
            break;
        case ctrl('W'):    /* ctrl-w, delete word at left. save deleted chars */
            /* eat any spaces on the left */
            {
                int pos = current->pos;
                while (pos > 0 && get_char(current, pos - 1) == ' ') {
                    pos--;
                }

                /* now eat any non-spaces on the left */
                while (pos > 0 && get_char(current, pos - 1) != ' ') {
                    pos--;
                }

                if (remove_chars(current, pos, current->pos - pos)) {
                    refreshLine(current->prompt, current);
                }
            }
            break;
        case ctrl('R'):    /* ctrl-r */
            {
                /* Display the reverse-i-search prompt and process chars */
                char rbuf[50];
                char rprompt[80];
                int rchars = 0;
                int rlen = 0;
                int searchpos = history_len - 1;

                rbuf[0] = 0;
                while (1) {
                    int n = 0;
                    const char *p = NULL;
                    int skipsame = 0;
                    int searchdir = -1;

                    snprintf(rprompt, sizeof(rprompt), "(reverse-i-search)'%s': ", rbuf);
                    refreshLine(rprompt, current);
                    c = fd_read(current);
                    if (c == ctrl('H') || c == 127) {
                        if (rchars) {
                            int p = utf8_index(rbuf, --rchars);
                            rbuf[p] = 0;
                            rlen = strlen(rbuf);
                        }
                        continue;
                    }
#ifdef USE_TERMIOS
                    if (c == 27) {
                        c = check_special(current->fd);
                    }
#endif
                    if (c == ctrl('P') || c == SPECIAL_UP) {
                        /* Search for the previous (earlier) match */
                        if (searchpos > 0) {
                            searchpos--;
                        }
                        skipsame = 1;
                    }
                    else if (c == ctrl('N') || c == SPECIAL_DOWN) {
                        /* Search for the next (later) match */
                        if (searchpos < history_len) {
                            searchpos++;
                        }
                        searchdir = 1;
                        skipsame = 1;
                    }
                    else if (c >= ' ') {
                        if (rlen >= (int)sizeof(rbuf) + 3) {
                            continue;
                        }

                        n = utf8_getchars(rbuf + rlen, c);
                        rlen += n;
                        rchars++;
                        rbuf[rlen] = 0;

                        /* Adding a new char resets the search location */
                        searchpos = history_len - 1;
                    }
                    else {
                        /* Exit from incremental search mode */
                        break;
                    }

                    /* Now search through the history for a match */
                    for (; searchpos >= 0 && searchpos < history_len; searchpos += searchdir) {
                        p = strstr(history[searchpos], rbuf);
                        if (p) {
                            /* Found a match */
                            if (skipsame && strcmp(history[searchpos], current->buf) == 0) {
                                /* But it is identical, so skip it */
                                continue;
                            }
                            /* Copy the matching line and set the cursor position */
                            set_current(current,history[searchpos]);
                            current->pos = utf8_strlen(history[searchpos], p - history[searchpos]);
                            break;
                        }
                    }
                    if (!p && n) {
                        /* No match, so don't add it */
                        rchars--;
                        rlen -= n;
                        rbuf[rlen] = 0;
                    }
                }
                if (c == ctrl('G') || c == ctrl('C')) {
                    /* ctrl-g terminates the search with no effect */
                    set_current(current, "");
                    c = 0;
                }
                else if (c == ctrl('J')) {
                    /* ctrl-j terminates the search leaving the buffer in place */
                    c = 0;
                }
                /* Go process the char normally */
                refreshLine(current->prompt, current);
                goto process_char;
            }
            break;
        case ctrl('T'):    /* ctrl-t */
            if (current->pos > 0 && current->pos <= current->chars) {
                /* If cursor is at end, transpose the previous two chars */
                int fixer = (current->pos == current->chars);
                c = get_char(current, current->pos - fixer);
                remove_char(current, current->pos - fixer);
                insert_char(current, current->pos - 1, c);
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('V'):    /* ctrl-v */
            if (has_room(current, 3)) {
                /* Insert the ^V first */
                if (insert_char(current, current->pos, c)) {
                    refreshLine(current->prompt, current);
                    /* Now wait for the next char. Can insert anything except \0 */
                    c = fd_read(current);

                    /* Remove the ^V first */
                    remove_char(current, current->pos - 1);
                    if (c != -1) {
                        /* Insert the actual char */
                        insert_char(current, current->pos, c);
                    }
                    refreshLine(current->prompt, current);
                }
            }
            break;
        case ctrl('B'):
        case SPECIAL_LEFT:
            if (current->pos > 0) {
                current->pos--;
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('F'):
        case SPECIAL_RIGHT:
            if (current->pos < current->chars) {
                current->pos++;
                refreshLine(current->prompt, current);
            }
            break;
        case SPECIAL_PAGE_UP:
          dir = history_len - history_index - 1; /* move to start of history */
          goto history_navigation;
        case SPECIAL_PAGE_DOWN:
          dir = -history_index; /* move to 0 == end of history, i.e. current */
          goto history_navigation;
        case ctrl('P'):
        case SPECIAL_UP:
            dir = 1;
          goto history_navigation;
        case ctrl('N'):
        case SPECIAL_DOWN:
history_navigation:
            if (history_len > 1) {
                /* Update the current history entry before to
                 * overwrite it with tne next one. */
                free(history[history_len - 1 - history_index]);
                history[history_len - 1 - history_index] = strdup(current->buf);
                /* Show the new entry */
                history_index += dir;
                if (history_index < 0) {
                    history_index = 0;
                    break;
                } else if (history_index >= history_len) {
                    history_index = history_len - 1;
                    break;
                }
                set_current(current, history[history_len - 1 - history_index]);
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('A'): /* Ctrl+a, go to the start of the line */
        case SPECIAL_HOME:
            current->pos = 0;
            refreshLine(current->prompt, current);
            break;
        case ctrl('E'): /* ctrl+e, go to the end of the line */
        case SPECIAL_END:
            current->pos = current->chars;
            refreshLine(current->prompt, current);
            break;
        case ctrl('U'): /* Ctrl+u, delete to beginning of line, save deleted chars. */
            if (remove_chars(current, 0, current->pos)) {
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('K'): /* Ctrl+k, delete from current to end of line, save deleted chars. */
            if (remove_chars(current, current->pos, current->chars - current->pos)) {
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('Y'): /* Ctrl+y, insert saved chars at current position */
            if (current->capture && insert_chars(current, current->pos, current->capture)) {
                refreshLine(current->prompt, current);
            }
            break;
        case ctrl('L'): /* Ctrl+L, clear screen */
            linenoiseClearScreen();
            /* Force recalc of window size for serial terminals */
            current->cols = 0;
            refreshLine(current->prompt, current);
            break;
        default:
            /* Only tab is allowed without ^V */
            if (c == '\t' || c >= ' ') {
                if (insert_char(current, current->pos, c) == 1) {
                    refreshLine(current->prompt, current);
                }
            }
            break;
        }
    }
    return current->len;
}
Esempio n. 5
0
ItemContent::ItemContent(string json_contents, size_t item_pos, string item_id) {
    ItemId = item_id;
    ItemPos = item_pos;
    vector<string> Genres;
    vector<string> Directors;
    vector<string> Awards;
    vector<string> Actors;
    vector<string> PlotTerms;
    vector<string> Title;

    vector<string> content_values = split(json_contents, "\",\"");
    for (auto raw_token: content_values) {
        string token(raw_token);
        token = str2lower(token);
        token = remove_chars(token, "\"");
        string token_value = get_token_value(token);
        token = remove_chars(token, ",");
        if (token_value != NONE_STR) {
            if (!starts_with(token, "year") && !starts_with(token, "imdbrating"))
                token_value = remove_chars(token_value, INVALID_CHARS);

            if (starts_with(token, "title")) {
                Title = split(token_value, " ");
            }
            else if (starts_with(token, "genre"))
                Genres =  split(token_value, ",");

            /*else if (starts_with(token, "director")) {
                token_value = remove_chars(token_value," ");
                Directors = split(token_value, ",");
            }
            else if (starts_with(token, "actors")) {
                token_value = remove_chars(token_value, " ");
                Actors = split(token_value, ",");
            }*/

            else if (starts_with(token, "country"))
               MainTerms.push_back(token_value);

            else if (starts_with(token, "type")) {
               MainTerms.push_back(token_value);
            }
            
            else if (starts_with(token, "company"))
               MainTerms.push_back(token_value);

            else if (starts_with(token, "awards"))
                Awards = split(token_value, ",");

            else if (starts_with(token, "plot")) {
                token_value = remove_chars(token_value, ",");
                Plot = token_value;
            }
            // Extracting the decade 198X
            else if (starts_with(token, "year")) {
                    Year = stoi(token_value);
                    MainTerms.push_back(token_value.substr(0, token_value.size()-1) + "X");                 
            }
            else if (starts_with(token, "imdbrating")) {
                    imdbRating = stof(token_value);
            }
        }
    }

    
    append_vectors<string>(MainTerms, Title);

    for (size_t i = 0; i <= 5; i++)
        append_vectors<string>(MainTerms, Genres);    

    append_vectors<string>(MainTerms, Awards);


    /*Make the vector too much sparse */

    //append_vectors<string>(MainTerms, Actors);
    
    //append_vectors<string>(MainTerms, Directors);
    

    // Update main terms set to be used during the feature vector build
    for (string term: MainTerms)
        UniqueMainTerms.insert(term);


    // Make a reinforcement of the terms if they exists
    // in the plot too - The bool value stands for reinforcement only
    analyze_terms(PlotTerms, false);

    // Update main terms set to be used during the feature vector build
    append_vectors<string>(MainTerms, PlotTerms);

    for (string term: PlotTerms)
        UniqueMainTerms.insert(term);
    
    Title.clear();
    Genres.clear();
    Directors.clear();
    Actors.clear();
    PlotTerms.clear();
}