Skip to content

Instantly share code, notes, and snippets.

@atr000
Created October 12, 2010 02:28
Show Gist options
  • Save atr000/621572 to your computer and use it in GitHub Desktop.
Save atr000/621572 to your computer and use it in GitHub Desktop.
/*
Copyright 2004,2005,2006 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl.txt
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VER "0.1"
#define BUFFSZ 16384
void std_err(void);
int main(int argc, char *argv[]) {
FILE *fd,
*fdout;
unsigned char *buff,
*ptr;
unsigned int numspc,
pos,
tmp;
int err;
setbuf(stdout, NULL);
fputs("\n"
"Formatted tabs to spaces "VER"\n"
"by Luigi Auriemma\n"
"e-mail: [email protected]\n"
"web: aluigi.org\n"
"\n", stdout);
if(argc < 3) {
printf("\nUsage: %s <num_of_spaces*> <input_file> [output_file(stdout)]\n"
"\n"
"* a normal TAB is composed by 8 \"spaces\", however be free to choose\n"
" what size you want. This program doesn't make a simple substitution\n"
" but a real conversion from tab to spaces\n"
"\n", argv[0]);
exit(1);
}
if(argc > 3) {
fdout = fopen(argv[3], "wb");
if(!fdout) std_err();
} else fdout = stdout;
numspc = atoi(argv[1]);
fd = fopen(argv[2], "rb");
if(!fd) std_err();
buff = malloc(BUFFSZ + 1);
if(!buff) std_err();
pos = 0;
while(!feof(fd)) {
err = fread(buff, 1, BUFFSZ, fd);
if(!err) break;
ptr = buff;
for(; err > 0; err--, ptr++) {
if(*ptr == 0x9) {
tmp = numspc - (pos % numspc); // optimization impossibile
pos += tmp; // with custom values
for(; tmp > 0; tmp--) fputc(0x20, fdout);
} else {
fputc(*ptr, fdout);
pos++;
if(*ptr == 0xa) pos = 0;
}
}
}
fclose(fd);
if(fdout != stdout) {
fputs("\nDone\n", stdout);
fclose(fdout);
}
return(0);
}
void std_err(void) {
perror("\nError");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment