/************************************************************************ Name: AMI.CPL Maintain added menu items for Liberty Copyright (c) 1995 New Solutions Ltd. ALL RIGHTS RESERVED. Portions copyright (c) 1991 Dataflight Software, Inc. 1.0 MGC 1-4-96 new 1.0a MGC 3-4-96 Use full db.database name to locate files Embed getText() from ljutils.cpl 2.0 MGC 15-4-96 Release 2.0 2.1a MGC 24-4-97 Correct error when adding first entry into ini file Overlap the menus *************************************************************************/ text vers="v2.1a"; int loop=1, maxamis, RED=128, GREEN=32768, BLUE=12599296, BLACK=0, LT_GREY = 12632256, DK_GREY = 8421504, WHITE = 16777215, TRUE = 1, FALSE = 0, LF = 10, /* Line Feed */ FF = 12, /* Form Feed */ CR = 13, /* Carriage Return */ ESC = 27, /* Escape key */ msgr=26; text amis[50]; main() { int db; text dbname; puts(0,MaxCol_-18,vers); MenuBackground_ = LT_GREY; MenuHighlightBackground_ = BLUE; dbname=getDbName(db); /* If we have a valid database */ if (len(dbname)>0) { while (loop) { /* Show INI file entries */ if (readINI(dbname)) showINI(dbname); else loop=FALSE; } } } /************************************************************ Extract the [AddedMenuItems] section from the INI file. Write the remaining lines to ONO. If we save changes we will append our section onto the end of ONO and then rename this to INI. ************************************************************/ readINI(text dbname) { int ih, oh, amiflag; char iline[3000]; ih=open(dbname+".INI","r"); if (ih<0) { puts(msgr,5,"Failed to open "+dbname+".INI"); sleep(3000); return(FALSE); } oh=open(dbname+".ONO","w"); if (oh<0) { puts(msgr,5,"Failed to open "+dbname+".ONO"); close(ih); sleep(3000); return(FALSE); } amiflag=0; maxamis=0; while (readln(ih,iline)>=0) { if (amiflag) { if (len(iline)>0) { if (substr(iline,1,1)=="[") { /* This is the beginning of another section */ writeln(oh,iline,len(iline)); amiflag=0; } else { amis[maxamis]=iline; maxamis=maxamis+1; } } } else { if (iline=="[AddedMenuItems]") amiflag=1; else writeln(oh,iline,len(iline)); } } close(ih); close(oh); return(TRUE); } /************************************************************ Display the entries from the AMI section - one user per line. ************************************************************/ showINI(text dbname) { text clist[100]; text user, savet, savea; int j, rc, cuser, clistx, mx=2, oh; while (loop) { clist[0]="Select User Id"; clist[1]="Add new user"; clistx=2; cuser=0; while (cuser0) { savet=save(7,7,18,27); rc=menu(7,7,18,27,clist,1); restore(7,7,savet); } else rc=0; if (rc) { user=amis[rc-1]; j=matchc(user,"="); user=substr(user,j+1); amis[maxamis]=amis[maxamis]+user; } maxamis=maxamis+1; break; default: savea=save(4,20,15,45); showHead(rc-2); restore(4,20,savea); break; } } /* Should we save the file */ if (saveChanges()) { oh=open(dbname+".ONO","a"); if (oh<0) { puts(msgr,5,"Failed to write "+dbname+".ONO"); sleep(3000); return; } savea="[AddedMenuItems]"; writeln(oh,savea,len(savea)); for (j=0; j0) { head=substr(entries,1,j-1); /* This heading */ k=matchc(head,"&"); if (k>0) { for (k=2; k 0) { k=matchc(head,"&"); if (k>0) { for (k=2; k0) { head=substr(entries,1,j-1); /* This heading */ entries=substr(entries,j+1); j=matchc(entries,","); item=substr(entries,1,j-1); /* This item */ entries=substr(entries,j+1); if (head==chead and len(item)>0) { clist[clistx]=item; clistx=clistx+1; } j=matchc(entries,","); entries=substr(entries,j+1); j=matchc(entries,")"); entries=substr(entries,j+1); j=matchc(entries,","); entries=substr(entries,j+1); j=matchc(entries,"("); } rc=menu(5,40,16,65,clist,mx); mx=rc; switch (rc) { case 0: break; case 1: item="Enter new item"; item=getText(item); if (substr(amis[amix],len(amis[amix]))=="=") amis[amix]=amis[amix]+chead+"("+item+",,-1)"; else amis[amix]=amis[amix]+","+chead+"("+item+",,-1)"; break; default: updateItem(amix, chead, clist[rc]); break; } } } /************************************************************ Update the values for this particular entry ************************************************************/ updateItem(int amix; text chead, citem) { char item[60], pgm[60], parm[60], pos[60]; text entries, head, savec; text ami1, ami2, bchar; int rc, j, k, startx, elen, element=1; item=citem; savec=save(18,10,25,70); box(18,10,23,70, "3D", DK_GREY, LT_GREY); puts(19,15," Item text:", RED, LT_GREY); puts(20,15," Program:", RED, LT_GREY); puts(21,15," Parameters:", RED, LT_GREY); puts(22,15," Position:", RED, LT_GREY); while (rc<>ESC) { entries=amis[amix]; elen=len(entries); j=matchc(entries,"="); /* Drop out user */ entries=substr(entries,j+1); j=matchc(entries,"("); while (j>0) { /* Locate heading */ startx=elen-len(entries); head=substr(entries,1,j-1); /* This heading */ k=matchc(head,"&"); head=substr(head,1,k-1)+substr(head,k+1); entries=substr(entries,j+1); j=matchc(entries,","); item=substr(entries,1,j-1); /* This item */ entries=substr(entries,j+1); j=matchc(entries,","); pgm=substr(entries,1,j-1); /* Program */ entries=substr(entries,j+1); j=matchc(entries,",)"); pos=substr(entries,1,j-1); /* Pos */ if (substr(entries,j,1)==",") { entries=substr(entries,j+1); j=matchc(entries,")"); /* The parm may include a backet search for another */ parm=substr(entries,1,j-1); /* Parm */ entries=substr(entries,j+1); if (substr(entries,1,1)==")") parm=parm+")"; j=1; } else parm=""; entries=substr(entries,j+1); j=matchc(entries,","); /* This should be a 0 distance, ")," */ entries=substr(entries,j+1); j=matchc(entries,"("); if (chead==head and citem==item) break; } rc=edit( item, 19,28,19,68, "ET", pgm, 20,28,20,68, "ET", parm, 21,28,21,68, "ET", pos, 22,28,22,68, "ET", "Ok", 24,23,25,33, "Button", "Cancel",24,35,25,45,"Button", "Delete",24,47,25,57,"Button", element, "", 1,1,1); if (rc==ESC) break; switch (element) { case 4: /* Fall off end of fields */ case 5: /* Save */ entries=amis[amix]; ami1=substr(entries,1,startx-1); bchar=substr(entries,startx,1); ami2=substr(entries,startx); j=matchc(ami2,"("); /* Take out head */ ami2=substr(ami2,j+1); j=matchc(ami2,","); /* Take out item */ ami2=substr(ami2,j+1); j=matchc(ami2,","); /* Take out program */ ami2=substr(ami2,j+1); j=matchc(ami2,",)"); /* Take out pos */ ami2=substr(ami2,j); if (substr(ami2,1,1)==",") { ami2=substr(ami2,2); j=matchc(ami2,")"); /* Take out parm */ ami2=substr(ami2,j+1); if (substr(ami2,1,1)==")") ami2=substr(ami2,2); } else ami2=substr(ami2,2); head=head+"("+item+","+pgm+","+pos; if (len(parm)>0) head=head+","+parm+")"; else head=head+")"; if (bchar=="=") amis[amix]=ami1+"="+head+ami2; else amis[amix]=ami1+","+head+ami2; puts(msgr,5,rep(' ',75)); puts(msgr,5,substr(amis[amix],len(ami1)-5,75)); rc=ESC; break; case 6: /* cancel */ rc=ESC; break; case 7: /* delete entry */ entries=amis[amix]; ami1=substr(entries,1,startx-1); ami2=substr(entries,startx); j=matchc(ami2,"("); /* Take out head */ ami2=substr(ami2,j+1); j=matchc(ami2,","); /* Take out item */ ami2=substr(ami2,j+1); j=matchc(ami2,","); /* Take out program */ ami2=substr(ami2,j+1); j=matchc(ami2,",)"); /* Take out pos */ ami2=substr(ami2,j); if (substr(ami2,1,1)==",") { ami2=substr(ami2,2); j=matchc(ami2,")"); /* Take out parm */ ami2=substr(ami2,j+1); if (substr(ami2,1,1)==")") ami2=substr(ami2,2); } else ami2=substr(ami2,2); amis[amix]=ami1+ami2; puts(msgr,5,rep(' ',75)); puts(msgr,5,substr(amis[amix],len(ami1)-5,75)); rc=ESC; break; } } restore(18,10,savec); } /************************************************* Return the database name for the current database Leave off the extension. *************************************************/ getDbName(int db) { int i; text dbname; dbname=db.database; /* while (i=matchc(dbname,"\")) dbname=substr(dbname,i+1); */ i=matchc(dbname,"."); if (i>0) dbname=substr(dbname,1,i-1); return(dbname); } saveChanges() { text savec; char ch=1; savec=save(18,10,25,70); box(18,10,23,70, "3D", DK_GREY, LT_GREY); puts(20,15,"Save changes to INI file? (Y/N)", BLUE, LT_GREY); while (ch<>0) { beep(600,50); ch=getkey(); if (ch=='Y' or ch=='y') return(1); if (ch=='N' or ch=='n') return(0); } return(0); } /**************************************************************** Name: getText Synopsis: Generic text prompting ****************************************************************/ getText(text prompt) { int element=1, so=1, eo=1, rc; char id[80]; text scrn; scrn=save(8,10,16,70); box( 8, 10, 16, 70, '3D', BLUE, LT_GREY); puts(10,15,prompt,BLUE,LT_GREY); rc=edit( id, 12,15,12,65, 'ES', element, '', so,eo, BLACK); if (rc==ESC) id=""; restore(8,10,scrn); return(id); } /* getText */