Example #1
0
/*
* Takes in the following parameters
* File - The file to en-/decode
* Key - file containing the key used for en-/decoding the file
*/
int main (int argc, char **argv) {
	
	// Check if the program was started with two parameters (file and key)
	if (argc == 3){
		FileContainer files;
		int securityDistance;
		
		if (checkFiles(argv) != 0) {
			files = openAndReadKey(argv[2]);
			char action = getUserInput();
			int choice = encodeOrDecode(action);

			switch (encodeOrDecode(action)) {
			case 0:
				printMessage(TYPE_INFO, CHOICE_EXIT);
				free(files.key);
				exit(0);
			case 1:
				securityDistance = getWantedSecLvl();
				
				printMessage(TYPE_INFO, INFO_ENCODING_START);
				encodeFile(files, argv[1], securityDistance);
				printMessage(TYPE_INFO, INFO_ENCODING_END);
				break;
			case 2:
				printMessage(TYPE_INFO, INFO_DECODING_START);
				decodeFile(files, argv[1]);
				printMessage(TYPE_INFO, INFO_DECODING_END);
				break;
			default:
				break;
			}
		}
	} else {
		printMessage(TYPE_ERROR, ERROR_PARAMETERS);
	}

	return 0;
}
Example #2
0
File: main.c Project: ASchurman/LZW
/* Processes the command line arguments and calls the appropriate function from
 * lzw.h */
int main(int argc, char** argv)
{
    MODE mode; /* indicates whether lzw is called as encode or decode */
    
    // check argv[0] to see if it's encode or decode
    if(argc == 0 || (mode = encodeOrDecode(argv[0])) == INVALID)
    {
        argsError();
        return INVALID_ARGS;
    }
    else if(mode == DECODE)
    {
        if(argc > 1) // decode cannot have additional args
        {
            argsError();
            return INVALID_ARGS;
        }
        else
        {
            if(!decode())
            {
                fprintf(stderr, "Error on decode; invalid encoded stream\n");
                return FAILED_DECODE;
            }
        }
    }
    else // mode == ENCODE
    {
        long maxBits = 0; // value of -m argument, or 0 if there's no -m
        long window = 0; // value of -p argument, or 0 if there's no -p
        bool eFlag = false; // true if -e flag has been seen
        
        // iterate over args
        for(unsigned int i = 1; i < argc; i++)
        {
            FLAG argType = checkFlag(argv[i]);
            
            switch(argType)
            {
                case M:
                    i++;
                    if(i >= argc || // there is no following number arg
                       (maxBits = checkNumArg(argv[i])) <= 0)
                    {
                        argsError();
                        return 1;
                    }
                    else
                    {
                        // maxBits is a positive int; now set to correct value
                        // if out of range
                        if(maxBits <= 8 || maxBits > 24)
                        {
                            maxBits = 12;
                        }
                    }
                    break;
                    
                case P:
                    i++;
                    if(i >= argc || // there is no following number arg
                       (window = checkNumArg(argv[i])) <= 0)
                    {
                        argsError();
                        return 1;
                    }
                    break;
                    
                case E:
                    eFlag = true;
                    break;
                    
                default:
                    argsError();
                    return 1;
            }
        }
        
        if(!maxBits) // if maxBits wasn't set, default to 12
        {
            maxBits = 12;
        }
        
        encode(maxBits, window, eFlag);
    }

    return SUCCESS;
}