SSブログ

entab.cpp [C++]

// entab
#include <iostream>
using namespace std;

void entab(FILE *src, FILE *dst, int width){
    int ch;
    int count = 0;
    int ntab = 0;
    int pos = 1;
   
    for( ;(ch = fgetc(src)) != EOF; pos++){
        if(ch == ' '){
            if(pos % width != 0){
                count++;
            }
            else{
                count = 0;
                ntab++;
            }
        }
        else{
            for( ; ntab > 0; --ntab){
                fputc('\t', dst);
            }
            if(ch == '\t'){
                count = 0;
            }
            else{
                for( ; count > 0; count--){
                    fputc(' ', dst);
                }
            }
            fputc(ch, dst);
            if(ch == '\n'){
                pos = 0;
            }
            else if(ch == '\t'){
                pos += width - (pos - 1) % width - 1;
            }
        }
    }
}

int main(int argc, char *argv[]){
    int width = 8;
    FILE *fp;
   
    if(argc < 2){
        entab(stdin, stdout, width);    //標準入力から標準出力へ
    }
    else{
        while(--argc > 0){
            if(**(++argv) == '-'){
                if(*++(*argv) == 't'){
                    width = atoi(++*argv);
                }
                else{
                    fputs("パラメータが不正です。\n", stderr);
                    return(1);
                }
            }
            else if((fp = fopen(*argv, "r")) == NULL){
                fprintf(stderr, "ファイル%sがオープンできません。\n", *argv);
                return(1);
            }
            else{
                entab(fp, stdout, width);   //ファイルから標準出力へ
                fclose(fp);
            }
        }
    }
    return(0);
}
─────

CPP entab0.jpg

CPP entab1.jpg


nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

Facebook コメント

トラックバック 0

detab.cpphdump.cpp ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。