Exemplo n.º 1
0
int main(int argc, char *argv[]){
	if(argc == 1){
		printf("Very Few Arguments\n");
		printf("File Name Missing\n");
		exit(1);
	}

	list *l = (list *)malloc(sizeof(list));
	init(l);
	path *paths;
	FILE *fp;
	int i, j, flag = 0, SIZE = 1024, USIZE = 1024, uCount, uFlag = 0, uFlag_1 = 0, uTmp;
	char zipFileName[32] = "", *buffer, *binary, *output, *huffList, character, dflag, cnt, *saket;
	for(i = 0; argv[1][i] != '\0'; i++){
		zipFileName[i] = argv[1][i];
	}
	zipFileName[i++] = '.';
	zipFileName[i++] = 's';
	zipFileName[i++] = 'a';
	zipFileName[i++] = 'k';

	fp = fopen(argv[1], "r");
	if(!fp){
		printf("File Doesn't Exist\n");
		exit(2);
	}

	buffer = (char *)malloc(SIZE * sizeof(char));
	i = 0;
	while((character = getc(fp)) != EOF){
		buffer[i] = character;
		if(i >= (SIZE - 1)){
			SIZE *= 2;
			buffer = (char *)realloc(buffer, SIZE);
		}
		if(flag == 0){
			append(l, character);
			flag = 1;
		}
		else
			searchAndAdd(l, character);
		i++;
	}
	buffer[i] = '\0';
	fclose(fp);

	bubbleSort(l); //Initial Sorting According to Weight.

	l = huffmanTree(l);

	assignPath(l);

	paths = (path *)malloc(l->cnt * sizeof(path));
	storePath(l->head, paths);

	huffList = (char *)malloc(SIZE * sizeof(char));
	storeHuffList(huffList, paths, l->cnt);

	binary = (char *)malloc(SIZE * sizeof(char));
	binaryConversion(buffer, binary, paths);
	printf("%s\n", binary);
	i = strlen(binary);
	printf("%d\n", i);
	free(buffer);
	free(paths);

	appendRedundantBits(binary);
	printf("%s\n", binary);

	output = (char *)malloc(SIZE * sizeof(char));
	binToAscii(binary, output);
	printf("%s\n", output);

	free(binary);

	dflag = (char)252; //11111100
	cnt = (char)l->cnt;//nodeCount
	fp = fopen(zipFileName, "w+");
	fprintf(fp, "%c", cnt);
	fprintf(fp, "%s", huffList);
	fprintf(fp, "%c", dflag);
	fprintf(fp, "%s", output);
	free(huffList);
	free(output);
	fclose(fp);

	printf("\nCompressed\n\t-------------------------------------------------------------------------------------------------------------\n");

	return 0;
}
Exemplo n.º 2
0
void  ConvertMgr::convert (BIT8 *src, Value *pValue, int _iFuncID, int _iLen, int _iBitPos)
{
    static int iAdjustLen;
    int iLen = 0;
    switch (_iFuncID) {
        case 0: // 不做转换, 内存直接赋值
            //memcpy (pValue->m_sValue, src, _iLen);
            //pValue->m_sValue[_iLen]=0;
			iLen= _iLen>(sizeof(pValue->m_sValue)-1)?(sizeof(pValue->m_sValue)-1):_iLen;
            memcpy (pValue->m_sValue, src, iLen);
            pValue->m_sValue[iLen]=0;
            break;
        
        case 1: // 将 BIN 格式字段转换成为 ASC, 即数字转换为对应格式化字符串输出
            iAdjustLen = MAX_STRING_VALUE_LEN-1 < _iLen ? MAX_STRING_VALUE_LEN-1 : _iLen ;
            binToAscii (src, pValue->m_sValue, iAdjustLen);
            break;
        
        case 2: // 将 BCD 格式转换成 long (BCD源格式不允许非数字bcd码,若有,则截断,如 123e56-->123)
            pValue->m_lValue = bcdToLong (src, _iLen);
            break;
        
        case 3: // 将 BIN 格式转换为 long
            pValue->m_lValue = binToLong (src, _iLen);            
            if (_iBitPos>=1 && _iBitPos<=8 && _iLen==1) //## 需要取bit位的值
                pValue->m_lValue = getBitValue ((unsigned char)pValue->m_lValue, _iBitPos);
            break;
        
        case 4: // 将 BCD 格式转换成 Ascii 码 (不允许非数字字符)
            iAdjustLen = (MAX_STRING_VALUE_LEN-1)/2 < _iLen ? (MAX_STRING_VALUE_LEN-1)/2 : _iLen ;
            bcdToAscii_Left_D (src, (BIT8 *)pValue->m_sValue, iAdjustLen);
            break;
        
        case 5: // 将 BCD 格式转换成 Ascii 码 (允许非数字字符A~F)
            iAdjustLen = (MAX_STRING_VALUE_LEN-1)/2 < _iLen ? (MAX_STRING_VALUE_LEN-1)/2 : _iLen ;
            bcdToAscii_Left_X (src, (BIT8 *)pValue->m_sValue, iAdjustLen);
            break;
            
        case 6: // Ascii码格式转换成Long
            pValue->m_lValue = asciiToLong (src, _iLen);
            break;
        

        case 7: // 将 BIN 格式转换为 long
            pValue->m_lValue = binToLong2 (src, _iLen);
            if (_iBitPos>=1 && _iBitPos<=8 && _iLen==1) //## 需要取bit位的值
                pValue->m_lValue = getBitValue ((unsigned char)pValue->m_lValue, _iBitPos);
            break;

        case 8: // 将ASC码的 费用(单位元) 转换成数值型的费用(单位分)
            iAdjustLen = MAX_STRING_VALUE_LEN-1 < _iLen ? MAX_STRING_VALUE_LEN-1 : _iLen ;
            memcpy (pValue->m_sValue, src, iAdjustLen);
            pValue->m_sValue[iAdjustLen]=0;
            //pValue->m_lValue = (long)(atof(pValue->m_sValue)*100);
            strcat(pValue->m_sValue,"e+2");
            pValue->m_lValue = (long)(atof(pValue->m_sValue));
            break;
        
        case 9:
            longToString (src, (BIT8 *)pValue->m_sValue, _iLen);
            break;

        default :
            break;
    }
}