home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / fm2000 / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-04  |  10.8 KB  |  474 lines

  1. /*
  2.      Filemaster - Multitasking directory utility.
  3.      Copyright (C) 2000  Toni Wilen
  4.      
  5.      This program is free software; you can redistribute it and/or
  6.      modify it under the terms of the GNU General Public License
  7.      as published by the Free Software Foundation; either version 2
  8.      of the License, or (at your option) any later version.
  9.      
  10.      This program is distributed in the hope that it will be useful,
  11.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.      GNU General Public License for more details.
  14.      
  15.      You should have received a copy of the GNU General Public License
  16.      along with this program; if not, write to the Free Software
  17.      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20.  
  21. #include <proto/all.h>
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <devices/timer.h>
  25. #include <strings.h>
  26. #include <stdio.h>
  27. #include "fmdos.h"
  28. #include "fmnode.h"
  29. #include "child.h"
  30. #include "fmlocale.h"
  31. #include "fmgui.h"
  32.  
  33. struct ParentConfig {
  34.     UBYTE parentbuffers;
  35. };
  36.  
  37. void parentpath(struct FMList*,BPTR);
  38. void dirsort(struct FMList*);
  39. void clearlisti(struct List*);
  40. void readdir(struct FMList*);
  41.  
  42. struct FMPathlist pathlist[PATHLISTS];
  43.  
  44. extern struct FMConfig *fmconfig;
  45. extern UBYTE hsparwed[];
  46. extern struct FMMain fmmain;
  47. extern struct Gadget string1;
  48. extern struct Gadget string2;
  49.  
  50. //*****
  51.  
  52. void __saveds getdir(void)
  53. {
  54. struct ProcMsg *pm;
  55. struct FMList *list;
  56.  
  57. pm=sinitproc();
  58. list=fmmain.sourcedir;
  59. if (!(setalloc(list,1))) {
  60.     DisplayBeep(0);
  61.     Delay(50);
  62.     initproc(0,0);
  63.     goto endi;
  64. }
  65. initproc(list,getstring(MSG_DIRNAME));
  66. readdir(list);
  67. endproc(list);
  68. endi:
  69. deinitproc(pm);
  70. }
  71.  
  72. void readdir(struct FMList *list)
  73. {
  74. struct FileInfoBlock *fib;
  75. register struct FMNode *node;
  76. ULONG date,protect;
  77. WORD cnt,cnt2;
  78. LONG timer2;
  79. BPTR lock;
  80.  
  81. clearlist(list);
  82. outputlist(list);
  83. strcpymsg(list->fmmessage1,MSG_DIRRING);
  84. list->fmmessage2[0]=0;
  85. fmmessage(list);
  86.  
  87. if (!(fib=allocmem(sizeof(struct FileInfoBlock)))) { setalloc(list,0); return; }
  88. if (lock=fmlock(list,list->path)) {
  89.     if (fmmain.ikkuna&&list->li) RefreshGList(&list->li->string,fmmain.ikkuna,0,1);
  90.     list->flags&=~LDEVLIST; list->flags|=LDIRLIST|LUPDATEMSG;
  91.     updadirmeter(list);
  92.     if (fmexamine(list,lock,fib,list->path)) {
  93.         timer2=fmmain.timer+5;
  94.         while (fmexnext(list,lock,fib)>0) {
  95.             if (node=allocnode()) {
  96.                 strcpy(list->fmmessage1,fib->fib_FileName);
  97.                 node->numdate=dstolong(&fib->fib_Date);
  98.                 date=dstolong(&fib->fib_Date);
  99.                 longtodatestring(NDDATE(node),date);
  100.                 siirran(NDFILE(node),fib->fib_FileName,fmmain.filelen);
  101.                 siirran(NDCOMM(node),fib->fib_Comment,fmmain.commlen);
  102.                 node->numprot=fib->fib_Protection;
  103.                 protect=fib->fib_Protection^0x0f; cnt2=128;
  104.                 for(cnt=0;cnt<8;cnt++) {
  105.                     if(protect&cnt2) *(NDPROT(node)+cnt)=hsparwed[cnt*3+1]; else *(NDPROT(node)+cnt)='-';
  106.                     cnt2=cnt2>>1;
  107.                 }
  108.                 if (fib->fib_DirEntryType>0) {
  109.                     node->flags=NDIRECTORY;
  110.                 } else {
  111.                     node->flags=NFILE;
  112.                     node->numlen=fib->fib_Size;
  113.                     sformat(NDLENGTH(node),"%lU",fib->fib_Size);
  114.                 }
  115.                 AddTail((struct List*)list,(struct Node*)node);
  116.             }
  117.             if(fmmain.timer>timer2) {
  118.                 dirsort(list);
  119.                 outputlist(list);
  120.                 timer2=fmmain.timer;
  121.             }
  122.             if (testabort(list)) {
  123.                 dirsort(list);
  124.                 if (askabort(list)) break;
  125.             }
  126.         }
  127.     }
  128.     UnLock(lock);
  129. }
  130. freemem(fib);
  131. dirsort(list);
  132. }
  133.  
  134. void dirsort(struct FMList *list)
  135. {
  136. list->fmmessage1[0]=0; list->fmmessage2[0]=0;
  137. fmmessage(list);
  138. csortlist(list);
  139. parselist(list,0);
  140. outputlist(list);
  141. updadirmeter(list);
  142. endofdirtxt(list,0);
  143. }
  144.  
  145. void parentpath(struct FMList *list,BPTR origlock)
  146. {
  147. BPTR lock=0,lock2=0;
  148. UBYTE *ptr1,*ptr2,*ptr3,*ptr4;
  149. WORD len,aa;
  150. struct FileInfoBlock *fib;
  151.  
  152. if (!(ptr1=allocmem(PATH))) goto pathfail;
  153. if (!(fib=allocmem(sizeof(struct FileInfoBlock)))) goto pathfail;
  154. ptr2=ptr1+(PATH-2); *ptr2='/'; ptr4=ptr2;
  155.  
  156. if(!origlock) {
  157.     if (!(lock=Lock(list->path,SHARED_LOCK))) goto pathfail;
  158. } else {
  159.     lock=origlock;
  160. }
  161.  
  162. for(;;) {
  163.     if (!(Examine(lock,fib))) goto pathfail;
  164.     if (fib->fib_DirEntryType<0) goto pathfail;
  165.     len=strlen(fib->fib_FileName);
  166.     ptr2-=len; ptr3=fib->fib_FileName;
  167.     for(aa=0;aa<len;aa++) {
  168.         *(ptr2+aa)=*ptr3++;
  169.     }
  170.     ptr2--; *ptr2='/';
  171.     if (!(lock2=ParentDir(lock))) break;
  172.     UnLock(lock);
  173.     lock=lock2; lock2=0;
  174. }
  175. ptr2++;
  176. ptr3=ptr2;
  177. while(*ptr3++!='/');
  178. *(ptr3-1)=':';
  179. if (*ptr4=='/') *ptr4=0;
  180.  
  181. strcpy(list->path,ptr2);
  182.  
  183. pathfail:
  184. if (lock) UnLock(lock);
  185. if (lock2) UnLock(lock2);
  186. freemem(ptr1);
  187. freemem(fib);
  188. }
  189.  
  190. //*****
  191.  
  192. void endofdirtxt(struct FMList *list,struct FMNode *node)
  193. {
  194. UWORD selfils=0,seldirs=0,totfils=0,totdirs=0,mfils=0,mdirs=0;
  195. ULONG selbytes=0,totbytes=0,mtotbytes=0;
  196. register struct FMNode *node2;
  197.  
  198. if(list->flags&LDIRLIST) {
  199.     for(node2=list->head;node2->succ;node2=node2->succ) {
  200.         if(node2->flags&NMATCHED) {
  201.             totbytes+=node2->numlen;
  202.             if (node2->flags&NSELECTED) {
  203.                 selbytes+=node2->numlen;
  204.                 if (node2->flags & NFILE) selfils++;
  205.                 if (node2->flags & NDIRECTORY) seldirs++;
  206.             }
  207.             if (node2->flags & NFILE) totfils++;
  208.             if (node2->flags & NDIRECTORY) totdirs++;
  209.         }
  210.         mtotbytes+=node2->numlen;
  211.         if (node2->flags & NFILE) mfils++;
  212.         if (node2->flags & NDIRECTORY) mdirs++;
  213.     }
  214.     sformatmsg(list->fmmessage1,MSG_DIRREDLINE1,selfils,totfils,mfils,seldirs,totdirs,mdirs,selbytes,totbytes,mtotbytes);
  215.     list->fmmessage2[0]=0;
  216.     if (node) {
  217.         sformatmsg(list->fmmessage2,MSG_DIRREDLINE2,NDFILE(node),NDPROT(node),NDDATE(node),NDCOMM(node));
  218.     }
  219. } else {
  220.     if(node) {
  221.         sformat(list->fmmessage1,"%s",NDFILE(node));
  222.         sformat(list->fmmessage2,"%s",NDCOMM(node));
  223.     } else {
  224.         list->fmmessage1[0]=0;
  225.         list->fmmessage2[0]=0;
  226.     }
  227. }
  228. fmmessage(list);
  229. }
  230.  
  231. //*****
  232.  
  233.  
  234. void dirmeters(void)
  235. {
  236. WORD cnt;
  237.  
  238. extern struct FMList fmlist[];
  239.  
  240. for(cnt=0;cnt<LISTS;cnt++) updadirmeter(&fmlist[cnt]);
  241. }
  242.  
  243. WORD infodata(UBYTE *file,UBYTE *dst,WORD mode)
  244. {
  245. UBYTE state=getstring(MSG_DIRMETER_RW)[0];
  246. WORD ret=0;
  247. struct InfoData *idata;
  248. struct Process *myproc;
  249. APTR ooldwinptr;
  250. ULONG apu1;
  251. UBYTE used,total;
  252. ULONG usedbytes,totalbytes;
  253. BPTR lock;
  254. WORD percent;
  255.  
  256. if (idata=allocmem(sizeof(struct InfoData))) {
  257.     myproc=(struct Process*)FindTask(0);
  258.     ooldwinptr=myproc->pr_WindowPtr;
  259.     myproc->pr_WindowPtr=(APTR)-1;
  260.     if (lock=Lock(file,SHARED_LOCK)) {
  261.         if (Info(lock,idata)) {
  262.             usedbytes=idata->id_NumBlocksUsed*idata->id_BytesPerBlock;
  263.             totalbytes=idata->id_NumBlocks*idata->id_BytesPerBlock;
  264.             percent=0;
  265.             apu1=totalbytes>>10;
  266.             if(apu1) percent=(usedbytes>>10)*100/apu1;
  267.             if(idata->id_DiskState==ID_VALIDATED) state=getstring(MSG_DIRMETER_RW)[1];
  268.             ret=1;
  269.         }
  270.         UnLock(lock);
  271.     }
  272.     freemem(idata);
  273.     myproc->pr_WindowPtr=ooldwinptr;
  274. }
  275. if(ret) {
  276.     usedbytes/=1024;
  277.     used='K';
  278.     if(usedbytes>=1024) {
  279.         used='M';
  280.         usedbytes/=1024;
  281.     }
  282.     totalbytes/=1024;
  283.     total='K';
  284.     if(totalbytes>=1024) {
  285.         total='M';
  286.         totalbytes/=1024;
  287.     }
  288.     switch(mode)
  289.     {
  290.     case 1:
  291.     sformat(dst,getstring(MSG_DIRMETER),usedbytes,used,totalbytes,total,percent,state);
  292.     break;
  293.     case 0:
  294.     sformat(dst,"%3ld%% %lc",percent,state);
  295.     break;
  296.     }
  297. }
  298. return(ret);
  299. }
  300.  
  301. void updadirmeter(struct FMList *list)
  302. {
  303. UBYTE varatxt[40];
  304. WORD len,xx,yy,didit=0;
  305. struct TextExtent txtext;
  306. struct ListInfo *li;
  307.  
  308.  
  309. li=list->li;
  310. if(!li||!fmmain.ikkuna) return;
  311.  
  312. ObtainSemaphore(&fmmain.gfxsema);
  313. SetDrMd(fmmain.rp,JAM1);
  314. xx=li->dirx; yy=li->topliney;
  315. if (list->flags&LSOURCE) {
  316.     SetAPen(fmmain.rp,fmconfig->sourcepen);
  317. } else if (list->flags&LDESTINATION) {
  318.     SetAPen(fmmain.rp,fmconfig->destpen);
  319. } else {
  320.     SetAPen(fmmain.rp,fmconfig->backpen);
  321. }
  322.  
  323. if (list->flags&LDIRLIST) {
  324.     if(infodata(list->path,varatxt,1)) {
  325.         len=TextFit(fmmain.rp,varatxt,strlen(varatxt),&txtext,0,1,list->li->dirwidth,32767);
  326.         WaitTOF();
  327.         RectFill(fmmain.rp,xx-1,yy-1,xx+li->dirwidth,yy+fmmain.txtysize-1);
  328.         SetAPen(fmmain.rp,fmconfig->txtpen);
  329.         Move(fmmain.rp,xx+(list->li->dirwidth-txtext.te_Width)/2,yy+fmmain.txtbaseline);
  330.         Text(fmmain.rp,varatxt,len);
  331.         didit=1;
  332.     }
  333. }
  334. if(!didit) {
  335.     WaitTOF();
  336.     RectFill(fmmain.rp,xx-1,yy-1,xx+li->dirwidth,yy+fmmain.txtysize-1);
  337. }
  338. SetDrMd(fmmain.rp,JAM2);
  339. ReleaseSemaphore(&fmmain.gfxsema);
  340. }
  341.  
  342. WORD getparent(struct FMList *list)
  343. {
  344. WORD cnt;
  345. struct FMNode *node2,*node;
  346. struct FMPathlist *pathlist;
  347.  
  348. for(cnt=0;cnt<fmmain.numpathlist;cnt++) {
  349.     pathlist=fmmain.pathlist+cnt;
  350.     if (pathlist->path[0]&&strcmp(pathlist->path,list->path)==0) {
  351.         clearlist(list);
  352.         for(node=pathlist->head;node->succ;node=node->succ) {
  353.             node2=dupnode(node);
  354.             if (node2) {
  355.                 node2->flags&=~NSELECTED;
  356.                 AddTail((struct List*)list,(struct Node*)node2);
  357.             }
  358.         }        
  359.         list->firstline=pathlist->firstline;
  360.         list->firstlinew=pathlist->firstlinew;
  361.         pathlist->cnt++;
  362.         if (list->li&&fmmain.ikkuna) RefreshGList(&list->li->string,fmmain.ikkuna,0,1);
  363.         csortlist(list);
  364.         parselist(list,0);
  365.         endofdirtxt(list,0);
  366.         outputlist(list);
  367.         return(1);
  368.     }
  369. }
  370. return(0);
  371. }
  372.  
  373. void shareware(void);
  374.  
  375. void setparent(struct FMList *list)
  376. {
  377. LONG cnt2,cnt3,cnt4;
  378. WORD cnt;
  379. struct FMPathlist *pathlist;
  380.  
  381. if (list->flags&LDIRLIST&&fmmain.numpathlist) {
  382.     cnt3=0x7fffffff; cnt4=0; cnt2=0;
  383.     for(cnt=0;cnt<fmmain.numpathlist;cnt++) {
  384.         pathlist=fmmain.pathlist+cnt;
  385.         if(Stricmp(pathlist->path,list->path)==0) {
  386.             cnt3=0x80000000;
  387.             cnt2=cnt;
  388.         }
  389.         if(pathlist->cnt<cnt3) {
  390.             cnt3=pathlist->cnt;
  391.             cnt2=cnt;
  392.         }
  393.         if(pathlist->cnt>cnt4) cnt4=pathlist->cnt;
  394.     }
  395.     pathlist=fmmain.pathlist+cnt2;
  396.     pathlist->cnt=0;
  397.     pathlist->path[0]=0;
  398.     clearlisti((struct List*)pathlist);
  399.     if (duplist(list,(struct FMList*)pathlist)) strcpy(pathlist->path,list->path);
  400.     pathlist->firstline=list->firstline;
  401.     pathlist->firstlinew=list->firstlinew;
  402.     pathlist->cnt=cnt4+1;
  403. }
  404. }
  405.  
  406. void freepathlist(void)
  407. {
  408. struct FMPathlist *pathlist;
  409. WORD cnt1;
  410.  
  411. pathlist=fmmain.pathlist;
  412. if(!pathlist) return;
  413. for(cnt1=0;cnt1<fmmain.numpathlist;cnt1++) {
  414.     clearlisti((struct List*)(pathlist+cnt1));
  415. }
  416. freemem(pathlist);
  417. fmmain.pathlist=0;
  418. fmmain.numpathlist=0;
  419. }
  420. void initpathlist(void)
  421. {
  422. struct ParentConfig *pc;
  423. struct FMPathlist *pathlist;
  424. WORD cnt1;
  425.  
  426. freepathlist();
  427. pc=getconfignumber(PARENTCONFIG)->moreconfig;
  428. if(!pc->parentbuffers) return;
  429. while(pc->parentbuffers>0) {
  430.     pathlist=allocvec(0,sizeof(struct FMPathlist)*pc->parentbuffers,MEMF_PUBLIC|MEMF_CLEAR);
  431.     if(pathlist) break;
  432.     pc->parentbuffers--;
  433. }
  434. if(pathlist) {
  435.     for(cnt1=0;cnt1<pc->parentbuffers;cnt1++) NewList((struct List*)(pathlist+cnt1));
  436. }
  437. fmmain.numpathlist=pc->parentbuffers;
  438. fmmain.pathlist=pathlist;
  439. }
  440.  
  441. void *parentconfigdefault(struct CMenuConfig *cmc)
  442. {
  443. struct ParentConfig *config;
  444. WORD ret;
  445.  
  446. ret=allocconfig(cmc,sizeof(struct ParentConfig));
  447. if(ret<0) return(cmc->moreconfig);
  448. if(!ret) return(0);
  449. config=cmc->moreconfig;
  450. config->parentbuffers=10;
  451. return(cmc->moreconfig);
  452. }
  453.  
  454. WORD parentconfig(struct GUIBase *gb,struct CMenuConfig *cmc)
  455. {
  456. WORD c;
  457. struct ParentConfig *pc;
  458. LONG bufs;
  459.  
  460. pc=getconfig(cmc);
  461. bufs=pc->parentbuffers;
  462. setguigroup(gb,1,0);
  463. reqinfomsg(gb,MSG_PARENT_CONFIG_BUFFERS,200,guiUC|guiLEFT);
  464. setguigroup(gb,2,0);
  465. reqinteger(gb,200,&bufs,0,99);
  466. commandanswer(gb);
  467. c=quickreq(gb);
  468. if(c) {
  469.     pc->parentbuffers=bufs;
  470.     initpathlist();
  471. }
  472. return(c);
  473. }
  474.