Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
14 KiB

  1. //
  2. // ReadProfile.cpp - Routine to parse a windows Profile file and setup globals
  3. //
  4. #include "precomp.hxx"
  5. #include "global.h"
  6. #include "winspool.h"
  7. #include <Tchar.h>
  8. // Pre-defined section names
  9. #define SECTION_CONTROL "Control"
  10. #define SECTION_FONT "Font"
  11. #define SECTION_RENDER "Render"
  12. #define SECTION_API "API"
  13. #define SECTION_FONTLIST "FontList"
  14. #define SECTION_FONTHEIGHT "FontHeight"
  15. #define SECTION_AUTOFONTS "AutoFonts"
  16. #define SECTION_AUTOHEIGHTS "AutoHeights"
  17. #define SECTION_DRIVERSTRING "DriverString"
  18. // Maximum length for a profile value string...
  19. #define PROFILEVALUEMAX 4096
  20. // Enumeration of profile variable types
  21. typedef enum
  22. {
  23. epitInvalid = 0, // Invalid value
  24. epitBool = 1, // BOOL value
  25. epitInt = 2, // system integer (32 bits on x86) value
  26. epitFloat = 3, // single-precision floating point value
  27. epitDouble = 4, // double-precision floating point value
  28. epitString = 5, // ANSI string value
  29. epitAlign = 6, // StringAlignment value
  30. epitColor = 7 // RGBQUAD color
  31. } PROFILEINFOTYPE;
  32. // profile information structure
  33. typedef struct PROFILEINFO_tag
  34. {
  35. char szSection[80]; // Section of the profile to read value from
  36. char szVariable[80]; // Name of the variable
  37. PROFILEINFOTYPE type; // Type of the variable
  38. void *pvVariable; // void * to the variable (&g_Foo...)
  39. DWORD dwVariableLength; // size in bytes of the variable (sizeof(g_Foo...))
  40. } PROFILEINFO;
  41. ////////////////////////////////////////////////////////////////////////////////////////
  42. // Global profile info structure : Add to this table to get a variable from the .INI
  43. ////////////////////////////////////////////////////////////////////////////////////////
  44. PROFILEINFO g_rgProfileInfo[] =
  45. {
  46. { SECTION_CONTROL, "AutoDrive", epitBool, &g_AutoDrive, sizeof(g_AutoDrive) },
  47. { SECTION_CONTROL, "NumIterations", epitInt, &g_iNumIterations, sizeof(g_iNumIterations) },
  48. { SECTION_CONTROL, "NumRepaints", epitInt, &g_iNumRepaints, sizeof(g_iNumRepaints) },
  49. { SECTION_CONTROL, "NumRenders", epitInt, &g_iNumRenders, sizeof(g_iNumRenders) },
  50. { SECTION_CONTROL, "AutoFonts", epitBool, &g_AutoFont, sizeof(g_AutoFont) },
  51. { SECTION_CONTROL, "AutoHeight", epitBool, &g_AutoHeight, sizeof(g_AutoHeight) },
  52. { SECTION_CONTROL, "TextFile", epitString, &g_szSourceTextFile, sizeof(g_szSourceTextFile) },
  53. { SECTION_CONTROL, "FontOverride", epitBool, &g_FontOverride, sizeof(g_FontOverride) },
  54. { SECTION_API, "DrawString", epitBool, &g_ShowDrawString, sizeof(g_ShowDrawString) },
  55. { SECTION_API, "ShowDriver", epitBool, &g_ShowDriver, sizeof(g_ShowDriver) },
  56. { SECTION_API, "ShowPath", epitBool, &g_ShowPath, sizeof(g_ShowPath) },
  57. { SECTION_API, "ShowFamilies", epitBool, &g_ShowFamilies, sizeof(g_ShowFamilies) },
  58. { SECTION_API, "ShowGlyphs", epitBool, &g_ShowGlyphs, sizeof(g_ShowGlyphs) },
  59. { SECTION_API, "ShowMetric", epitBool, &g_ShowMetric, sizeof(g_ShowMetric) },
  60. { SECTION_API, "ShowGDI", epitBool, &g_ShowGDI, sizeof(g_ShowGDI) },
  61. { SECTION_API, "UseDrawText", epitBool, &g_UseDrawText, sizeof(g_UseDrawText) },
  62. { SECTION_FONT, "FaceName", epitString, &g_szFaceName, sizeof(g_szFaceName) },
  63. { SECTION_FONT, "Height", epitInt, &g_iFontHeight, sizeof(g_iFontHeight) },
  64. { SECTION_FONT, "Unit", epitInt, &g_fontUnit, sizeof(g_fontUnit) },
  65. { SECTION_FONT, "Typographic", epitBool, &g_typographic, sizeof(g_typographic) },
  66. { SECTION_FONT, "Bold", epitBool, &g_Bold, sizeof(g_Bold) },
  67. { SECTION_FONT, "Italic", epitBool, &g_Italic, sizeof(g_Italic) },
  68. { SECTION_FONT, "Underline", epitBool, &g_Underline, sizeof(g_Underline) },
  69. { SECTION_FONT, "Strikeout", epitBool, &g_Strikeout, sizeof(g_Strikeout) },
  70. { SECTION_RENDER, "TextMode", epitInt, &g_TextMode, sizeof(g_TextMode) },
  71. { SECTION_RENDER, "Align", epitAlign, &g_align, sizeof(g_align) },
  72. { SECTION_RENDER, "LineAlign", epitAlign, &g_lineAlign, sizeof(g_lineAlign) },
  73. { SECTION_RENDER, "HotKey", epitInt, &g_hotkey, sizeof(g_hotkey) },
  74. { SECTION_RENDER, "LineTrim", epitInt, &g_lineTrim, sizeof(g_lineTrim) },
  75. { SECTION_RENDER, "NoFitBB", epitBool, &g_NoFitBB, sizeof(g_NoFitBB) },
  76. { SECTION_RENDER, "NoWrap", epitBool, &g_NoWrap, sizeof(g_NoWrap) },
  77. { SECTION_RENDER, "NoClip", epitBool, &g_NoClip, sizeof(g_NoClip) },
  78. { SECTION_RENDER, "Offscreen", epitBool, &g_Offscreen, sizeof(g_Offscreen) },
  79. { SECTION_RENDER, "TextColor", epitColor, &g_TextColor, sizeof(g_TextColor) },
  80. { SECTION_RENDER, "BackColor", epitColor, &g_BackColor, sizeof(g_BackColor) },
  81. { SECTION_AUTOFONTS, "NumFonts", epitInt, &g_iAutoFonts, sizeof(g_iAutoFonts) },
  82. { SECTION_AUTOHEIGHTS, "NumHeights", epitInt, &g_iAutoHeights, sizeof(g_iAutoHeights) },
  83. { SECTION_DRIVERSTRING,"CMapLookup", epitBool, &g_CMapLookup, sizeof(g_CMapLookup) },
  84. { SECTION_DRIVERSTRING,"Vertical", epitBool, &g_Vertical, sizeof(g_Vertical) },
  85. { SECTION_DRIVERSTRING,"RealizedAdvance", epitBool, &g_RealizedAdvance, sizeof(g_RealizedAdvance) },
  86. { SECTION_DRIVERSTRING,"CompensateRes", epitBool, &g_CompensateRes, sizeof(g_CompensateRes) },
  87. { "INVALID" "INVALID", epitInvalid, NULL, 0 }
  88. };
  89. ////////////////////////////////////////////////////////////////////////////////////////
  90. // Routine to read the specified profile file (full-path required) and set the variables
  91. // defined in the above table based on the results.
  92. ////////////////////////////////////////////////////////////////////////////////////////
  93. void ReadProfileInfo(char *szProfileFile)
  94. {
  95. int iProfile =0;
  96. int iRead = 0;
  97. char szValue[PROFILEVALUEMAX];
  98. if (!szProfileFile)
  99. return;
  100. // Loop through the table of profile information...
  101. while(g_rgProfileInfo[iProfile].pvVariable != NULL)
  102. {
  103. void *pvValue = g_rgProfileInfo[iProfile].pvVariable;
  104. DWORD dwValueLength = g_rgProfileInfo[iProfile].dwVariableLength;
  105. // Read the profile string
  106. iRead = ::GetPrivateProfileStringA(
  107. g_rgProfileInfo[iProfile].szSection,
  108. g_rgProfileInfo[iProfile].szVariable,
  109. NULL,
  110. szValue,
  111. sizeof(szValue),
  112. szProfileFile);
  113. if (iRead > 0)
  114. {
  115. // Convert the string value to the proper variable type based on
  116. // the specified type in the table of profile information...
  117. switch(g_rgProfileInfo[iProfile].type)
  118. {
  119. case epitInvalid :
  120. {
  121. ASSERT(0);
  122. }
  123. break;
  124. case epitBool :
  125. {
  126. ASSERT(dwValueLength == sizeof(BOOL));
  127. // Only look at the first character for boolean values...
  128. if (szValue[0] == 'Y' || szValue[0] == 'y' || szValue[0] == 'T' || szValue[0] == 't' || szValue[0] == '1')
  129. {
  130. *((BOOL *)pvValue) = true;
  131. }
  132. else
  133. {
  134. *((BOOL *)pvValue) = false;
  135. }
  136. }
  137. break;
  138. case epitInt :
  139. {
  140. ASSERT(dwValueLength == sizeof(int));
  141. // Just use atoi here - strips whitespace and supports negative numbers...
  142. int iValue = atoi(szValue);
  143. *((int *)pvValue) = iValue;
  144. }
  145. break;
  146. case epitFloat :
  147. {
  148. ASSERT(dwValueLength == sizeof(float));
  149. // Just use atof here - strips whitespace...
  150. float fltValue = (float)atof(szValue);
  151. *((float *)pvValue) = fltValue;
  152. }
  153. break;
  154. case epitDouble :
  155. {
  156. ASSERT(dwValueLength == sizeof(double));
  157. // Just use atof here - strips whitespace...
  158. double dblValue = atof(szValue);
  159. *((double *)pvValue) = dblValue;
  160. }
  161. break;
  162. case epitString :
  163. {
  164. // Just use strncpy. NOTE : Truncates if necessary and does NOT support full UNICODE
  165. strncpy((char *)pvValue, szValue, dwValueLength);
  166. }
  167. break;
  168. case epitColor :
  169. {
  170. // We will only handle HEX color values here:
  171. int i;
  172. ARGB color = 0;
  173. for(i=0;i<8;i++)
  174. {
  175. if (szValue[i] == 0)
  176. break;
  177. // move along...
  178. color <<= 4;
  179. if (szValue[i] >= '0' && szValue[i] <= '9')
  180. {
  181. color += szValue[i] - '0';
  182. }
  183. else if (szValue[i] >='a' && szValue[i] <= 'f')
  184. {
  185. color += (szValue[i] - 'a') + 10;
  186. }
  187. else if (szValue[i] >='A' && szValue[i] <= 'F')
  188. {
  189. color += (szValue[i] - 'A') + 10;
  190. }
  191. }
  192. *((ARGB *)pvValue) = color;
  193. }
  194. break;
  195. case epitAlign :
  196. {
  197. ASSERT(dwValueLength == sizeof(StringAlignment));
  198. switch(szValue[0])
  199. {
  200. case 'n' :
  201. case 'N' :
  202. {
  203. // Near Alignment (left or top for US English)
  204. *((StringAlignment *)pvValue) = StringAlignmentNear;
  205. }
  206. break;
  207. case 'c' :
  208. case 'C' :
  209. {
  210. // Center Alignment
  211. *((StringAlignment *)pvValue) = StringAlignmentCenter;
  212. }
  213. break;
  214. case 'F' :
  215. case 'f' :
  216. {
  217. // Far Alignment (right or bottom for US English)
  218. *((StringAlignment *)pvValue) = StringAlignmentFar;
  219. }
  220. break;
  221. }
  222. }
  223. break;
  224. }
  225. }
  226. iProfile++;
  227. }
  228. // Get the enumerated fonts list (if any)
  229. if (g_AutoFont)
  230. {
  231. int iFont = 0;
  232. if (g_iAutoFonts > MAX_AUTO_FONTS)
  233. g_iAutoFonts = MAX_AUTO_FONTS;
  234. for(iFont=0;iFont<g_iAutoFonts;iFont++)
  235. {
  236. char szFontIndex[MAX_PATH];
  237. char szValue[MAX_PATH];
  238. wsprintfA(szFontIndex, "Font%d", iFont+1);
  239. // Read the profile string
  240. ::GetPrivateProfileStringA(
  241. SECTION_AUTOFONTS,
  242. szFontIndex,
  243. NULL,
  244. szValue,
  245. sizeof(g_rgszAutoFontFacenames[iFont]),
  246. szProfileFile);
  247. #ifdef UNICODE
  248. MultiByteToWideChar( CP_ACP,
  249. 0,
  250. szValue,
  251. -1,
  252. g_rgszAutoFontFacenames[iFont],
  253. lstrlenA(szValue) );
  254. #else
  255. strcpy(g_rgszAutoFontFacenames[iFont], szValue);
  256. #endif
  257. }
  258. }
  259. // Get the enumerated font heights (if any)
  260. if (g_AutoHeight)
  261. {
  262. int iHeight = 0;
  263. if (g_iAutoHeights > MAX_AUTO_HEIGHTS)
  264. g_iAutoHeights = MAX_AUTO_HEIGHTS;
  265. for(iHeight=0;iHeight<g_iAutoHeights;iHeight++)
  266. {
  267. char szHeightIndex[MAX_PATH];
  268. char szValue[MAX_PATH];
  269. wsprintfA(szHeightIndex, "Height%d", iHeight+1);
  270. // Read the profile string
  271. ::GetPrivateProfileStringA(
  272. SECTION_AUTOHEIGHTS,
  273. szHeightIndex,
  274. NULL,
  275. szValue,
  276. sizeof(szValue),
  277. szProfileFile);
  278. g_rgiAutoHeights[iHeight] = atoi(szValue);
  279. }
  280. }
  281. // Combine various booleans into proper bit-flags
  282. g_DriverOptions =
  283. (g_CMapLookup ? DriverStringOptionsCmapLookup : 0) |
  284. (g_Vertical ? DriverStringOptionsVertical : 0) |
  285. (g_RealizedAdvance ? DriverStringOptionsRealizedAdvance : 0)
  286. ;
  287. g_formatFlags =
  288. (g_NoFitBB ? StringFormatFlagsNoFitBlackBox : 0) |
  289. (g_NoWrap ? StringFormatFlagsNoWrap : 0) |
  290. (g_NoClip ? StringFormatFlagsNoClip : 0);
  291. }