int
ParamBasicOnOff::addChar(const bool input)
{
  if (input == current_state) {
    /* If we are still in the same state, increase the stored counter */
    ++length;
  } else {
    /* The state is changing, use the other function to flush the internal counter */
    /* Length can be null at the begining */
    if (length) {
      addChars(current_state, length);
    }
    /* Reset counter and state */
    current_state = input;
    length = 1;
  }
  return 0;
}
Example #2
0
int main(int argc, char *argv[]) {

unsigned char *buf;
int inFile, num, elem, outFile;
node *head;
node *arr[charMax];
node *indexArr[charMax];

    head = NULL;
    elem = 0;
    buf = malloc(RW_MAX * sizeof(unsigned char));
    if(!buf) {
        perror("malloc");
        exit(4);
    }
    if( argc < 2 ) {
        printf("Usage: inFile [outFile]\n");
        exit(1);
    }
    else { 
        if((inFile = open(argv[1],  O_RDONLY, S_IRUSR )) < 0) {
            perror("can't open inFile");
            exit(2);
        }
        
        if(argc == 3) {
            if((outFile = open(argv[2],  O_WRONLY | O_TRUNC | O_CREAT,
                               S_IRUSR | S_IWUSR )) < 0) {
                perror("can't open outFile");
                exit(2);
            }
        }else if(argc == 2) {
            outFile = STDOUT_FILENO;
        }
        while((num=read(inFile, buf, RW_MAX)) > 0) {
            head = addChars(head, buf, num, &elem);
        }
        
        if(!head) {
            if((write(outFile, &num, sizeof(int))) < 0) {
                 perror("write");
                exit(3);
             }
             if(close(inFile)){
                perror("close");
                exit(47);
            }
            if(close(outFile)){
                perror("close");
                exit(47);
            }
        }else {
            if(elem > charMax)
                elem = charMax;
            makeTable(head, arr);
            arrSort(arr, elem);
            head = createTree(head);
            traverseTree(head);
            arrangeCharArr(arr, indexArr,  elem);

/* prints the table to the start of the file */
            if(head) {
                printTable(arr, elem, outFile);
                lseek(inFile, 0, 0);
                while((num=read(inFile, buf, RW_MAX)) > 0) {
                    encode(buf, num, outFile, indexArr);  
                }
            }
            if(close(inFile)){
                perror("close");
                exit(47);
            }
            if(close(outFile)){
                perror("close");
                exit(47);
            }
        }
    }
    for(num = 0; num < elem; num++) {
        free(arr[num]);
    }
    free(buf);
    return 0; 
}