]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
changed campaign level descriptions
[divverent/nexuiz.git] / data / qcsrc / common / util.qc
1
2 string wordwrap(string s, float l)
3 {
4         local string t, c;
5         local float lleft, i, j, wlen;
6
7         s = strzone(s);
8         t = "";
9         lleft = l;
10         for (i = 0;i < strlen(s);i++)
11         {
12                 if (substring(s, i, 2) == "\\n")
13                 {
14                         t = strcat(t, "\n");
15                         lleft = l;
16                         i++;
17                 }
18                 else if (substring(s, i, 1) == " ")
19                 {
20                         if (lleft > 0)
21                         {
22                                 t = strcat(t, " ");
23                                 lleft = lleft - 1;
24                         }
25                 }
26                 else
27                 {
28                         for (j = i;j < strlen(s);j++)
29                         {
30                                 c = substring(s, j, 1);
31                                 if (c == " ")
32                                         break;
33                                 if (c == "\\")
34                                         break;
35                                 // we need to keep this tempstring alive even if substring is
36                                 // called repeatedly, so call strcat even though we're not
37                                 // doing anything
38                                 t = strcat(t);
39                         }
40                         wlen = j - i;
41                         if (lleft < wlen)
42                         {
43                                 t = strcat(t, "\n");
44                                 lleft = l;
45                         }
46                         t = strcat(t, substring(s, i, wlen));
47                         lleft = lleft - wlen;
48                         i = j - 1;
49                 }
50         }
51         strunzone(s);
52         return t;
53
54 /*
55         string t;
56         string word;
57
58         float lleft;
59         float i;
60
61         float startidx;
62
63         startidx = 0;
64
65         t = "";
66         word = "";
67         for (i = 0;i < strlen(s);i++)
68         {
69                 c = substring(s, i, 1);
70                 forceflush = false;
71                 if (c == " ")
72                         dowhat = 0;
73                 else if (c == "\\" && substring(s, i + 1, 1) == "n")
74                         dowhat = 1;
75                 else
76                 {
77                         dowhat = 2;
78                         word = strcat(word, c);
79                 }
80
81                 if (dowhat < 2)
82                 {
83                         // a space may add some whitespace to the output, and flushes the word buffer
84                         if (word != "")
85                         {
86                                 if (lleft < strlen(word) + 1)
87                                 {
88                                         // add a newline
89                                         t = strcat(t, "\n");
90                                         lleft = l;
91                                 }
92                                 else
93                                 {
94                                         // otherwise just add a space if there's already text in
95                                         // this line
96                                         if (lleft != l)
97                                                 t = strcat(t, " ");
98                                 }
99                                 t = strcat(t, word);
100                                 word = "";
101                         }
102                         if (dowhat == 0)
103                         {
104                                 // if this is a double space, add the space
105                                 if (lleft)
106                                         t = strcat(t, " ");
107                         }
108                         else if (dowhat == 1)
109                         {
110                                 t = strcat(t, "\n");
111                                 lleft = l;
112                         }
113                 }
114                 // we need to keep these tempstrings alive even if substring is
115                 // called repeatedly, so call strcat even though we're not doing
116                 // anything
117                 t = strcat(t);
118                 word = strcat(word);
119         }
120         return t;
121 */
122
123 /*
124         string t;
125         string word;
126
127         float lleft;
128         float i;
129
130         float startidx;
131
132         startidx = 0;
133
134         t = "";
135
136         lleft = l;
137         for(i = 0; i <= strlen(s); ++i)
138         {
139                 if(i != strlen(s) && substring(s, i, 1) != " ")
140                 {
141                         // we need to keep this tempstring alive even if substring is
142                         // called repeatedly, so call strcat even though we're not doing
143                         // anything
144                         t = strcat(t);
145                         continue;
146                 }
147
148                 word = substring(s, startidx, i - startidx);
149                 startidx = i + 1;
150
151                 if(word == "+++")
152                 {
153                         t = strcat(t, "\n\n");
154                         lleft = l;
155                 }
156                 else if(!l || (strlen(word) < lleft))
157                 {
158                         if(lleft != l)
159                         {
160                                 t = strcat(t, " ");
161                                 lleft = lleft - 1;
162                         }
163                         t = strcat(t, word);
164                         lleft = lleft - strlen(word);
165                 }
166                 else
167                 {
168                         t = strcat(t, "\n", word);
169                         lleft = l - strlen(word);
170                 }
171         }
172         return t;
173 */
174 }
175