home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / odbcsdk / samples / admndemo / ini.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-07  |  10.0 KB  |  279 lines

  1. //*---------------------------------------------------------------------------------
  2. //|  ODBC System Administrator
  3. //|
  4. //|  This code is furnished on an as-is basis as part of the ODBC SDK and is
  5. //|  intended for example purposes only.
  6. //|
  7. //|    Title:    INI.C
  8. //|        This module contains the functions which work with SATOOL.INI.
  9. //*---------------------------------------------------------------------------------
  10. #include "ini.h"
  11. #include "results.h"
  12. #include <commdlg.h>
  13.  
  14.  
  15. //*---------------------------------------------------------------------------------
  16. //|    Global variables
  17. //*---------------------------------------------------------------------------------
  18. dCSEG(char)    szLABINI[]                                = "ADMNDEMO.INI";
  19. dCSEG(char)    szSCREEN[]                                 = "screen";
  20. dCSEG(char) szMAX[]                                    = "Maximized";
  21. dCSEG(char)    szCONNECTOPTIONS[]                    = "Connect Options";
  22. dCSEG(char) szMAXRESULTS[]                            = "Max Results Windows";
  23. dCSEG(char) szFONT[]                                    = "Font";
  24. dCSEG(char) szFONTSIZE[]                            = "FontSize";
  25. dCSEG(char) szFONTWEIGHT[]                            = "FontWeight";
  26. dCSEG(char) szFONTNAME[]                            = "FontName";
  27. dCSEG(char)    szFONTRGB[]                                = "FontColor";
  28. dCSEG(char) szSTRIKEOUT[]                            = "FontStrikeOut";
  29. dCSEG(char) szUNDERLINE[]                            = "FontUnderline";
  30. dCSEG(char) szFONTSTYLE[]                            = "FontStyle";
  31. dCSEG(char) szPIPES[]                                = "Pipes";
  32. dCSEG(char) szRegular[]                                = "Regular";
  33. dCSEG(char) szArial[]                                = "Arial";
  34. dCSEG(char) szMSSansSerif[]                        = "MS Sans Serif";
  35. dCSEG(char) szINSTALL[]                                = "Installed";
  36. dCSEG(char) szDROPPROC[]                            = "Drop Procedure (with semi-colon)";
  37. dCSEG(char) szDROPTABLE[]                            = "Drop Table";
  38. dCSEG(char) szSQLSECTION[]                            = "Sql";
  39. dCSEG(char) szPARMSSECTION[]                        = "Parms";
  40. dCSEG(char) szPARMOPTSECTION[]                    = "ParmOpt";
  41. dCSEG(char) szDELETESECTION[]                        = "Delete";
  42. dCSEG(char) szSQLDROPSTMT[]                        = "drop procedure ?";
  43. dCSEG(char) szSQLDROPTABLESTMT[]                    = "drop table ?";
  44. dCSEG(char) szPARMVALUE[]                            = "value";
  45. dCSEG(char) szPARM1[]                                = "1";
  46. dCSEG(char) szPARM3[]                                = "3";
  47.  
  48.  
  49. //*---------------------------------------------------------------------------------
  50. //|    Local Functions
  51. //*---------------------------------------------------------------------------------
  52. LPSTR GetFontStyle(LPSTR szStyle, int cbStyle);
  53. void SetDefaultFont(LOGFONT FAR * lf);
  54. void SetDefaultRGB(COLORREF rgb);
  55. void SetFontStyle(LPSTR szFontStyle);
  56.  
  57.  
  58. //*---------------------------------------------------------------------------------
  59. //| ResolveIniFile:
  60. //|    Look for ADMNDEMO.INI, if not found, fill out the defaults.
  61. //| Parms:
  62. //|    Nothing
  63. //| Returns:              
  64. //|    Nothing.
  65. //*---------------------------------------------------------------------------------
  66. VOID INTFUN ResolveIniFile()
  67. {
  68.     // Max status is written after every exit, so if this constant cannot
  69.     //    be found, there is no file to find.
  70.     if(GetPrivateProfileInt(szSCREEN, szMAX, -1, szLABINI) == -1) {
  71.         // No file, create one
  72.         WritePrivateProfileString(szPIPES, szDROPPROC, szINSTALL, szLABINI);
  73.         WritePrivateProfileString(szDROPPROC, szSQLSECTION, szSQLDROPSTMT, szLABINI);
  74.         WritePrivateProfileString(szDROPPROC, szPARMSSECTION, szPARM3, szLABINI);
  75.         WritePrivateProfileString(szDROPPROC, szPARMOPTSECTION, szPARMVALUE, szLABINI);
  76.         WritePrivateProfileString(szDROPPROC, szDELETESECTION, szPARM1, szLABINI);
  77.  
  78.         WritePrivateProfileString(szPIPES, szDROPTABLE, szINSTALL, szLABINI);
  79.         WritePrivateProfileString(szDROPTABLE, szSQLSECTION, szSQLDROPTABLESTMT, szLABINI);
  80.         WritePrivateProfileString(szDROPTABLE, szPARMSSECTION, szPARM3, szLABINI);
  81.         WritePrivateProfileString(szDROPTABLE, szPARMOPTSECTION, szPARMVALUE, szLABINI);
  82.         WritePrivateProfileString(szDROPTABLE, szDELETESECTION, szPARM1, szLABINI);
  83.         }
  84. }
  85.  
  86.  
  87. //*---------------------------------------------------------------------------------
  88. //| GetDefaultFont:
  89. //|    This function will retrieve all of the font information that is stored in
  90. //|    the .INI file and places it in the LOGFONT structure passed in.
  91. //|    NOTE:  User should clear the LOGFONT structure to 0 (eg: fmemset(&lf, 0,sizeof())
  92. //|      before calling this function.  The font name buffer must point to a user's
  93. //|      allocated buffer.
  94. //| Parms:
  95. //|    in            lf                    LOGFONT structure
  96. //| Returns:              
  97. //|    Pointer to the LOGFONT structure passed in or NULL on error.
  98. //*---------------------------------------------------------------------------------
  99. LOGFONT FAR * GetDefaultFont(LOGFONT FAR * lf)
  100. {
  101.     lf->lfHeight = GetPrivateProfileInt(szFONT, szFONTSIZE, -12, szLABINI);
  102.     lf->lfWeight = GetPrivateProfileInt(szFONT, szFONTWEIGHT, FW_NORMAL, szLABINI);
  103.     lf->lfStrikeOut = GetPrivateProfileInt(szFONT, szSTRIKEOUT, 0, szLABINI);
  104.     lf->lfUnderline = GetPrivateProfileInt(szFONT, szUNDERLINE, 0, szLABINI);
  105.     lf->lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
  106.     if(!GetPrivateProfileString(szFONT, szFONTNAME, NULL,
  107.                 lf->lfFaceName, LF_FACESIZE, szLABINI))
  108.         lstrcpy(lf->lfFaceName, (LPSTR)szArial);
  109.  
  110.     return lf;
  111. }
  112.  
  113.  
  114. //*---------------------------------------------------------------------------------
  115. //| SetDefaultFont:
  116. //|    This function uses the values in the LOGFONT structure passed in to write the
  117. //|    default values to the .INI file.
  118. //| Parms:
  119. //|    in            lf                    LOGFONT structure
  120. //| Returns:              
  121. //|    Nothing
  122. //*---------------------------------------------------------------------------------
  123. void SetDefaultFont(LOGFONT FAR * lf)
  124. {
  125.     char     szTmp[20];
  126.     char    szpd[] = "%d";
  127.     
  128.     wsprintf(szTmp, szpd, lf->lfHeight);
  129.     WritePrivateProfileString(szFONT, szFONTSIZE, szTmp, szLABINI);
  130.     
  131.     wsprintf(szTmp, szpd, lf->lfWeight);
  132.     WritePrivateProfileString(szFONT, szFONTWEIGHT, szTmp, szLABINI);
  133.  
  134.     wsprintf(szTmp, szpd, lf->lfStrikeOut);
  135.     WritePrivateProfileString(szFONT, szSTRIKEOUT, szTmp, szLABINI);
  136.  
  137.     wsprintf(szTmp, szpd, lf->lfUnderline);
  138.     WritePrivateProfileString(szFONT, szUNDERLINE, szTmp, szLABINI);
  139.  
  140.     WritePrivateProfileString(szFONT, szFONTNAME, lf->lfFaceName, szLABINI);
  141. }
  142.  
  143.  
  144.  
  145. //*---------------------------------------------------------------------------------
  146. //| GetDefaultRGB:
  147. //|    This function will retrieve the default rgbvalue from the ini file.
  148. //| Parms:
  149. //|    Nothing.
  150. //| Returns:
  151. //|    A COLORREF value that can be used for color.
  152. //*---------------------------------------------------------------------------------
  153. COLORREF GetDefaultRGB(void)
  154. {
  155.     COLORREF    dft;
  156.     LPSTR        lpsz;
  157.     char        szBuff[20];
  158.  
  159.     if(!GetPrivateProfileString(szSCREEN, szFONTRGB, NULL,
  160.                 szBuff, sizeof(szBuff), szLABINI))
  161.         dft = RGB(0,0,0);                // If not specified, use Black
  162.     else
  163.         dft = strtoul(szBuff, &lpsz, 10);
  164.  
  165.     return dft;
  166. }
  167.  
  168.  
  169. //*---------------------------------------------------------------------------------
  170. //| SetDefaultRGB:
  171. //|    This function will set the default RGB value which will be used for
  172. //|    color.
  173. //| Parms:
  174. //|    in            rgb                        The rgb value to use
  175. //| Returns:              
  176. //|    Nothing
  177. //*---------------------------------------------------------------------------------
  178. void SetDefaultRGB(COLORREF rgb)
  179. {
  180.     char        szBuff[20];
  181.     
  182.     wsprintf(szBuff, "%lu", rgb);
  183.     WritePrivateProfileString(szSCREEN, szFONTRGB, 
  184.             szBuff, szLABINI);
  185. }
  186.  
  187.  
  188. //*---------------------------------------------------------------------------------
  189. //| GetFontStyle:
  190. //|    This function will copy the default font style to the user's buffer.
  191. //| Parms:
  192. //|    in            szStyle                        Pointer to output for font sytle
  193. //|    in            cbStyle                        Maximum size of buffer
  194. //| Returns:
  195. //|    Style to use, NULL otherwise
  196. //*---------------------------------------------------------------------------------
  197. LPSTR GetFontStyle(LPSTR szStyle, int cbStyle)
  198. {
  199.     if(!GetPrivateProfileString(szSCREEN, szFONTSTYLE, NULL,
  200.                 szStyle, cbStyle, szLABINI))
  201.         lstrcpy(szStyle, (LPSTR)szRegular);
  202.     return szStyle;
  203. }
  204.  
  205.  
  206. //*---------------------------------------------------------------------------------
  207. //| SetFontStyle:
  208. //|    This function will set the default font style.
  209. //| Parms:
  210. //|    in            szFontStyle                The new font style
  211. //| Returns:              
  212. //|    Nothing
  213. //*---------------------------------------------------------------------------------
  214. void SetFontStyle(LPSTR szFontStyle)
  215. {
  216.     WritePrivateProfileString(szSCREEN, szFONTSTYLE, 
  217.             szFontStyle, szLABINI);
  218. }
  219.  
  220.  
  221. //*---------------------------------------------------------------------------------
  222. //| GetPipeNames:
  223. //|    This function will get as many pipes as will fit into the user's
  224. //|    buffer.  The list is simply retrieve via GetPrivateProfileString
  225. //|    and therefore each pipe name will be null terminated with 2 nulls
  226. //|    at the end of the entire list.
  227. //| Parms:
  228. //|    in            szPipes                        Pointer to string to hold names
  229. //|    in            size                            Maximum size of pipe string
  230. //| Returns:              
  231. //|    Nothing
  232. //*---------------------------------------------------------------------------------
  233. void GetPipeNames(LPSTR szPipes, int size)
  234. {
  235.     if(!GetPrivateProfileString(szPIPES, NULL, NULL, szPipes, size,
  236.                 szLABINI))
  237.         *szPipes = '\0';
  238. }
  239.  
  240.  
  241. //*---------------------------------------------------------------------------------
  242. //| PromptForFontName:
  243. //|    This function will prompt the user for a font style and name.
  244. //| Parms:
  245. //|    in            szPipes                        Pointer to string to hold names
  246. //|    in            size                            Maximum size of pipe string
  247. //| Returns:              
  248. //|    Nothing
  249. //*---------------------------------------------------------------------------------
  250. void PromptForFontName(HWND hwndOwner)
  251. {
  252.     CHOOSEFONT        cf;
  253.     LOGFONT            lf;
  254.     HWND                fHwnd=GetFocus();
  255.     char                lpszStyle[LF_FACESIZE];
  256.     
  257.     memset(&lf, 0, sizeof(LOGFONT));
  258.     memset(&cf, 0, sizeof(CHOOSEFONT));
  259.     cf.lpLogFont = (LOGFONT FAR *)&lf;
  260.     cf.lpszStyle = (LPSTR)lpszStyle;
  261.     GetDefaultFont(&lf);
  262.  
  263.     cf.lStructSize = sizeof(CHOOSEFONT);
  264.     cf.hwndOwner = hwndOwner;
  265.     cf.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT;
  266.     cf.rgbColors = GetDefaultRGB();
  267.     cf.nFontType = SCREEN_FONTTYPE;
  268.     GetFontStyle(cf.lpszStyle, LF_FACESIZE);
  269.  
  270.     if(ChooseFont(&cf)) {
  271.         SetDefaultFont(&lf);
  272.         SetDefaultRGB(cf.rgbColors);
  273.         SetFontStyle(cf.lpszStyle);
  274.         }
  275.         
  276.     if(fHwnd)
  277.         SetFocus(fHwnd);
  278. }
  279.