Esempio n. 1
0
bool Dictionary::isNewWord(std::string playerWord){
    if(isInDictionary(playerWord)){
        return false;
    }else{
        return true;
    }

}
Esempio n. 2
0
int main(int argc, char *argv[]) {

    // Needs exactly one argument
    if (argc != 2) {
        printf("No word provided. Usage: `dictionary.exe WORD`\n");
        return 1;
    }
    
    // Store the word
    string word = argv[1];
    
    // Test word and indicate result
    if (1 == isInDictionary(word)) {
        printf("YES\n");
    }
    else {
        printf("NO\n");
    }

    return 0;
}
Esempio n. 3
0
void LZ78::compress(const std::string* outfile) {
    //unsigned char last_index = 0x00;
    unsigned long last_index = 0x00000000;
    this->dict = new map<std::vector<unsigned char>, unsigned long>();
    //char buffer[1];
    unsigned char buffer;
    //size_t size = this->filename->size() + 1;
    //char * bufferName = new char[ size ];
    // copier la chaîne
    debug("Compressing...", DEBUG);
    if (this->filename != NULL) {
        debug("Opening from file : " + *(this->filename), DEBUG);
        if (!freopen(this->filename->c_str(), "rb", stdin)) {
            debug("Error opening file !", CRITICAL);
        }
    }
    //strncpy(bufferName, this->filename.c_str(), size);

    //ifstream file(bufferName, std::ios::in | std::ios::binary);
    //delete bufferName;

    //FILE *fp;
    if (outfile != NULL) {
        debug("Opening outfile : " + *outfile, DEBUG);
        if (!freopen(outfile->c_str(), "wb", stdout)) {
            debug("Error opening file for output !", CRITICAL);
        }
    }

    // Since EOF can be returned by fgect, feof(STREAM*) has to be used
    // for binary files
    bool eofile = false;
    std::vector<unsigned char> currBlock;

    while ((!eofile)) {
        // read a new Byte
        buffer = fgetc(stdin);

        currBlock.push_back(buffer);

        if (feof(stdin)) {
            debug("End Of file", DEBUG);
            eofile = true;
            //read to far :-)
            currBlock.pop_back();
            continue;
        }
        if (ferror(stdin)) {
            // in case of error, terminate compression loop
            debug("ERROR while reading file, please submit a bug report to <*****@*****.**> including your file and the command you entered", CRITICAL);
            break;
        } else {
            // if everything is ok, compression can proceed

            if (this->dict->size() >= MAX_DICSIZE) {
                debug("Cleaning dictionary, it was full !",DEBUG);
                this->dict->clear();
                last_index = 0x00000000;
            }

            if (isInDictionary(currBlock) != 0 && this->dict->size() < MAX_DICSIZE) {
                debug("Found  currBlock  in dictionnary, continuing match !", DEBUG);
                // in dictionary, try to find the longest match
            } else {
                debug("Not in dictionary !", DEBUG);
                // not in dictionary
                //length 1, this is the start... just add it in dict
                //length >1, then previous string was matched
                last_index += 0x00000001;


                //fprintf(stderr,"last index is %lu \n",last_index);

                if (currBlock.size() == 1) {
		    
                    this->dict->insert(std::pair<std::vector<unsigned char>, unsigned long>(currBlock, last_index));

                    //debug(ulongToString(&last_index) + "[" + buffer + "]", DEBUG);

                    writeDictToByte(MISS);
                    fputc(currBlock.front(), stdout);
                    fflush(stdout);
                } else {
                    //fprintf(stderr,"Adding to dict...\n");
                    // substring was a "match"
                    std::vector<unsigned char> prev = std::vector<unsigned char>(currBlock);
                    prev.pop_back();
                    unsigned long match = isInDictionary(prev);
                    this->dict->insert(std::pair<std::vector<unsigned char>, unsigned long>(currBlock, last_index));

                    // debug("Block of symbol:" + currBlock + "(" + makestring(last_index) + ")", DEBUG);
                    //debug(ulongToString(&last_index) + "[" + buffer + "]", DEBUG);

                    writeDictToByte(SET_MATCH(match));
                    //fputc(currBlock.substr(currBlock.size() - 1, currBlock.size()).c_str()[0], stdout);
                    fputc(currBlock.at(currBlock.size()-1),stdout);
                    fflush(stdout);
                }
                currBlock.clear();
            }
        }

    }
    
    // EOFILE reached
    if (currBlock.size() != 0) {
        // was found in dictionary but has not been inserted in output file, this is the last word
        //debug(makestring(isInDictionary(currBlock)), DEBUG);
        /*for (std::vector<unsigned char>::iterator it = currBlock.begin(); it!=currBlock.end(); ++it) {
            fprintf(stderr,"0x%x ",*it);
        }*/
        if (isInDictionary(currBlock)) {
            if (currBlock.size()!=1) {
                //fprintf(stderr,"Is dict ! :D %lu",isInDictionary(currBlock));
                unsigned char trailing;
                trailing = *(currBlock.end()-1);
                currBlock.pop_back();
                writeDictToByte(SET_MATCH(isInDictionary(currBlock)));
                fputc(trailing,stdout);
            } else {
		//fprintf(stderr,"Not in dict :(\n");
                writeDictToByte(MISS);
		fputc(currBlock.at(0),stdout);
            }
        }
        else {
            //end of block prevends from getting dictionary entry...
            //fprintf(stderr,"Not in dict :(");
            std::vector<unsigned char> copy(currBlock);
            copy.pop_back();
            writeDictToByte(SET_MATCH(isInDictionary(copy)));
	    writeDictToByte(MISS);
            //fputc(MISS,stdout);
            fputc(*(currBlock.end()-1),stdout);
        }
    }

    /*
     * Manual termination of the compressed file (depends on DICT_NUMBYTE)
     */
    if (DICT_NUMBYTE == 3) {
        fputc(0x7F, stdout);
    }
    if (DICT_NUMBYTE >= 2) {
        if (DICT_NUMBYTE == 2) {
            fputc(0x7F, stdout);
        } else {
            fputc(0xFF, stdout);
        }
    }
    if (DICT_NUMBYTE >= 1) {
        if (DICT_NUMBYTE == 1) {
            fputc(0x7F, stdout);
        } else {
            fputc(0xFF, stdout);
        }
    }
    fclose(stdin);
    fclose(stdout);
    debug("Done !", DEBUG);
}