bool canSubmit ( unsigned long h , long now , long maxAddUrlsPerIpDomPerDay ) {
	// . sometimes no limit
	// . 0 means no limit because if they don't want any submission they
	//   can just turn off add url and we want to avoid excess 
	//   troubleshooting for why a url can't be added
	if ( maxAddUrlsPerIpDomPerDay <= 0 ) return true;
	// init the table
	if ( ! s_init ) {
		s_htable.set ( 50000 );
		s_init = true;
	}
	// clean out table every 24 hours
	if ( now - s_lastTime > 24*60*60 ) {
		s_lastTime = now;
		s_htable.clear();
	}
	// . if table almost full clean out ALL slots
	// . TODO: just clean out oldest slots
	if ( s_htable.getNumSlotsUsed() > 47000 ) s_htable.clear ();
	// . how many times has this IP domain submitted?
	// . allow 10 times per day
	long n = s_htable.getValue ( h );
	// if over 24hr limit then bail
	if ( n >= maxAddUrlsPerIpDomPerDay ) return false;
	// otherwise, inc it
	n++;
	// add to table, will replace old values
	s_htable.addKey ( h , n );
	return true;
}
// . list of types is on: http://www.duke.edu/websrv/file-extensions.html
// . i copied it to the bottom of this file though
const char *HttpMime::getContentTypeFromExtension ( char *ext ) {
	// assume text/html if no extension provided
	if ( ! ext || ! ext[0] ) return "text/html";
	// get hash for table look up
	long key = hash32n ( ext );
	char *ptr = (char *)s_mimeTable.getValue ( key );
	if ( ptr ) return ptr;
	// if not found in table, assume text/html
	return "text/html";
}
示例#3
0
int main() {
    ifstream inFile;
    inFile.open("hw4prob3in");
    if (!inFile) {
        cout << "There was an error opening hw4prob3in";
        return 1;
    }

    int wordsToSearch;
    inFile >> wordsToSearch;
    string words[wordsToSearch];
    
    for (int i = 0; i < wordsToSearch; i++) {
       inFile >> words[i]; 
    }

    string longWord;
    inFile >> longWord;

    inFile.close();

    int tableSize = getPrimeTableSize(longWord.size());
    HashTable * myHashTable = new HashTable(tableSize);

    int lengthOfWords = (int)words[0].size();
    for (int i = 0; i <= (int)longWord.size() - lengthOfWords; i++) {
        string wordToHash = longWord.substr(i, lengthOfWords); 
        myHashTable->insert(wordToHash, i);
    }

    ofstream outFile;
    outFile.open("hw4prob3out");
    
    outFile << "start end" << endl;
    
    for (int i = 0; i < wordsToSearch; i++) {
        int value = myHashTable->getValue(words[i]);
        if (value == -1) {
            outFile << "-1 -1" << endl;
        } else {
            outFile << value << " " << value + (int)words[i].size() - 1 << endl;
        }
    }

    outFile.close();

    myHashTable = NULL;
    delete[] myHashTable;
    return 0;
}
iconv_t gbiconv_open( char *tocode, char *fromcode) {
	// get hash for to/from
	unsigned long hash1 = hash32Lower_a(tocode, gbstrlen(tocode), 0);
	unsigned long hash2 = hash32Lower_a(fromcode, gbstrlen(fromcode),0);
	unsigned long hash = hash32h(hash1, hash2);

	g_errno = 0;
	iconv_t conv = (iconv_t)s_convTable.getValue(hash);
	//log(LOG_DEBUG, "uni: convertor %s -> %s from hash 0x%lx: 0x%lx",
	//    fromcode, tocode,
	//    hash, conv);
	if (!conv){
		//log(LOG_DEBUG, "uni: Allocating new convertor for "
		//    "%s to %s (hash: 0x%lx)",
		//    fromcode, tocode,hash);
		conv = iconv_open(tocode, fromcode);
		if (conv == (iconv_t) -1) {
			log(LOG_WARN, "uni: failed to open converter for "
			    "%s to %s: %s (%d)", fromcode, tocode, 
			    strerror(errno), errno);
			// need to stop if necessary converters don't open
			//char *xx=NULL; *xx = 0;
			g_errno = errno;
			if (errno == EINVAL)
				g_errno = EBADCHARSET;
			
			return conv;
		}
		// add mem to table to keep track
		g_mem.addMem((void*)conv, 52, "iconv", 1);
		// cache convertor
		s_convTable.addKey(hash, (long)conv);
		//log(LOG_DEBUG, "uni: Saved convertor 0x%ld under hash 0x%lx",
		//    conv, hash);
	}
	else{
		// reset convertor
		char *dummy = NULL;
		size_t dummy2 = 0;
		// JAB: warning abatement
		//size_t res = iconv(conv,NULL,NULL,&dummy,&dummy2);
		iconv(conv,NULL,NULL,&dummy,&dummy2);
	}

	return conv;
}
示例#5
0
int main( int argc, char **argv ) {
    
    HashTable hash;
    
    HashTableInit(&hash);
    
    unsigned char c = 'd';
    
    hash.create(65536);
    
    hash.put("hi", &c, sizeof(unsigned char));
    
    printf("%s\n", hash.getValue("hi"));
    
    hash.destroy();

    
}