1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
| #include <stdio.h>
#include <stdlib.h>
#include <strings.h>
//#include "fio_utl.c"
//#include "str_code.c"
int is_word_char(char c)
{
int ci;
unsigned char cu;
cu=(unsigned char)c;
if ((cu>=65)&&(cu<=90)) return 1;
if ((cu>=97)&&(cu<=122)) return 1;
if ((cu>=192)&&(cu<=255)) return 1;
if ((cu>=140)&&(cu<=144)) return 1;//Rom
switch(cu) //Roms chars
{
case 128:
case 129:
case 131:
case 138:
case 154:
case 168://e-
case 184:
return 1;
}
return 0;
}
char endL[]="\r\n";
char out_str[256];
void print_out(char *buf,int startIx,int count){
memset(out_str,0,sizeof(out_str));
memcpy(out_str,&buf[startIx],count);
printf("\n%s",out_str);
}
void format_80(char *fname)
{
char buf[260];
char tmpi[10];
char c;
char *tmp,*tmp2;
int i,j,firstWordIx,le,readed;
int fsz,is_word,lastI,co;
FILE *fi,*fo;
fsz=file_size_fn(fname);
fi=fopen(fname,"rb");
if (fi==NULL)
{
printf("\n Err can not acces file:%s",fname);
return;
}
tmp=(char*)malloc(fsz);
i=0;
readed=fread(tmp,1,fsz,fi);
fclose(fi);
if (readed<=0)
{
free(tmp);
return;
}
sprintf(tmpi,"_80.txt");
strcpy(buf,fname);
strcat(buf,tmpi);
fo=fopen(buf,"wb");
if (fo==NULL)
{
printf("\n Err can not acces file:%s",buf);;
free(tmp);
return;
}
tmp2=(char*)malloc(81);
for (i=0; i<80; i++) tmp2[i]=' ';
j=-1;//tek kol-vo simvolov
firstWordIx=-1;//index pervogo char v slove
lastI=0;//Poslednii simvol (kotorii ne zapisan)
for (i=0; i<fsz; i++)
{
j++;
c=tmp[i];
is_word=is_word_char(c);
if (is_word)
{
if (firstWordIx<0)
{
firstWordIx=i;//
}
else
{
}
}
else firstWordIx=-1;
if ((c==10)&&(j<80))
{
fwrite(&tmp[lastI],1,1+i-lastI,fo);
lastI=i+1;
j=0;
firstWordIx=-1;
continue;
}
if (j>=80)
{
if (!is_word)
{
co=i-lastI;
//printf("\nObicinii lastI:%i wt:%i",lastI,co);
fwrite(&tmp[lastI],1,co,fo);
fwrite(endL,1,2,fo);
}
else
{
if (firstWordIx>=0)
{
co=firstWordIx-lastI;
fwrite(&tmp[lastI],1,co,fo);
//printf("\nlastI:%i wt:%i",lastI,co);
//print_out(tmp,lastI,firstWordIx-lastI);
co=80-(firstWordIx-lastI);
fwrite(tmp2,1,co,fo);
fwrite(endL,1,2,fo);
co=i-firstWordIx+1;
fwrite(&tmp[firstWordIx],1,co,fo);
//printf("\nfirstWordIx:%i wt:%i",firstWordIx,co);
//print_out(tmp,firstWordIx,co);
j=1+i-firstWordIx;
lastI=i+1;
firstWordIx=-1;
continue;
}
}
lastI=i+1;
j=0;
firstWordIx=-1;
}
}
fclose(fo);
free(tmp);
free(tmp2);
}
static void usage()
{
printf("format_80 infile.txt\n");
}
int main(int argc,char **argv)
{
char *fn;
char fout[260];
char tmpi[20];
int size,le;
if (argc==2)
{
format_80(argv[1]);
//printf("\nComplete!");
}
usage();
return 0;
} |