Esempio n. 1
0
char *strcasestr(const char *haystack, const char *needle)
{
    char *h1 = lowerstr(haystack);
    char *n1 = lowerstr(needle);
    char *s1 = strstr(h1, n1);
    char *res = NULL;

    if(s1) {
       int a = s1-h1;
       res = (char *)(haystack+a);
    }
    if(h1) free(h1);
    if(n1) free(n1);
    return res;
}
void buildtree(word **w, char **lines, int nlines) {
    char *wstr = (char*)malloc(sizeof(char[MAXSTRLEN]));
    for (int i = 0; i < nlines; i++) {
        while (getword(wstr, &(lines[i])))
            *w = add2tree(*w, lowerstr(wstr));
    }
}
Esempio n. 3
0
static void parse_units(char *value) {
	lowerstr(value);
	if (strcmp(value, "iec")==0)
		units= UNITS_IEC;
	else if (strcmp(value, "si")==0)
		units= UNITS_SI;
}
Esempio n. 4
0
struct mime_handler *get_mime_handler(const char *mimetype)
{
    struct mime_handler *handler;
    char *tempstring;

    if (!mimetype) return NULL;

    tempstring = lowerstr(mimetype);

    handler = mime_handlers;

    while (handler) {
        regexp *treg;

        treg = regcomp((char *)handler->mimetype);
        if (treg) {
            if (regexec(treg, tempstring)) {
                free(treg);  free(tempstring); return handler;
            }
            free(treg);
        }
        handler = handler->next;
    }

    free(tempstring);

