There is a LARGE number of syntax styles in most (if not all)
programming languages.
For example, one syntax style (my current one):
...
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; //some weird calculation
}
return 0;
}
with your example my funny coding style is:
int main(){for(int i=0;i<50;i++){cout<<"Hello World!\n";j=2*i+1;
//some weird calculation
}return 0;}
It's obtained by the following C program:
/************************ File: cz.c **********************
It has been proved that the number of errors in a C or C++ program
is proportional to the number of lines into the source files.
This program dramaticaly reduce the number of errors,
reducing (up to 99%) the number of lines in your sources.
OBVIOUSLY a source code that is run by this program:
is semantic equivalent to the original
but it is less readable !
Try all forms:
cz cz.c cz_short.c
cz -256 cz.c cz_small.c
cz -30000 -X cz.c cz_atom.c
cz -0 cz.c cz_long.c
to obtain:
cz_short.c ( 200 lines long)
cz_small.c ( 118 lines long)
cz_atom.c ( 27 lines long)
cz_long.c (2746 lines long)
from:
cz.c ( 639 lines long)
Author: Dario Email: (e-mail address removed)
************************************************************//*
Version History:
1.0 Dec 6 1994
Initial version
1.1 Dec 7 1994
1. Correct from:
sscanf(argv
+1, "%ld", &maxOutLen)
to:
sscanf(argv+1, "%d", &maxOutLen)
2. Correct from:
static char * operator[] = {...
to:
static char * operatorString[] = {...
3. Operators added to operatorString:
%%%% ... <> <% %> <: :> %%
4. Introduced the function isIdientifierLetter
5. Changed from:
*n = *n+1;
to:
(*n)++;
6. Correct from
#if defined(__STDC__) || defined(__cpluscplus)
to:
#if defined(__STDC__) || defined(__cplusplus)
7. Many casts added to the result of memory allocators
8. Correct from:
static forceNewLine(void)
to:
static void forceNewLine(void)
1.2 Dec 12 1994
1. Recognize as preprocessor line a line starting with %%
2. Now may be also compiled by pre-ANSI-C compiler
1.3 Aug 04 1997
1. Third paramter of strncmp is a size_t
2. Many int casted to char
3. Changed the default for maxOutLen from 80 to 70
*/
#if defined(__STDC__) || defined(__cplusplus)
#define VOID void
#define FUNC_1(t1,v1) (t1 v1)
#define FUNC_2(t1,v1,t2,v2) (t1 v1, t2 v2)
#define FUNC_3(t1,v1,t2,v2,t3,v3) (t1 v1, t2 v2, t3 v3)
#define TRIG_BACK_SLASH "?" "?" "/" /* trigraph for \ */
#define TRIG_POUND_SIGN "?" "?" "=" /* trigraph for # */
#else
#define VOID
#define FUNC_1(t1,v1) (v1) t1 v1;
#define FUNC_2(t1,v1,t2,v2) (v1, v2) t1 v1; t2 v2;
#define FUNC_3(t1,v1,t2,v2,t3,v3) (v1, v2, v3) t1 v1; t2 v2; t3 v3;
#define TRIG_BACK_SLASH "??/" /* trigraph for \ */
#define TRIG_POUND_SIGN "??=" /* trigraph for # */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MYstrncmp(a1,a2,a3) strncmp(a1,a2,(size_t)(a3))
static FILE*inF;static FILE*outF;static char*inLine=0;static char*
lastToken=0;static char*prevToken=0;static int outCounter=0;static
int inComment=0;static int removeComment=0;static int maxOutLen=70-1
;static char*operatorString[]={/* sorted by lenght! *//* 4 */"%%%%",
/* 3 */"...","->*","<<=",">>=",
#if defined(__STDC__) || defined(__cplusplus)
"?""?""(",/* trigraph for "[" */"?""?"")",/* trigraph for "]" */"?"
"?""!",/* trigraph for "|" */"?""?""'",/* trigraph for "^" */"?""?"
"<",/* trigraph for "{" */"?""?"">",/* trigraph for "}" */"?""?""-",
/* trigraph for "~" */
#else
"??(",/* trigraph for "[" */"??)",/* trigraph for "]" */"??!",
/* trigraph for "|" */"??'",/* trigraph for "^" */"??<",
/* trigraph for "{" */"??>",/* trigraph for "}" */"??-",
/* trigraph for "~" */
#endif
/* 2 */"->","++","--",".*","<<",">>","<=",">=","==","!=","&&","||",
"*=","/=","%=","+=","-=","&=","^=","|=","::","##","<>","<%","%>",
"<:",":>","%%",/* 1 */"!","%","^","&","*","(",")","-","+","=","{",
"}","|","~","[","]",/* "\\", */";",/* "\'", */":",/* "\"", */"<",">"
,"?",",",".","/","#",/* 0 */0,};static void clean(VOID){if(inLine){
free((void*)inLine);inLine=0;}if(prevToken){free((void*)prevToken);
prevToken=0;}if(lastToken){free((void*)lastToken);lastToken=0;}if(
inF&&inF!=stdin){(void)fclose(inF);inF=0;}if(outF&&outF!=stdout){(
void)fclose(outF);outF=0;}}static int getOpLen FUNC_1(char*,p){int i
;unsigned int len;for(i=0;operatorString;i++){len=strlen(
operatorString);if(strlen(p)>=len&&!MYstrncmp(p,operatorString
,len))return len;}return 0;}static void byebye(VOID){clean();exit(-1
);}static void outOfMemory(VOID){(void)fprintf(stderr,"%s\n",
"Out of memory!");byebye();}static int endLine FUNC_1(char*,line){
int len=strlen(line);if(len==0||line[len-1]!='\n')return 0;if(len>=2
&&line[len-2]=='\\'){line[len-2]=(char)0;return 0;}if(len>=4&&!
MYstrncmp(line+len-4,TRIG_BACK_SLASH,3)){line[len-4]=(char)0;return 0
;}return 1;}static void addCharToLine FUNC_2(char,c,char**,line){int
len=strlen(*line);*line=(char*)realloc((void*)*line,(size_t)(len+1+1
));if(!*line)outOfMemory();(*line)[len+0]=c;(*line)[len+1]=(char)0;}
static void readLine(VOID){int c;int len;if(inLine){free((void*)
inLine);inLine=0;}inLine=(char*)malloc((size_t)1);if(!inLine)
outOfMemory();*inLine=(char)0;while(!endLine(inLine)){c=fgetc(inF);
if(c==EOF)break;addCharToLine((char)c,&inLine);}len=strlen(inLine);
if(len)inLine[len-1]=(char)0;}static void forceNewLine(VOID){if(
outCounter){(void)fprintf(outF,"\n");outCounter=0;}}static int
isIdentifierLetter FUNC_1(char,c){return c!=0&&(isalnum((int)c)||c==
'_'||c=='$');}static int needSpace(VOID){int result;char*p;if(!
prevToken||!outCounter)return 0;if(isIdentifierLetter(prevToken[
strlen(prevToken)-1])&&isIdentifierLetter(lastToken[0]))return 1;p=(
char*)malloc((size_t)(strlen(prevToken)+strlen(lastToken)+1));if(!p)
outOfMemory();(void)sprintf(p,"%s%s",prevToken,lastToken);result=(
getOpLen(prevToken)!=getOpLen(p));free((void*)p);return result;}
static void putLastToken(VOID){int len;int isComment;int need;len=
strlen(lastToken);isComment=inComment||((len>=2)&&(!MYstrncmp(
lastToken,"//",2)||!MYstrncmp(lastToken,"/*",2)||!MYstrncmp(
lastToken+len-2,"*/",2)));if(isComment&&removeComment)return;if(
outCounter+1+len>maxOutLen)forceNewLine();need=needSpace();(void)
fprintf(outF,"%s%s",need?" ":"",lastToken);outCounter+=need+len;if(
inComment||(isComment&&len>=2&&!MYstrncmp(lastToken,"//",2)))
forceNewLine();}static void preProcessorLine FUNC_1(char*,p){
forceNewLine();(void)fprintf(outF,"%s",p);outCounter+=strlen(p);
forceNewLine();}static void setToken FUNC_3(char*,p,int,s,int,e){
/*
The token is between index s (includeed)
and e (excluded) of p string.
*/int len;int i;if(prevToken){free((void*)prevToken);
/* prevToken = 0; */}prevToken=lastToken;/* lastToken = 0; */
len=e-s;lastToken=(char*)malloc((size_t)(len+1));if(!lastToken)
outOfMemory();for(i=0;i<len;i++)lastToken=p[s+i];lastToken[len]=(
char)0;}static int get1Comment FUNC_2(char*,p,int*,n){int s=*n;while
(p[*n])(*n)++;setToken(p,s,*n);return 1;}static int getNComment
FUNC_2(char*,p,int*,n){int s=*n;(*n)+=2;inComment=1;while(p[*n])if(p
[*n]=='*'&&p[*n+1]=='/'){(*n)+=2;inComment=0;break;}else(*n)++;
setToken(p,s,*n);return 1;}static int getString FUNC_2(char*,p,int*,
n){int start;int s=*n;if(p[*n]=='L')(*n)++;start=p[*n];(*n)++;while(
p[*n]&&p[*n]!=start){if(strlen(p+*n)>=2&&p[*n]=='\\')(*n)+=2;else if
(strlen(p+*n)>=4&&!MYstrncmp(p+*n,TRIG_BACK_SLASH,3))(*n)+=4;else(*n
)++;}if(p[*n]==start)(*n)++;setToken(p,s,*n);return 1;}static int
getNumber FUNC_2(char*,p,int*,n){/*
This functions recognize a superset of valid C or C++ numbers,
but this is not a problem in this program.
*/int s=*n;while(isIdentifierLetter(p[*n])||p[*n]=='+'||p[*n]=='-'
||p[*n]=='.')(*n)++;setToken(p,s,*n);return 1;}static int
getIdentifier FUNC_2(char*,p,int*,n){int s=*n;while(
isIdentifierLetter(p[*n]))(*n)++;setToken(p,s,*n);return 1;}static
int getOperator FUNC_3(char*,p,int*,n,int,len){int s=*n;(*n)+=len;
setToken(p,s,*n);return 1;}static int getToken FUNC_2(char*,p,int*,n
){int len;while(p[*n]){if(p[*n]=='/'&&p[*n+1]=='/')return get1Comment
(p,n);if(p[*n]=='/'&&p[*n+1]=='*')return getNComment(p,n);if(isdigit
((int)p[*n])||(p[*n]=='.'&&isdigit((int)p[*n+1])))return getNumber(p
,n);if(isIdentifierLetter(p[*n]))return getIdentifier(p,n);if(p[*n]
=='"'||(p[*n]=='L'&&p[*n+1]=='"')||p[*n]=='\''||(p[*n]=='L'&&p[*n+1]
=='\''))return getString(p,n);if(!isspace((int)p[*n])){len=getOpLen(
p+*n);return getOperator(p,n,len?len:1);}(*n)++;}return 0;}static
void cLine FUNC_1(char*,p){int n;char*e;if(inComment){e=strstr(p,
"*/");if(e){e+=2;setToken(p,0,e-p);inComment=0;}else setToken(p,0,(
int)strlen(p));putLastToken();}else e=p;if(e){n=e-p;while(getToken(p
,&n))putLastToken();}}static void analizeLine(VOID){char*p=inLine;
while(isspace((int)*p))p++;if(!inComment&&(*p=='#'||(p[0]=='%'&&p[1]
=='%')||(strlen(p)>=3&&!MYstrncmp(p,TRIG_POUND_SIGN,3))))
preProcessorLine(inLine);else cLine(inLine);}static void engine(VOID
){while(!feof(inF)){readLine();analizeLine();}forceNewLine();}static
void usage FUNC_1(char*,argv0){(void)fprintf(stderr,
"Usage: %s [options] [infile [outfile]]\n",argv0);(void)fprintf(
stderr,"where each argument may be:\n");(void)fprintf(stderr,
" -n compact C or C++ code in lines n characters long
(default 70)\n"
);(void)fprintf(stderr," -X remove comments\n");(void)
fprintf(stderr," -C preserve comments (default)\n");
(void)fprintf(stderr,
" infile C or C++ source file (default standard input)\n");
(void)fprintf(stderr,
" outfile C or C++ output file (default standard output)\n")
;byebye();}static void runArguments FUNC_2(int,argc,char**,argv){
FILE*f;int i;if(argc<=1)usage(argv[0]);for(i=1;i<argc;i++)if(inF==
stdin&&!strcmp(argv,"-X"))removeComment=1;else if(inF==stdin&&!
strcmp(argv,"-C"))removeComment=0;else if(inF==stdin&&argv[0]
=='-'){if(argv[1]==0)maxOutLen=70;else if(sscanf(argv+1,"%d",&
maxOutLen)!=1||maxOutLen<0)usage(argv[0]);maxOutLen--;if(maxOutLen<0
)maxOutLen=1;}else{if(inF!=stdin&&outF!=stdout)usage(argv[0]);f=
fopen(argv,(inF==stdin)?"r":"w");if(!f){(void)fprintf(stderr,
"Unable to open file %s\n",argv);byebye();}if(inF==stdin)inF=f;
else outF=f;}}int main FUNC_2(int,argc,char**,argv){inF=stdin;outF=
stdout;runArguments(argc,argv);engine();clean();return 0;}