File size: 7,747 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

class CSHEntry
{
  CDocEntry *next,*last;
};

class CSHSection
{
  CDocEntry *next,*last;
};

I64 SHGetChar(CDoc *doc,CDocEntry **_doc_e,U8 **_st)
{
  I64 ch;
  U8 *st=*_st;
  CDocEntry *doc_e=*_doc_e;
  while (TRUE) {
    if (st) {
      if (ch=*st) {
        *_st=++st;
        return ch;
      } else
        st--;
    }
    if (doc_e==doc) {
      *_st=NULL;
      return 0;
    } else {
      *_doc_e=doc_e=doc_e->next;
      if (doc_e->type_u8==DOCT_TEXT)
        st=doc_e->tag;
    }
  }
}

U8 *SHSkipDataType(CDoc *doc,CDocEntry **_doc_e,U8 *st)
{
  I64 ch;
  CDocEntry *last_doc_e=*_doc_e;
  U8 *last_st=st;
  if (!(ch=SHGetChar(doc,_doc_e,&st)))
    return NULL;
  while (ch && Bt(char_bmp_white_space,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  if (ch=='#')
    return st;
  while (ch && Bt(char_bmp_alpha_numeric,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  while (ch && Bt(char_bmp_white_space,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  while (ch=='*') {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  *_doc_e=last_doc_e; st=last_st;
  return st;
}

U8 *SHSkipLabel(CDoc *doc,CDocEntry **_doc_e,U8 *st)
{
  I64 ch;
  CDocEntry *last_doc_e=*_doc_e;
  U8 *last_st=st;
  if (!(ch=SHGetChar(doc,_doc_e,&st)))
    return NULL;
  while (ch && Bt(char_bmp_white_space,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  while (ch && Bt(char_bmp_alpha_numeric,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  while (ch && Bt(char_bmp_white_space,ch)) {
    last_doc_e=*_doc_e; last_st=st;
    ch=SHGetChar(doc,_doc_e,&st);
  }
  *_doc_e=last_doc_e; st=last_st;
  return st;
}

U8 *SHNewEntry(CDoc *doc,CDocEntry **_doc_e,I64 *_type=NULL)
{
  U8 *st;
  if ((*_doc_e)->type_u8==DOCT_TEXT && (st=(*_doc_e)->tag)) {
    if (!StrNCmp("#help_index",st,11)) {
      if (_type) *_type=0;
      return st+11;
    } else if (!StrNCmp("#help_file",st,10)) {
      if (_type) *_type=1;
      return st+10;//'#' makes it first alphabetically.
    } else if (!StrNCmp("import",st,6)) {
      if (_type) *_type=2;
      return st+6;
    } else if (!StrNCmp("public import",st,13)) {
      if (_type) *_type=2;
      return st+13;
    } else if (!StrNCmp("_import",st,7)) {
      if (_type) *_type=2;
      return SHSkipLabel(doc,_doc_e,st+7);
    } else if (!StrNCmp("public _import",st,14)) {
      if (_type) *_type=2;
      return SHSkipLabel(doc,_doc_e,st+14);
    } else if (!StrNCmp("_intern",st,7)) {
      if (_type) *_type=3;
      return SHSkipLabel(doc,_doc_e,st+7);
    } else if (!StrNCmp("public _intern",st,14)) {
      if (_type) *_type=3;
      return SHSkipLabel(doc,_doc_e,st+14);
    } else if (!StrNCmp("extern",st,6)) {
      if (_type) *_type=4;
      return st+6;
    } else if (!StrNCmp("public extern",st,13)) {
      if (_type) *_type=4;
      return st+13;
    } else if (!StrNCmp("_extern",st,7)) {
      if (_type) *_type=4;
      return SHSkipLabel(doc,_doc_e,st+7);
    } else if (!StrNCmp("public _extern",st,14)) {
      if (_type) *_type=4;
      return SHSkipLabel(doc,_doc_e,st+14);
    }
  }
  if (_type) *_type=-1;
  return NULL;
}

I64 SHCompareEntry(CSHEntry *e1,CSHEntry *e2)
{
  U8 *st1,*st2;
  I64 t1,t2;
  CDocEntry *doc_e1,*doc_e2;
  if (e1 && e2) {
    doc_e1=e1->next;
    doc_e2=e2->next;
    if (doc_e1!=e1 && doc_e2!=e2) {
      if (doc_e1->type_u8==DOCT_TEXT && doc_e2->type_u8==DOCT_TEXT) {
        st1=SHNewEntry(e1,&doc_e1,&t1);
        st2=SHNewEntry(e2,&doc_e2,&t2);
        if (t1!=t2)
          return t1-t2;
        else {
          if (st1&&st2) {
            st1=SHSkipDataType(e1,&doc_e1,st1);
            st2=SHSkipDataType(e2,&doc_e2,st2);
            if (st1&&st2)
              return StrCmp(st1,st2);
            else
              return SignI64(st1-st2);
          } else
            return SignI64(st1-st2);
        }
      } else
        return SignI64(e1->next-e2->next);
    }
  }
  return SignI64(e1-e2);
}

U0 SHSortSection(CSHSection *tmps)
{
  I64 i=0,cnt;
  U8 *st;
  CSHEntry **a;
  CDocEntry *doc_e,*doc_e1,*doc_e2,*sec_start=tmps->next;

  while (sec_start!=tmps && sec_start->type_u8==DOCT_TEXT) {
    st=sec_start->tag;
    if (*st) {
      st+=StrLen(st)-1;
      if (*st=='\\')
        sec_start=sec_start->next;
      else
        break;
    } else
      break;
  }
  sec_start=sec_start->next;

  cnt=0;
  doc_e=sec_start;
  while (doc_e!=tmps) {
    doc_e2=doc_e;
    if (SHNewEntry(tmps,&doc_e2))
      cnt++;
    doc_e=doc_e->next;
  }
  cnt++;

  a=MAlloc(sizeof(CSHEntry *)*cnt);
  for (i=0;i<cnt;i++) {
    a[i]=MAlloc(sizeof(CSHEntry));
    QueInit(a[i]);
  }

  i=0;
  doc_e=sec_start;
  while (doc_e!=tmps) {
    doc_e1=doc_e->next;
    QueRem(doc_e);
    doc_e2=doc_e;
    if (SHNewEntry(tmps,&doc_e2))
      i++;
    QueIns(doc_e,a[i]->last);
    doc_e=doc_e1;
  }

  QSortI64(a,cnt,&SHCompareEntry);

  for (i=0;i<cnt;i++) {
    doc_e=a[i]->next;
    while (doc_e!=a[i]) {
      doc_e1=doc_e->next;
      QueRem(doc_e);
      QueIns(doc_e,tmps->last);
      doc_e=doc_e1;
    }
    Free(a[i]);
  }
  Free(a);
}

U8 *SHNewSection(CDocEntry *doc_e)
{
  U8 *st;
  if (doc_e->type_u8==DOCT_TEXT && (st=doc_e->tag)) {
    if (!StrNCmp(st,"#help_index",11))
      return st+11;
  }
  return NULL;
}

I64 SHCompareSection(CSHSection *e1,CSHSection *e2)
{
  I64 i1,i2;
  U8 *st1,*st2;
  if (e1 && e2 && e1->next!=e1 && e2->next!=e2) {
    if (e1->next->type_u8==DOCT_TEXT && e2->next->type_u8==DOCT_TEXT) {
      st1=SHNewSection(e1->next);
      st2=SHNewSection(e2->next);
      if (st1&&st2) {
        if (StrMatch("\"\"",st1))
          i1=1;
        else
          i1=0;
        if (StrMatch("\"\"",st2))
          i2=1;
        else
          i2=0;
        if (i1!=i2)
          return i1-i2;
        else
          return StrCmp(st1,st2);
      } else
        return SignI64(st1-st2);
    } else
      return SignI64(e1->next-e2->next);
  } else
    return SignI64(e1-e2);
}

public U0 SortHeader(U8 *name)
{
  CDoc *doc;
  I64 i=0,cnt;
  CSHSection **a;
  CDocEntry *doc_e,*doc_e1;

  if (!name || !*name || !FileFind(name))
    return;

  doc=DocRead(name,DOCF_PLAIN_TEXT_TABS|DOCF_NO_CURSOR);

  cnt=1;
  doc_e=doc->head.next;
  while (doc_e!=doc) {
    if (SHNewSection(doc_e))
      cnt++;
    doc_e=doc_e->next;
  }

  a=MAlloc(sizeof(CSHSection *)*cnt);
  for (i=0;i<cnt;i++) {
    a[i]=MAlloc(sizeof(CSHSection));
    QueInit(a[i]);
  }

  i=0;
  doc_e=doc->head.next;
  while (doc_e!=doc) {
    doc_e1=doc_e->next;
    if (doc_e->type_u8==DOCT_TEXT && doc_e->tag && *doc_e->tag) {
      QueRem(doc_e);
      if (SHNewSection(doc_e))
        i++;
      QueIns(doc_e,a[i]->last);
    } else
      DocEntryDel(doc,doc_e);
    doc_e=doc_e1;
  }

  QSortI64(a,cnt,&SHCompareSection);

  for (i=0;i<cnt;i++) {
    if (a[i]->next!=a[i]) {
      SHSortSection(a[i]);
      doc_e=a[i]->next;
      while (doc_e!=a[i]) {
        doc_e1=doc_e->next;
        QueRem(doc_e);
        QueIns(doc_e,doc->head.last);
        if (doc_e->type_u8==DOCT_TEXT && doc_e->tag) {
          doc->cur_entry=doc_e;
          doc->cur_col=StrLen(doc_e->tag);
          DocPrint(doc,"\n");
        }
        doc_e=doc_e1;
      }
      if (i+1<cnt && a[i+1]->next!=a[i+1])
        DocPrint(doc,"\n");
    }
    Free(a[i]);
  }
  Free(a);
  DocWrite(doc);
  DocDel(doc);
}

public U0 SortHeaders()
{
  "Sorting Headers...\n";
  SortHeader("/Kernel/KernelB.HH.Z");
  SortHeader("/Kernel/KernelC.HH.Z");
  SortHeader("/Kernel/KExts.HC.Z");
  SortHeader("/Compiler/CExts.HC.Z");
  SortHeader("/Compiler/CompilerB.HH.Z");
  SortHeader("/Adam/AExts.HC.Z");
}

SortHeaders;