    return NULL;
}
Esempio n. 5
0
static void
read_dictionary(DictSyn *d, char *filename)
{
	char	   *real_filename = get_tsearch_config_filename(filename, "rules");
	tsearch_readline_state trst;
	char	   *line;
	int			cur = 0;

	if (!tsearch_readline_begin(&trst, real_filename))
		ereport(ERROR,
				(errcode(ERRCODE_CONFIG_FILE_ERROR),
				 errmsg("could not open synonym file \"%s\": %m",
						real_filename)));

	while ((line = tsearch_readline(&trst)) != NULL)
	{
		char	   *value;
		char	   *key;
		char	   *end = NULL;

		if (*line == '\0')
			continue;

		value = lowerstr(line);
		pfree(line);

		key = find_word(value, &end);
		if (!key)
		{
			pfree(value);
			continue;
		}

		if (cur == d->len)
		{
			d->len = (d->len > 0) ? 2 * d->len : 16;
			if (d->syn)
				d->syn = (Syn *) repalloc(d->syn, sizeof(Syn) * d->len);
			else
				d->syn = (Syn *) palloc(sizeof(Syn) * d->len);
		}

		d->syn[cur].key = pnstrdup(key, end - key);
		d->syn[cur].value = value;

		cur++;
	}

	tsearch_readline_end(&trst);

	d->len = cur;
	if (cur > 1)
		qsort(d->syn, d->len, sizeof(Syn), compare_syn);

	pfree(real_filename);
}
Esempio n. 6
0
void add_mime_handler(const char *mimetype, int priority, MimeFn function)
{
    struct mime_handler *handler, **temp;

    handler = (struct mime_handler *)malloc(sizeof(struct mime_handler));
    handler->mimetype = lowerstr(mimetype);
    handler->handler = function;
    handler->priority = priority;
    handler->next = mime_handlers;

    temp = &mime_handlers;

    while(*temp && ((*temp)->priority <= priority))
        temp = &((*temp)->next);

    handler->next = *temp;
    *temp = handler;
}
Esempio n. 7
0
void load_config(void) {
	if ((lang = malloc(strlen(DEFAULT_LANGUAGE)+1))==NULL)
		print_error("Out of memory");
	strcpy(lang, DEFAULT_LANGUAGE);
	if ((locale = malloc(strlen(DEFAULT_LOCALE)+1))==NULL)
		print_error("Out of memory");
	strcpy(locale, DEFAULT_LOCALE);
	units = DEFAULT_UNITS;
	ifaces = NULL;
	FILE *stream;

	if ((stream = fopen(CONFIG_PATH, "r")) == NULL)
		print_error("Can't open config file");;
	while (!feof(stream)) {
		char *line = read_line(stream);
		char *directive, *value;
		parse_line(line, &directive, &value);

		if (directive!=NULL) {
			lowerstr(directive);
			if (strcmp(directive, "language")== 0) {
				parse_language(value);
			}
			else if (strcmp(directive, "locale")== 0) {
				parse_locale(value);
			}
			else if (strcmp(directive, "units")==0) {
				parse_units(value);
			}
			else if (strcmp(directive, "interface")==0) {
				parse_interface(value);
			}
			free(directive);
			free(value);
		}
		free(line);
	}

	if (ifaces == NULL) {
		print_error("No interface specified in config file.");
	}
}
Esempio n. 8
0
int main(void)  {

    struct tnode *root            = NULL;
    char   word[MAXWORDSIZE]      = "";
    char   unstemmed[MAXWORDSIZE] = "";
    int    line                   = 1;

    while (getword(word, MAXWORDSIZE) != EOF)
        if (isalpha(word[0])) {
            lowerstr(word);
            if (isnotstopword(word)) {
                strncpy(unstemmed, word, MAXWORDSIZE);
                squeezechar(word, '\'');
                word[stem(word, 0, strlen(word) - 1) + 1] = '\0';
                root = treeadd(root, word, unstemmed, line);
            }
        } else if (word[0] == '\n')
            line++;

    treeprint(root);

    return 0;
}
Esempio n. 9
0
static void
read_dictionary(DictSyn *d, char *filename)
{
	char	   *real_filename = get_tsearch_config_filename(filename, "rules");
	tsearch_readline_state trst;
	char	   *line;
	int			cur = 0;

	if (!tsearch_readline_begin(&trst, real_filename))
		ereport(ERROR,
				(errcode(ERRCODE_CONFIG_FILE_ERROR),
				 errmsg("could not open synonym file \"%s\": %m",
						real_filename)));

	while ((line = tsearch_readline(&trst)) != NULL)
	{
		char	   *value;
		char	   *key;
		char	   *pos;
		char	   *end;

		if (*line == '\0')
			continue;

		value = lowerstr(line);
		pfree(line);

		pos = value;
		while ((key = find_word(pos, &end)) != NULL)
		{
			/* Enlarge syn structure if full */
			if (cur == d->len)
			{
				d->len = (d->len > 0) ? 2 * d->len : 16;
				if (d->syn)
					d->syn = (Syn *) repalloc(d->syn, sizeof(Syn) * d->len);
				else
					d->syn = (Syn *) palloc(sizeof(Syn) * d->len);
			}

			/* Save first word only if we will match it */
			if (pos != value || d->matchorig)
			{
				d->syn[cur].key = pnstrdup(key, end - key);
				d->syn[cur].value = pstrdup(value);

				cur++;
			}

			pos = end;

			/* Don't bother scanning synonyms if we will not match them */
			if (!d->matchsynonyms)
				break;
		}

		pfree(value);
	}

	tsearch_readline_end(&trst);

	d->len = cur;
	if (cur > 1)
		qsort(d->syn, d->len, sizeof(Syn), compare_syn);

	pfree(real_filename);
}
Esempio n. 10
0
void printToken( FILE * listing, TokenType token, TokenStruct & tokenStruct ) { 
  switch (token) { 
	// Tags
	case OPENTAG: 
			switchflag = true;
			checktoken.push(token);
		break;
	case CLOSETAG: 
			switchflag = false;
			if(!checktoken.empty()&&checktoken.top()==OPENTAG)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: TAG, Row: %d\n", tokenStruct.row);
		break;
    case OPENDOCNO:
			usefulinfo = true;
			if(!switchflag)
			{
				fprintf( listing, "$ID\n" ); 
				numkeep = true;
			}
			checktoken.push(token);
		break;
    case CLOSEDOCNO: 
			usefulinfo = false;
			numkeep = false;
			if(!checktoken.empty()&&checktoken.top()==OPENDOCNO)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: DOCNO, Row: %d\n", tokenStruct.row);
		break;
    case OPENTEXT: 
			usefulinfo = true;
			if(!switchflag)
				fprintf( listing, "$NARR\n" );
			checktoken.push(token);
		break;
    case CLOSETEXT:
			usefulinfo = false;
			if(!checktoken.empty()&&checktoken.top()==OPENTEXT)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: TEXT, Row: %d\n", tokenStruct.row);
		break;
    case OPENDOC: 
			usefulinfo = true;
			if(!switchflag)
				fprintf( listing, "$DESC\n" );
			checktoken.push(token);			
		break;
    case CLOSEDOC: 
			usefulinfo = false;
			if(!checktoken.empty()&&checktoken.top()==OPENDOC)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: DOC, Row: %d\n", tokenStruct.row);
		break;
	case OPENGRAPHIC:
			usefulinfo = true;
			if(!switchflag)
				fprintf( listing, "$DESC\n" );
			checktoken.push(token);	
		break;
	case CLOSEGRAPHIC:
			usefulinfo = false;
			if(!checktoken.empty()&&checktoken.top()==OPENGRAPHIC)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: DOC, Row: %d\n", tokenStruct.row);
		break;
    case OPENHEADLINE: 
			usefulinfo = true;
			if(!switchflag)
				fprintf( listing, "$TITLE\n" ); 
			checktoken.push(token);	
		break;
    case CLOSEHEADLINE: 
			usefulinfo = false;
			if(!checktoken.empty()&&checktoken.top()==OPENHEADLINE)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: HEADLINE, Row: %d\n", tokenStruct.row);
		break;
		
	case OPENOTHERTAG:
			checktoken.push(token);
		break;
	case CLOSEOTHERTAG:
			if(!checktoken.empty()&&checktoken.top()==OPENOTHERTAG)
				checktoken.pop();
			else
				fprintf(listing, "TAGERROR: OTHER, Row: %d\n", tokenStruct.row);
		break;

	//Punctuation
	case PERIOD: break;
	case QUESTION: break;
	case EXCLAMATION: break;
	case OTHERMARK: break;
	
    case ENDFILE: fprintf( listing, "EOF\n" ); break;

    case NUMBER:
		if(!switchflag&&numkeep&&usefulinfo)
			fprintf( listing, "%s\n", tokenStruct.value.c_str() );
      break;
 
	case WORD:
	{
	  if(!switchflag&&usefulinfo)
	  {
		string s = lowerstr(tokenStruct.value);
		if(!checkStopwords(s))
			fprintf( listing, "%s\n", s.c_str());
	  }
	}
      break;
	case APOSTROPHIZED:
	{
	  if(!switchflag&&usefulinfo)
	  {
		string s = lowerstr(tokenStruct.value);

		if(!checkStopwords(s)&&s.length()>3)
			fprintf( listing, "%s\n", s.c_str());
	  }
	}
	  break;
	case TYPO:
	{
	  if(!switchflag&&usefulinfo)
	  {
		string s = lowerstr(tokenStruct.value);
		string f,b;
		int pos = s.find("'");
		f = s.substr(0,pos);
		b = s.substr(pos+1,s.length());
	  
		if(!checkStopwords(f))
			fprintf( listing, "%s\n", f.c_str());

		if(!checkStopwords(b))
			fprintf( listing, "%s\n", b.c_str());
	  }
	}
	  break;
	case HYPHENATED:
	{
	  if(!switchflag&usefulinfo)
	  {
		string s = lowerstr(tokenStruct.value);
		string f,b;
		int pos = s.find("-");
		f = s.substr(0,pos);
		b = s.substr(pos+1,s.length());
		if(f.length()>3&&b.length()>3)
		{
			if(!checkStopwords(f))
				fprintf(listing, "%s\n", f.c_str());
			if(!checkStopwords(b))
				fprintf(listing, "%s\n", b.c_str());
		}
		else
			fprintf( listing, "%s\n", s.c_str());
	  }
	}
	  break;
	 
    case ERROR:
	  if(!switchflag&usefulinfo)
		fprintf( listing, "ERROR: %s\n", tokenStruct.value.c_str() );
      break;
    default: // should never happen 
      fprintf( listing, "Unknown token: %d\n", token );
  }
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
    char usrkey;
    DIR *dir;
    struct dirent *ent;
    char *newname;
    if(argc > 1)
    {
        if(strcmp(argv[1], "/?") == 0 || strcmp(argv[1], "/h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
        {
            printf("\nLower Filename Case v1\nUsage:\ntolower [dirname]\n\ndirname : directory to operate on, \".\" will be used if empty\n\n**USE AT YOUR OWN RISK**\n\n");
            return 0;
        }
    }
    printf("\nLower Filename Case v1\[email protected]\n\n**USE AT YOUR OWN RISK**\n\nThis will rename all your files in selected directory (default is \".\")\nAre you sure? (type \"y\" to continue)... ");
    if(scanf("%c", &usrkey) == EOF)
        return 0;
    if (usrkey != 'y' && usrkey != 'Y')
        return 0;
    printf("\n\nRenaming files...\n");
    if(argc > 1)
    {
        dir = opendir (argv[1]);
    }
    else
    {
        dir = opendir (".");
    }

    if (dir != NULL)
    {
        while ((ent = readdir (dir)) != NULL)
        {
            if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
            {
                lowerstr(&newname, ent->d_name);
                if (strcmp(newname, ent->d_name) == 0)
                {
                    printf ("\"%s\" is already in lowercase\n", ent->d_name);
                }
                else
                {
                    if (rename(ent->d_name, newname) != 0)
                    {
                        printf ("\"%s\" is cannot be renamed\n", ent->d_name);

                    }
                    else
                    {
                        printf ("\"%s\" is renamed to \"%s\"\n", ent->d_name, newname);
                    }
                }
            }
        }
        free(newname);
        closedir (dir);
    }
    else
    {
        /* could not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
    return 0;
}
Esempio n. 12
0
void process_path_printer(const scanner_params &sp)
{
    /* 1. Get next token 
     * 2. if prefix part is a number, skip forward that much in sbuf and repeat.
     *    if the prefix is PRINT, print the buffer
     *    if next part is a string, strip it and run that decoder.
     *    if next part is a |, print
     * 3. If we are print, throw an exception to prevent continued analysis of buffer.
     */

    std::string new_path = sp.sbuf.pos0.path;
    std::string prefix = get_and_remove_token(new_path);

    /* Time to print ?*/
    if(prefix.size()==0 || prefix=="PRINT"){

	uint64_t print_start = 0;
	uint64_t print_len = 4096;
    
	/* Check for options */
	scanner_params::PrintOptions::iterator it;

	it = sp.print_options.find("Content-Length");
	if(it!=sp.print_options.end()){
	    print_len = stoi64(it->second);
	}

	it = sp.print_options.find("Range");
	if(it!=sp.print_options.end()){
	    if(it->second[5]=='='){
		size_t dash = it->second.find('-');
                std::string v1 = it->second.substr(6,dash-6);
                std::string v2 = it->second.substr(dash+1);
		print_start = stoi64(v1);
		print_len = stoi64(v2)-print_start+1;
	    }
	}

	if(print_start>sp.sbuf.bufsize){
	    print_len = 0;			// can't print anything
	}

	if(print_len>0 && print_start+print_len>sp.sbuf.bufsize){
	    print_len = sp.sbuf.bufsize-print_start;
	}

	switch(scanner_params::getPrintMode(sp.print_options)){
	case scanner_params::MODE_HTTP:
	    std::cout << "Content-Length: "		<< print_len  << HTTP_EOL;
	    std::cout << "Content-Range: bytes "	<< print_start << "-" << print_start+print_len-1 << HTTP_EOL;
	    std::cout << "X-Range-Available: bytes " << 0 << "-" << sp.sbuf.bufsize-1 << HTTP_EOL;
	    std::cout << HTTP_EOL;
	    sp.sbuf.raw_dump(std::cout,print_start,print_len); // send to stdout as binary
	    break;
	case scanner_params::MODE_RAW:
	    std::cout << print_len << HTTP_EOL;
	    std::cout.flush();
	    sp.sbuf.raw_dump(std::cout,print_start,print_len); // send to stdout as binary
	    break;
	case scanner_params::MODE_HEX:
	    sp.sbuf.hex_dump(std::cout,print_start,print_len);
	    break;
	case scanner_params::MODE_NONE:
	    break;
	}
        throw printing_done;
	//return;			// our job is done
    }
    /* If we are in an offset block, process recursively with the offset */
    if(isdigit(prefix[0])){
	uint64_t offset = stoi64(prefix);
	if(offset>sp.sbuf.bufsize){
	    printf("Error: %s only has %u bytes; can't offset to %u\n",
		   new_path.c_str(),(unsigned int)sp.sbuf.bufsize,(unsigned int)offset);
	    return;
	}
	process_path_printer(scanner_params(scanner_params::PHASE_SCAN,
					    sbuf_t(new_path,sp.sbuf+offset),
					    sp.fs,sp.print_options));
	return;
    }
    /* Find the scanner and use it */
    scanner_t *s = be13::plugin::find_scanner(lowerstr(prefix));
    if(s){
        (*s)(scanner_params(scanner_params::PHASE_SCAN,
                            sbuf_t(new_path,sp.sbuf),
                            sp.fs,sp.print_options),
             recursion_control_block(process_path_printer,prefix));
        return;
    }
    std::cerr << "Unknown name in path: " << prefix << "\n";
}