Exemple #1
0
void Lexer::setToken()
{
  std::string tmp;
  std::size_t pos = 0, begin = 0;
  std::string::iterator end;
  std::string::size_type comment_start;
  std::string::size_type newline;

  std::replace(_string.begin(), _string.end(), '\t', ' ');
  _string += "exit";
  _string = trim_comments(_string);
  _string = trim_string(_string);

  for (std::string::iterator it = _string.begin() ; it + pos != _string.end() ; ++it)
    {
      if ((int)(pos = myMin(_string.find('\n', pos), _string.find(' ', pos))) == -1)
	{
	  _token.push_back("exit");
	  return;
	}
      tmp = _string.substr(begin, pos-begin);
      pos++;
      begin = pos;
      _token.push_back(tmp);
    }
}
static int
parse_ip_list(FPST ** const ip_list_p, const char * const file)
{
    char       buf[MAX_QNAME_LENGTH + 1U];
    char      *line;
    FILE      *fp;
    char      *ptr;
    FPST      *ip_list;
    FPST      *ip_list_tmp;
    size_t     line_len;
    BlockType  block_type = BLOCKTYPE_UNDEFINED;
    int        ret = -1;

    assert(ip_list_p != NULL);
    *ip_list_p = NULL;
    ip_list = fpst_new();
    if ((fp = fopen(file, "r")) == NULL) {
        return -1;
    }
    while (fgets(buf, (int) sizeof buf, fp) != NULL) {
        if ((line = trim_comments(untab(buf))) == NULL || *line == 0) {
            continue;
        }
        line_len = strlen(line);
        if (line[line_len - 1] == '*') {
            line[line_len - 1] = 0;
            block_type = BLOCKTYPE_PREFIX;
        } else {
            block_type = BLOCKTYPE_EXACT;
        }
        str_tolower(line);
        if ((line = strdup(line)) == NULL) {
            break;
        }
        if ((ip_list_tmp = fpst_insert_str(ip_list, line,
                                           (uint32_t) block_type)) == NULL) {
            free(line);
            break;
        }
        ip_list = ip_list_tmp;
    }
    if (!feof(fp)) {
        fpst_free(ip_list, free_list);
    } else {
        *ip_list_p = ip_list;
        ret = 0;
    }
    fclose(fp);

    return ret;
}
static int
parse_domain_list(FPST ** const domain_list_p,
                  FPST ** const domain_rev_list_p,
                  FPST ** const domain_substr_list_p,
                  const char * const file)
{
    char       buf[MAX_QNAME_LENGTH + 1U];
    char      *line;
    FILE      *fp;
    char      *ptr;
    FPST      *domain_list;
    FPST      *domain_list_tmp;
    FPST      *domain_rev_list;
    FPST      *domain_rev_list_tmp;
    FPST      *domain_substr_list;
    FPST      *domain_substr_list_tmp;
    size_t     line_len;
    BlockType  block_type = BLOCKTYPE_UNDEFINED;
    int        ret = -1;

    assert(domain_list_p != NULL);
    assert(domain_rev_list_p != NULL);
    assert(domain_substr_list_p != NULL);
    *domain_list_p = NULL;
    *domain_rev_list_p = NULL;
    *domain_substr_list_p = NULL;
    domain_list = fpst_new();
    domain_rev_list = fpst_new();
    domain_substr_list = fpst_new();
    if ((fp = fopen(file, "r")) == NULL) {
        return -1;
    }
    while (fgets(buf, (int) sizeof buf, fp) != NULL) {
        if ((line = trim_comments(untab(buf))) == NULL || *line == 0) {
            continue;
        }
        line_len = strlen(line);
        if (line[0] == '*' && line[line_len - 1] == '*') {
            line[line_len - 1] = 0;
            line++;
            block_type = BLOCKTYPE_SUBSTRING;
        } else if (line[line_len - 1] == '*') {
            line[line_len - 1] = 0;
            block_type = BLOCKTYPE_PREFIX;
        } else {
            if (line[0] == '*') {
                line++;
            }
            if (line[0] == '.') {
                line++;
            }
            str_reverse(line);
            block_type = BLOCKTYPE_SUFFIX;
        }
        if (*line == 0) {
            continue;
        }
        str_tolower(line);
        if ((line = strdup(line)) == NULL) {
            break;
        }
        if (block_type == BLOCKTYPE_SUFFIX) {
            if ((domain_rev_list_tmp = fpst_insert_str(domain_rev_list, line,
                                                       (uint32_t) block_type)) == NULL) {
                free(line);
                break;
            }
            domain_rev_list = domain_rev_list_tmp;
        } else if (block_type == BLOCKTYPE_PREFIX) {
            if ((domain_list_tmp = fpst_insert_str(domain_list, line,
                                                   (uint32_t) block_type)) == NULL) {
                free(line);
                break;
            }
            domain_list = domain_list_tmp;
        } else if (block_type == BLOCKTYPE_SUBSTRING) {
            if ((domain_substr_list_tmp = fpst_insert_str(domain_substr_list, line,
                                                          (uint32_t) block_type)) == NULL) {
                free(line);
                break;
            }
            domain_substr_list = domain_substr_list_tmp;
        } else {
            free(line);
        }
    }
    if (!feof(fp)) {
        fpst_free(domain_list, free_list);
        fpst_free(domain_rev_list, free_list);
        fpst_free(domain_substr_list, free_list);
    } else {
        *domain_list_p = domain_list;
        *domain_rev_list_p = domain_rev_list;
        *domain_substr_list_p = domain_substr_list;
        ret = 0;
    }
    fclose(fp);

    return ret;
}
Exemple #4
0
int main() {
    std::string code = "push int32(50)\npush int32(45)\nadd\ndump\n;;";
    std::cout << code << "\n\n======\n\n" << trim_comments(code) << '\n';
}