summaryrefslogtreecommitdiff
path: root/setedit/setedit/pathlist.cc
blob: 51d9da12ac18b38e9becb81f421d7a583bccb72c (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* Copyright (C) 2001-2004 by Salvador E. Tropea (SET),
   see copyrigh file for details */
/**[txh]********************************************************************

  Module: Path Lists
  Comments:
  Handles the list of directories to search for when the user press ^+Enter
over a file name. It is usually the path for include files.@*
  It is handled as some kind of string collection (TPathList), it needs
more memory and disk space but is much more easy to setup. [Note: Here I'm
comparing it with the simple string used in RHIDE]
  
***************************************************************************/

#include <ceditint.h>
#define Uses_string
#define Uses_limits
#define Uses_stdlib
#define Uses_stdio
#define Uses_unistd
// TV
#define Uses_TCollection
#define Uses_TStreamableClass
#define Uses_fpstream
#define Uses_MsgBox
#define Uses_TScreen
// EasyDiag
#define Uses_TSHzLabel
#define Uses_TSInputLine
#define Uses_TSButton
#define Uses_AllocLocal
// SetTVUti
#define Uses_TStringable
#define Uses_TDialogAID

#include <easydia1.h>
#include <settvuti.h>
#include <easydiag.h>

#define Uses_SETAppDialogs
#define Uses_SETAppConst
#define Uses_SETAppProject
#include <setapp.h>
#include <rhutils.h>
#include <pathlist.h>
#include <edspecs.h>
#include <fileopen.h>
#include <debug.h> // DBG_AddPathForSources

class TPathList : public TCollection, public TStringable
{
public:
 TPathList() : TCollection(5,2), TStringable() {};
 // TStringable:
 virtual void getText(char *dest, unsigned item, int maxLen);
 virtual unsigned GetCount(void) { return getCount(); };
 TPathList &operator = (const TPathList & pl);

 SetDefStreamMembers(TPathList,TCollection)
};

const char * const TPathList::name="TPathList";
TStreamableClass RPathList(TPathList::name,TPathList::build,__DELTA(TPathList));

SetDefStreamOperators(TPathList);

void *TPathList::readItem(ipstream& is)
{
 return (void *)is.readString();
}

void TPathList::writeItem(void *obj, opstream& os)
{
 os.writeString((const char *)obj);
}

void TPathList::getText(char *dest, unsigned item, int maxLen)
{
 const char *ori=(const char *)at(item);
 strncpy(dest,ori,maxLen);
 dest[maxLen]=EOS;
}

TPathList & TPathList::operator = (const TPathList & pl)
{
 ccIndex i;
 freeAll();
 for (i=0;i<pl.count;i++)
     insert(newStr((char *)pl.items[i]));
 return *this;
}

static TPathList *lists[paliLists]={NULL,NULL}, *lEdited;
const char Version=2;
const int MaxLineLen=PATH_MAX;
static const char *Titles[paliLists]=
{
 __("Path to look for includes"),
 __("Path to look for sources")
};
static const char *tEdited;
static int listChanged;
static unsigned hCtxEdited;

void PathListSave(fpstream& s)
{
 s << Version;
 s << (uchar)paliLists;
 for (int i=0; i<paliLists; i++)
     if (lists[i] && lists[i]->getCount())
        s << (uchar)1 << lists[i];
     else
        s << (uchar)0;
}

void PathListUnLoad()
{
 for (int i=0; i<paliLists; i++)
     destroy0(lists[i]);
}

void PathListLoad(fpstream& s)
{
 uchar aux, version, cant;
 s >> version;
 PathListUnLoad();
 if (version>=2)
    s >> cant;
 else
    cant=1;
 for (int i=0; i<cant; i++)
    {
     s >> aux;
     if (aux)
        s >> lists[i];
    }
}

static
void PathListAddFromPrj(void *p, void *data)
{
 TPathList *l=(TPathList *)data;
 char f[PATH_MAX];
 ProjectGetNameFromItem(p,f,PATH_MAX);
 // Find the "path" part
 char *end=strrchr(f,'/');
 if (end)
   {
    char aux=*end;
    *end=0;
    // Not sorted, so we must do a brut force search
    int i, c=l->getCount(), found=0;
    for (i=0; i<c; i++)
        if (strcmp(f,(char *)l->at(i))==0)
          {
           found=1;
           break;
          }
    if (!found)
      {
       //printf("Adding %s from project\n",f);
       l->insert(newStr(f));
       // It works only when "stopped"
       DBG_AddPathForSources(f);
      }
    *end=aux;
   }
}

/**[txh]********************************************************************

  Description:
  Adds a path for a project item to the @var{which} list. Note that
@var{item} must be a PrjItem pointer.
  
***************************************************************************/

void PathListAddPathFor(void *item, int which)
{
 if (lists[which])
    PathListAddFromPrj(item,lists[which]);
}

/**[txh]********************************************************************

  Description:
  Fill the list of directories with the include directories. We use the
output of cpp or a guess.
  
  Return: !=0 if we got something from cpp
  
***************************************************************************/

int PathListPopulate(int which)
{
 if (which==paliInclude)
   {
    TPathList *IncludeList=new TPathList();
    lists[paliInclude]=IncludeList;
   
    // 1) Try invoking the GNU preprocessor
    char *err=open_stderr_out();
    TScreen::System("cpp -x c++ -v /dev/null");
    close_stderr_out();
    // Check what we got
    FILE *f=fopen(err,"r");
    if (f)
      {
       char resp[PATH_MAX];
       int state=0;
       while (!feof(f) && state!=2)
         {
          fgets(resp,PATH_MAX,f);
          switch (state)
            {
             case 0:
                  if (strncmp(resp,"#include <",10)==0)
                     state=1;
                  break;
             case 1:
                  if (resp[0]!=' ')
                     state=2;
                  else
                    {// Insert the path
                     int l=1;
                     char *s=resp;
                     for (; s[l] && CLY_IsntEOL(s[l]); l++);
                     s[l]=0;
                     char *path=new char[l];
                     memcpy(path,s+1,l);
                     IncludeList->insert(path);
                     //printf("Agregando <%s>\n",path);
                    }
                  break;
            }
         }
       fclose(f);
      }
    unlink(err);
   
    if (IncludeList->getCount())
       return 1;
   
    char buffer[PATH_MAX];
    #ifdef SECompf_djgpp
    char *djdir=getenv("DJDIR");
    if (!djdir)
       djdir="c:/djgpp";
    strcpy(buffer,djdir);
    strcat(buffer,"/include");
    #else
    strcpy(buffer,"/usr/include");
    #endif
    IncludeList->insert(newStr(buffer));
   }
 else if (which==paliSource)
   {
    TPathList *SourceList=new TPathList();
    lists[paliSource]=SourceList;

    if (IsPrjOpened())
       ProjectApplyToItems(PathListAddFromPrj,SourceList);
   }

 return 0;
}

int PathListGetItem(ccIndex pos, char *buffer, int which)
{
 TPathList *p=lists[which];
 if (!p)
    PathListPopulate(which);
 if (!p || pos>=p->getCount())
    return 0;
 const char *str=(const char *)p->at(pos);
 const char *var=strstr(str,"$(");
 int avail=PATH_MAX-1;
 if (!var)
   {
    int l=min((int)strlen(str),avail);
    memcpy(buffer,str,l);
    buffer[l]=0;
   }
 else
   {// This is some rudimentary $(VARIABLE) expansion
    int offset=0;
    do
      {
       int l=var-str;
       if (offset+l>avail)
          break;
       memcpy(buffer+offset,str,l);
       offset+=l;
       str+=l+2;
       var=strchr(str,')');
       if (var)
         {
          l=var-str;
          AllocLocalStr(v,l+1);
          memcpy(v,str,l);
          v[l]=0;
          const char *vVar=GetVariable(v);
          if (vVar)
            {
             int lVar=strlen(vVar);
             if (offset+lVar>avail)
                break;
             memcpy(buffer+offset,vVar,lVar);
             offset+=lVar;
            }
          str=var+1;
          var=strstr(str,"$(");
          if (!var)
            {
             if (offset+strlen(str)>(unsigned)avail)
                break;
             strcpy(buffer+offset,str);
            }
         }
       else
          buffer[offset]=0;
      }
    while (var);
    //printf("Expandido: %s\n",buffer);
   }
 return 1;
}

static
char *EditItem(const char *s)
{
 char b[MaxLineLen];
 TSViewCol *col=new TSViewCol(tEdited);

 TSHzLabel *lineLabel=new TSHzLabel(__("Directory"),
                                    new TSInputLine(MaxLineLen,60));

 col->insert(xTSCenter,yTSUpSep,lineLabel);
 EasyInsertOKCancel(col,3);

 TDialog *d=col->doItCenter(hCtxEdited);
 delete col;

 strcpy(b,s);
 unsigned ret=execDialog(d,b);
 if (ret==cmOK)
    return newStr(b);
 return 0;
}

static
int AddItem(void)
{
 char *s=EditItem("");
 if (s)
   {
    lEdited->insert(s);
    if (lEdited==lists[paliSource])
       // It works only when "stopped"
       DBG_AddPathForSources(s);
    listChanged++;
    return 1;
   }
 return 0;
}

static
int AddDir(void)
{
 char *s=ChooseDir();
 if (s)
   {
    lEdited->insert(s);
    if (lEdited==lists[paliSource])
       // It works only when "stopped"
       DBG_AddPathForSources(s);
    listChanged++;
    return 1;
   }
 return 0;
}

static
int DeleteItem(int which)
{
 lEdited->atRemove(which);
 listChanged++;
 return 1;
}

static
int CancelConfirm(void)
{
 if (listChanged)
    return messageBox(__("Do you want to discard the changes?"),
                      mfWarning | mfYesButton | mfNoButton)==cmYes;
 return 1;
}

void PathListEdit(int which, unsigned hCtx)
{
 lEdited=lists[which];
 if (!lEdited)
   {
    PathListPopulate(which);
    lEdited=lists[which];
    if (!lEdited)
       return;
   }
 tEdited=Titles[which];
 TDialogAID *d=CreateAddInsDelDialog(-1,-1,tEdited,12,50,aidOKEnabled | aidBrowse);
 d->helpCtx=hCtxEdited=hCtx;
 d->DelAction=DeleteItem;
 d->AddAction=AddItem;
 d->CancelAction=CancelConfirm;
 d->BrowseAction=AddDir;

 TStringableListBoxRec box;
 box.items=lEdited;
 box.selection=0;
 listChanged=0;

 TPathList *backup=new TPathList();
 *backup=*lEdited;

 unsigned ret=execDialog(d,&box);
 if (listChanged && ret!=cmOK)
    *lEdited=*backup;

 CLY_destroy(backup);
}

void PathListAdd(int which, const char *path)
{
 lEdited=lists[which];
 if (!lEdited)
   {
    PathListPopulate(which);
    lEdited=lists[which];
    if (!lEdited)
       return;
   }
 lEdited->insert(newStr(path));
}