home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / windows / winterm / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-27  |  5.3 KB  |  208 lines

  1. /*** config.c ***/
  2.  
  3. #include <stdio.h>
  4. #include "windows.h"
  5. #include "config.h"
  6. #include "expect.h"
  7. #include "sioerror.h"
  8. #include "pcl4w.h"
  9. #include "term.h"
  10.  
  11. extern int  OnLineFlag;
  12. extern HWND hMainWnd;
  13.  
  14. static struct
  15.  {int Port;
  16.   int BaudRate;
  17.   int WordLength;
  18.   int Parity;
  19.   int StopBits;
  20.  } Config = {COM1,Baud9600,WordLength8,NoParity,OneStopBit};
  21.  
  22. static long BaudRateList[10] =
  23.     {300L,600L,1200L,2400L,4800L,9600L,19200L,38400L,57600L,115200L};
  24. static char StopBitList[2] = {'1','2'};
  25. static char ParityList[4]  = {'N','O','?','E'};
  26. static char WordLengthList[4] = {0,0,'7','8'};
  27.  
  28. static int PortMsgID[4] = {MSG_COM1,MSG_COM2,MSG_COM3,MSG_COM4};
  29. static int BaudRateMsgID[10] = {0,0,MSG_1200,MSG_2400,
  30.                                 MSG_4800,MSG_9600,MSG_19200,MSG_38400,
  31.                                 MSG_57600,MSG_115200};
  32. static int StopBitsMsgID[2] = {MSG_1_SB,MSG_2_SB};
  33. static int ParityMsgID[4] = {MSG_NONE,MSG_ODD,0,MSG_EVEN};
  34. static int WordLengthMsgID[4] = {0,0,MSG_7_DB,MSG_8_DB};
  35.  
  36. static char *LineText[2] = {"OFF", "ON"};
  37.  
  38. static void SetPSL(int,int,int);
  39. static void CheckTheMenu(int);
  40. static void UncheckTheMenu(int);
  41. static void CheckAll(void);
  42. static void UncheckAll(void);
  43. static int  UpdateTheChecks(int,int);
  44.  
  45. static LastPortID = MSG_COM1;
  46. static LastParityID = MSG_NONE;
  47. static LastStopBitsID = MSG_1_SB;
  48. static LastWordLengthID = MSG_8_DB;
  49. static LastBaudRateID = MSG_9600;
  50.  
  51. void InitConfig()
  52. {CheckAll();
  53.  SetTitle();
  54. }
  55.  
  56. void LoadConfig()
  57. {int hFile;
  58.  hFile = _lopen("term.cfg",OF_READ | OF_SHARE_DENY_WRITE);
  59.  if(hFile<0) ErrorMessage("TERM.CFG not found");
  60.  else
  61.    {/* uncheck menu selections */
  62.     UncheckAll();
  63.     /* read in new configuration */
  64.     _lread(hFile,&Config,sizeof(Config));
  65.     _lclose(hFile);
  66.     SetTitle();
  67.     /* set current menu selections */
  68.     LastPortID = PortMsgID[Config.Port];
  69.     LastParityID = ParityMsgID[Config.Parity];
  70.     LastStopBitsID = StopBitsMsgID[Config.StopBits];
  71.     LastWordLengthID = WordLengthMsgID[Config.WordLength];
  72.     LastBaudRateID = BaudRateMsgID[Config.BaudRate];
  73.     /* check current menu selections */
  74.     CheckAll();
  75.    }
  76. } /* end LoadConfig */
  77.  
  78. void SaveConfig()
  79. {int hFile;
  80.  hFile = _lcreat("term.cfg",0);
  81.  _lwrite(hFile,&Config,sizeof(Config));
  82.  _lclose(hFile);
  83. } /* end LoadConfig */
  84.  
  85. void SetParity(int Parity)
  86. {
  87.  SetPSL(Parity,Config.StopBits,Config.WordLength);
  88.  LastParityID = UpdateTheChecks(LastParityID,ParityMsgID[Parity]);
  89. }
  90.  
  91. void SetStopBits(int StopBits)
  92. {
  93.  SetPSL(Config.Parity,StopBits,Config.WordLength);
  94.  LastStopBitsID = UpdateTheChecks(LastStopBitsID,StopBitsMsgID[StopBits]);
  95. }
  96.  
  97. void SetWordLength(int WordLength)
  98. {
  99.  SetPSL(Config.Parity,Config.StopBits,WordLength);
  100.  LastWordLengthID = UpdateTheChecks(LastWordLengthID,WordLengthMsgID[WordLength]);
  101. }
  102.  
  103. void SetPSL(int Parity,int StopBits,int WordLength)
  104. {int RetCode;
  105.  if(!ExpectNoXfer()) return;
  106.  Config.Parity = Parity;
  107.  Config.StopBits = StopBits;
  108.  Config.WordLength = WordLength;
  109.  if(OnLineFlag)
  110.    {RetCode = SioParms(Config.Port,Config.Parity,
  111.                        Config.StopBits,Config.WordLength);
  112.     if(RetCode<0) SioError(RetCode,"SioParms");
  113.    }
  114.  SetTitle();
  115.  /* end SetPSL */
  116. }
  117.  
  118. void SetBaud(int BaudRate)
  119. {int RetCode;
  120.  if(!ExpectNoXfer()) return;
  121.  Config.BaudRate = BaudRate;
  122.  if(OnLineFlag)
  123.    {RetCode = SioBaud(Config.Port,Config.BaudRate);
  124.     if(RetCode<0) SioError(RetCode,"SioBaud");
  125.    }
  126.  LastBaudRateID = UpdateTheChecks(LastBaudRateID,BaudRateMsgID[BaudRate]);
  127.  SetTitle();
  128. } /* end SetBaud */
  129.  
  130. void SetPort(int Port)
  131. {
  132.  if(!ExpectNoXfer()) return;
  133.  if(!ExpectOffLine()) return;
  134.  Config.Port = Port;
  135.  LastPortID = UpdateTheChecks(LastPortID,PortMsgID[Port]);
  136.  SetTitle();
  137. } /* end SetPort */
  138.  
  139. int GetBaud(void)
  140. {return (Config.BaudRate);
  141. }
  142.  
  143. int GetParity(void)
  144. {return (Config.Parity);
  145. }
  146.  
  147. int GetStopBits(void)
  148. {return (Config.StopBits);
  149. }
  150.  
  151. int GetWordLength(void)
  152. {return(Config.WordLength);
  153. }
  154.  
  155. int GetPort(void)
  156. {return (Config.Port);
  157. }
  158.  
  159. void SetTitle()
  160. {char Text[80];
  161.  sprintf(Text,"TERM: COM%d @ %ld baud %c%c%c %sLINE",
  162.     Config.Port+1,
  163.     BaudRateList[Config.BaudRate],
  164.     WordLengthList[Config.WordLength],
  165.     ParityList[Config.Parity],
  166.     StopBitList[Config.StopBits],
  167.     LineText[OnLineFlag&1]);
  168.  SetWindowText(hMainWnd,Text);
  169. } /* end SetTitle */
  170.  
  171. void CheckTheMenu(int MenuID)
  172. {HMENU hMenu;
  173.  if(MenuID)
  174.    {hMenu = GetMenu(hMainWnd);
  175.     CheckMenuItem(hMenu,MenuID,MF_BYCOMMAND | MF_CHECKED);
  176.    }
  177. } /* end CheckTheMenu */
  178.  
  179. void UncheckTheMenu(int MenuID)
  180. {HMENU hMenu;
  181.  if(MenuID)
  182.    {hMenu = GetMenu(hMainWnd);
  183.     CheckMenuItem(hMenu,MenuID,MF_BYCOMMAND | MF_UNCHECKED);
  184.    }
  185. } /* end CheckTheMenu */
  186.  
  187. void UncheckAll(void)
  188. {UncheckTheMenu(LastPortID);
  189.  UncheckTheMenu(LastParityID);
  190.  UncheckTheMenu(LastStopBitsID);
  191.  UncheckTheMenu(LastWordLengthID);
  192.  UncheckTheMenu(LastBaudRateID);
  193. } /* end UncheckAll */
  194.  
  195. void CheckAll(void)
  196. {CheckTheMenu(LastPortID);
  197.  CheckTheMenu(LastParityID);
  198.  CheckTheMenu(LastStopBitsID);
  199.  CheckTheMenu(LastWordLengthID);
  200.  CheckTheMenu(LastBaudRateID);
  201. } /* end CheckAll */
  202.  
  203. int UpdateTheChecks(int OldMsgID,int NewMsgID)
  204. {UncheckTheMenu(OldMsgID);
  205.  CheckTheMenu(NewMsgID);
  206.  return NewMsgID;
  207. } /* end UpdateTheChecks */
  208.