source: trunk/dll/menu.c@ 380

Last change on this file since 380 was 356, checked in by root, 19 years ago

Check more run time errors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
RevLine 
[2]1
[123]2/***********************************************************************
3
4 $Id: menu.c 356 2006-07-26 19:58:37Z root $
5
6 Custom menu support routines for FM/2
7
8 Copyright (c) 1996-98 M. Kimes
[356]9 Copyright (c) 2004, 2006 Steven H.Levine
[123]10
[356]11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 22 Jul 06 SHL Check more run time errors
[123]13
14***********************************************************************/
15
[2]16#define INCL_DOS
17#define INCL_WIN
[356]18#include <os2.h>
[2]19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <share.h>
[356]24
[2]25#include "fm3dll.h"
26#include "menu.h"
27
28#pragma data_seg(DATA2)
[356]29
30static PSZ pszSrcFile = __FILE__;
31
[2]32#pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
33
34MENU *menuhead = NULL;
35
[356]36INT tokenize (CHAR *str,INT max,CHAR **tokens)
37{
[2]38 INT x = 0;
39 CHAR *p;
40
41 if(str && max && tokens) {
42 p = str;
43 for(;;) {
44 p = skip_delim(p," \t");
45 if(!p)
46 break;
47 tokens[x++] = p;
48 if(x == max)
49 break;
50 p = to_delim(p," \t");
51 if(!p)
52 break;
53 *p = 0;
54 p++;
[356]55 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
[2]56 if(!*p)
57 break;
58 }
59 }
60 return x;
61}
62
63
[356]64VOID FreeMenuList (MENU *head)
65{
[2]66 MENU *info,*next;
67
68 info = head;
69 while(info) {
70 next = info->next;
71 if(info->text)
72 free(info->text);
73 free(info);
74 info = next;
75 }
76}
77
78
[356]79BOOL AddToMenu (CHAR *filename,HWND hwndMenu)
80{
[2]81 FILE *fp;
82 CHAR s[256];
83 CHAR *tokens[3];
84 INT lines = 0;
85 MENU *info,*last = NULL;
86 BOOL ret = FALSE;
87
[356]88 // fixme to complain?
89 if (!hwndMenu) {
90 Runtime_Error(pszSrcFile, __LINE__, "no data");
[2]91 return ret;
[356]92 }
93 if (!filename)
[2]94 filename = "FM3MENU.DAT";
95 fp = _fsopen(filename,"r",SH_DENYWR);
[356]96 if (!fp) {
97 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
98 }
99 else {
100 while (!feof(fp)) {
101 if (!fgets(s,256,fp))
[2]102 break;
103 lines++;
[123]104 bstripcr(s);
[2]105 if(!*s || *s == ';')
106 continue;
[356]107 if (tokenize(s,3,tokens) == 3 && (USHORT)atoi(tokens[1])) {
108 info = xmallocz(sizeof(MENU),pszSrcFile,__LINE__);
109 if (info) {
[2]110 info->size = sizeof(MENU);
[356]111 info->text = xstrdup(tokens[2],pszSrcFile,__LINE__);
112 if (!info->text)
113 free(info);
114 else {
115 if (!stricmp(tokens[0],"MENUITEM"))
[2]116 info->cmd = atoi(tokens[1]);
[356]117 else if (!stricmp(tokens[0],"SEPARATOR"))
[2]118 info->type = SEPARATOR;
[356]119 else {
120 /* error! */
[2]121 free(info->text);
122 free(info);
123 info = NULL;
124 }
[356]125 if (info) {
[2]126 if(!menuhead)
127 menuhead = info;
128 else
129 last->next = info;
130 info->next = NULL;
131 last = info;
132 }
133 }
134 }
135 }
[356]136 else {
137 // fixme to complain?
138 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
[2]139 }
140 }
141 fclose(fp);
142
[356]143 if (menuhead) {
[2]144 MENUITEM mi;
145
146 memset(&mi,0,sizeof(mi));
147 info = menuhead;
148 WinEnableWindow(hwndMenu,FALSE);
[356]149 while (info) {
[2]150 mi.iPosition = MIT_END;
151 mi.id = info->cmd;
152 mi.afStyle = (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
[356]153 if (WinSendMsg(hwndMenu, MM_INSERTITEM, MPFROMP(&mi),
154 MPFROMP(info->text)))
[2]155 ret = TRUE;
156 info = info->next;
157 }
158 WinEnableWindow(hwndMenu,TRUE);
159 FreeMenuList(menuhead);
160 menuhead = NULL;
161 }
162 }
163 return ret;
164}
Note: See TracBrowser for help on using the repository browser.