Bringing ChocolateCaste-0.7 into the main branch.
[AROS-Contrib.git] / rexx / lstring / justify.c
blobaf2a70fdea18a8df17e29ee2aad5bf7bca58545a
1 /*
2 * $Header$
3 * $Log$
4 * Revision 1.1 2001/04/04 05:43:37 wang
5 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
7 * Revision 1.1 1998/07/02 17:18:00 bnv
8 * Initial Version
12 #include <lstring.h>
14 /* ----------------- Ljustify ------------------ */
15 void
16 Ljustify( const PLstr to, const PLstr from, long length, char pad )
18 int spaces, ins, extra;
19 int p,lp,i;
20 float r,rstep;
21 char padstr[2];
22 Lstr tmp,sub,space;
24 L2STR(from);
25 LZEROSTR(*to);
26 if (length<=0) return;
28 LINITSTR(tmp);
29 LINITSTR(sub);
30 LINITSTR(space);
32 Lspace(&tmp,from,1,' ');
33 if (LLEN(tmp)==0) { /* Null string */
34 Lstrset(to,length,pad);
35 goto fin;
38 if (LLEN(tmp) > length) {
39 LLEN(tmp) = length;
40 Lstrcpy(to,&tmp);
41 if (pad != ' ')
42 for (p=0; p<length; p++)
43 if (LSTR(*to)[p]==' ')
44 LSTR(*to)[p] = pad;
45 goto fin;
46 /****** If we don't to destroy the words
47 if (LSTR(tmp)[length] != ' ') {
48 for (p=length-1; p>0; p--)
49 if (LSTR(tmp)[p] == ' ') break;
50 if (!p) goto fin;
51 LLEN(tmp) = p;
53 ******/
56 for (p=spaces=0; p<LLEN(tmp); p++) /* count spaces */
57 if (LSTR(tmp)[p] == ' ') spaces++;
59 if (!spaces) { /* Ooops seulement un mot */
60 Lleft(to,&tmp,length,pad);
61 goto fin;
63 padstr[0] = pad;
64 padstr[1] = 0;
66 ins = length - (LLEN(tmp) - spaces);
67 extra = ins%spaces;
68 rstep = (float)(spaces+1)/(float)(extra+1);
69 ins /= spaces;
71 Lstrset(&space,ins,pad);
73 for (r=rstep,p=0,i=1;;i++) {
74 lp = p;
75 LSKIPWORD(tmp,p);
76 _Lsubstr(&sub,&tmp,lp+1,p-lp);
77 Lstrcat(to,&sub);
78 if (p>=LLEN(tmp)) break;
79 Lstrcat(to,&space);
80 if (extra && (int)r<=i) {
81 Lcat(to,padstr);
82 extra--;
83 r += rstep;
85 LSKIPBLANKS(tmp,p);
87 fin:
88 LFREESTR(tmp);
89 LFREESTR(sub);
90 LFREESTR(space);
91 } /* Ljustify */