Annotation of mstools/samples/sdktools/porttool/port.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: #include <windows.h>
                     13: #include <string.h>
                     14: #include <stdlib.h>
                     15: #include "portpriv.h"
                     16: #include "port.h"
                     17: 
                     18: /* globals for this module */
                     19: HANDLE    hMMFile = 0;
                     20: 
                     21: /* function prototypes for private module functions */
                     22: BOOL WINAPI InitPortData (HANDLE);
                     23: void WINAPI FreePortData ();
                     24: int  WINAPI GetNextToken (LPPORT *);
                     25: void WINAPI IgnoreToken (char *, LPPORT);
                     26: BOOL WINAPI LoadSection (char *, char *, DWORD, int *, char *);
                     27: BOOL WINAPI GetIniFile (HANDLE, char *);
                     28: 
                     29: 
                     30: /* entry point for DLL loading and unloading */
                     31: #if 1
                     32: BOOL WINAPI DllMain (
                     33: #else
                     34: BOOL WINAPI DLLEntry (
                     35: #endif
                     36: 
                     37:     HANDLE    hModule,
                     38:     DWORD     dwFunction,
                     39:     LPVOID    lpNot)
                     40: {
                     41: #ifdef DEBUG
                     42: DebugBreak ();
                     43: #endif
                     44: 
                     45:     switch (dwFunction)
                     46:     {
                     47:     case DLL_PROCESS_ATTACH:
                     48:         if (!InitPortData (hModule))
                     49:         {
                     50:         /* display error loading DLL data and exit process */
                     51:         MessageBox (NULL, "Error loading port information, check file PORT.INI",
                     52:                 "PORT.DLL", MB_OK);
                     53:         return FALSE;
                     54:         }
                     55:         break;
                     56: 
                     57:     case DLL_PROCESS_DETACH:
                     58:         FreePortData ();
                     59:     default:
                     60:         break;
                     61:     }
                     62: 
                     63:     return TRUE;
                     64: }
                     65: 
                     66: 
                     67: 
                     68: /* function initializes port structures */
                     69: BOOL WINAPI InitPortData (
                     70:     HANDLE    hDLL)
                     71: {
                     72:     char    szSection[MAX_PATH];
                     73:     char    szIniFilePath[MAX_PATH];
                     74:     char    szMapFileName[MAX_PATH];
                     75:     OFSTRUCT    of;
                     76:     HANDLE  hFile;
                     77:     DWORD   nFileSize;
                     78:     int     nOffset = 0;
                     79:     char    *lpMMFile;
                     80: 
                     81:     /* load name for global file mapping */
                     82:     LoadString (hDLL, IDS_MAPFILENAME, szMapFileName, MAX_PATH);
                     83: 
                     84:     /* after first process initializes port data */
                     85:     if ((hMMFile = OpenFileMapping (FILE_MAP_WRITE, FALSE, szMapFileName)))
                     86:     /* exit now since initialization was already performed by another process */
                     87:     return TRUE;
                     88: 
                     89:     /* retrive path and file for ini file */
                     90:     if (!GetIniFile (hDLL, szIniFilePath))
                     91:     return FALSE;
                     92: 
                     93:     /* test for ini file existance and get length of file */
                     94:     if ((int)(hFile = (HANDLE)OpenFile (szIniFilePath, &of, OF_READ)) == -1)
                     95:     return FALSE;
                     96:     else
                     97:     {
                     98:     nFileSize = GetFileSize (hFile, NULL);
                     99:     CloseHandle (hFile);
                    100:     }
                    101: 
                    102:     /* allocate a segment of the swap file for shared memory 2*Size of ini file */
                    103:     if (!(hMMFile = CreateFileMapping ((HANDLE)0xffffffff,
                    104:                    NULL,
                    105:                    PAGE_READWRITE,
                    106:                    0,
                    107:                    nFileSize * 2,
                    108:                    szMapFileName)))
                    109:     return FALSE;
                    110: 
                    111:     /* map a view of this file for writing */
                    112:     lpMMFile = (char *)MapViewOfFile (hMMFile, FILE_MAP_WRITE, 0, 0, 0);
                    113: 
                    114:     /* load tokens for APIS section */
                    115:     LoadString (hDLL, IDS_PORTAPIS, szSection, MAX_PATH);
                    116:     if (!LoadSection (szIniFilePath, szSection, PT_APIS, &nOffset, lpMMFile))
                    117:     {
                    118:     /* clean up memory mapped file */
                    119:     UnmapViewOfFile (lpMMFile);
                    120:     CloseHandle (hMMFile);
                    121:     return FALSE;
                    122:     }
                    123: 
                    124:     /* load tokens for MESSAGES section */
                    125:     LoadString (hDLL, IDS_PORTMESSAGES, szSection, MAX_PATH);
                    126:     if (!LoadSection (szIniFilePath, szSection, PT_MESSAGES, &nOffset, lpMMFile))
                    127:     {
                    128:     /* clean up memory mapped file */
                    129:     UnmapViewOfFile (lpMMFile);
                    130:     CloseHandle (hMMFile);
                    131:     return FALSE;
                    132:     }
                    133: 
                    134:     /* load tokens for STRUCTURES section */
                    135:     LoadString (hDLL, IDS_PORTSTRUCTURES, szSection, MAX_PATH);
                    136:     if (!LoadSection (szIniFilePath, szSection, PT_STRUCTURES, &nOffset, lpMMFile))
                    137:     {
                    138:     /* clean up memory mapped file */
                    139:     UnmapViewOfFile (lpMMFile);
                    140:     CloseHandle (hMMFile);
                    141:     return FALSE;
                    142:     }
                    143: 
                    144:     /* load tokens for TYPES section */
                    145:     LoadString (hDLL, IDS_PORTTYPES, szSection, MAX_PATH);
                    146:     if (!LoadSection (szIniFilePath, szSection, PT_TYPES, &nOffset, lpMMFile))
                    147:     {
                    148:     /* clean up memory mapped file */
                    149:     UnmapViewOfFile (lpMMFile);
                    150:     CloseHandle (hMMFile);
                    151:     return FALSE;
                    152:     }
                    153: 
                    154:     /* load tokens for MACROS section */
                    155:     LoadString (hDLL, IDS_PORTMACROS, szSection, MAX_PATH);
                    156:     if (!LoadSection (szIniFilePath, szSection, PT_MACROS, &nOffset, lpMMFile))
                    157:     {
                    158:     /* clean up memory mapped file */
                    159:     UnmapViewOfFile (lpMMFile);
                    160:     CloseHandle (hMMFile);
                    161:     return FALSE;
                    162:     }
                    163: 
                    164:     /* load tokens for CONSTANTS section */
                    165:     LoadString (hDLL, IDS_PORTCONSTANTS, szSection, MAX_PATH);
                    166:     if (!LoadSection (szIniFilePath, szSection, PT_CONSTANTS, &nOffset, lpMMFile))
                    167:     {
                    168:     /* clean up memory mapped file */
                    169:     UnmapViewOfFile (lpMMFile);
                    170:     CloseHandle (hMMFile);
                    171:     return FALSE;
                    172:     }
                    173: 
                    174:     /* load tokens for CUSTOM section */
                    175:     LoadString (hDLL, IDS_PORTCUSTOM, szSection, MAX_PATH);
                    176:     if (!LoadSection (szIniFilePath, szSection, PT_CUSTOM, &nOffset, lpMMFile))
                    177:     {
                    178:     /* clean up memory mapped file */
                    179:     UnmapViewOfFile (lpMMFile);
                    180:     CloseHandle (hMMFile);
                    181:     return FALSE;
                    182:     }
                    183: 
                    184:     /* release WRITE view of memory mapped file */
                    185:     UnmapViewOfFile (lpMMFile);
                    186: 
                    187:     /* success */
                    188:     return TRUE;
                    189: }
                    190: 
                    191: 
                    192: 
                    193: /* release memory mapped file view */
                    194: void WINAPI FreePortData ()
                    195:     {
                    196:     /* release memory mapped file */
                    197:     CloseHandle (hMMFile);
                    198:     }
                    199: 
                    200: 
                    201: 
                    202: /* external function to check a string for porting issues */
                    203: BOOL WINAPI CheckString (
                    204:     char    *lpszSrc,
                    205:     DWORD   dwSearch,
                    206:     LPRESULT    lprIssue)
                    207: {
                    208:     BOOL      bRet = FALSE;
                    209:     LPPORT    lpToken;
                    210:     char      *lpStr = lpszSrc;
                    211:     int       nSrcLen = strlen (lpszSrc);
                    212:     int       nTokLen;
                    213:     char      *lpMMFile = (char *)MapViewOfFile (hMMFile, FILE_MAP_WRITE, 0, 0, 0);
                    214: 
                    215: 
                    216:     /* if view of file failed */
                    217:     if (!lpMMFile)
                    218:     return FALSE;
                    219: 
                    220:     /* if ignore token */
                    221:     if (dwSearch & PT_IGNORETOKEN)
                    222:     /* flag token as ignored */
                    223:     IgnoreToken (lpszSrc, (LPPORT)lpMMFile);
                    224: 
                    225:     else
                    226:     /* loop through all characters in string */
                    227:     while ((lpStr-lpszSrc) < nSrcLen)
                    228:         {
                    229:         /* initialize lpToken to beginning of list */
                    230:         lpToken = (LPPORT)lpMMFile;
                    231: 
                    232:         /* loop thru all tokens */
                    233:         while ((nTokLen = GetNextToken (&lpToken)))
                    234:         {
                    235:         /* filter tokens for search criteria */
                    236:         if (!(lpToken->dwType & PT_IGNORED) &&
                    237:             !(dwSearch & lpToken->dwType) &&
                    238:             ((dwSearch & PT_IGNORECASE &&
                    239:               !strnicmp ((char *)lpToken+lpToken->nPosToken, lpStr, nTokLen)) ||
                    240:              !strncmp ((char *)lpToken+lpToken->nPosToken, lpStr, nTokLen)))
                    241:             {
                    242:             /* token found in line, return ISSUE struct */
                    243:             strncpy (lprIssue->lpszToken,
                    244:                  (char *)lpToken+lpToken->nPosToken,
                    245:                  *(WORD *)lprIssue->lpszToken);
                    246:             strncpy (lprIssue->lpszHelpStr,
                    247:                  (char *)lpToken+lpToken->nPosHelpStr,
                    248:                  *(WORD *)lprIssue->lpszHelpStr);
                    249:             strncpy (lprIssue->lpszIssue,
                    250:                  (char *)lpToken+lpToken->nPosIssue,
                    251:                  *(WORD *)lprIssue->lpszIssue);
                    252:             strncpy (lprIssue->lpszSuggest,
                    253:                  (char *)lpToken+lpToken->nPosSuggest,
                    254:                  *(WORD *)lprIssue->lpszSuggest);
                    255:             lprIssue->nPosToken = (int)(lpStr - lpszSrc);
                    256: 
                    257:             bRet = TRUE;
                    258:             goto DONE;
                    259:             }
                    260:         }
                    261: 
                    262:         lpStr++;
                    263:         }
                    264: 
                    265: DONE:
                    266: 
                    267:     /* unmap view of memory mapped file */
                    268:     UnmapViewOfFile (lpMMFile);
                    269: 
                    270:     return bRet;
                    271: }
                    272: 
                    273: 
                    274: 
                    275: 
                    276: /* function get's the next token in the list */
                    277: int WINAPI GetNextToken (
                    278:     LPPORT    *lpToken)
                    279: {
                    280:     /* increment to next non-ignored token in list */
                    281:     do
                    282:     (char *)*lpToken += ((LPPORT)*lpToken)->nSize;
                    283:     while (((((LPPORT)*lpToken)->dwType) == PT_IGNORED) &&
                    284:        ((((LPPORT)*lpToken)->nSize) != 0));
                    285: 
                    286:     /* if at end of list, reset list to null */
                    287:     if ((((LPPORT)*lpToken)->nSize) == 0)
                    288:     {
                    289:     *lpToken = 0;
                    290:     return 0;
                    291:     }
                    292: 
                    293:     /* return length of token */
                    294:     return (strlen ((char *)*lpToken + ((LPPORT)*lpToken)->nPosToken));
                    295: }
                    296: 
                    297: 
                    298: 
                    299: /* function sets the ignore flag on the specified token */
                    300: void WINAPI IgnoreToken (
                    301:     char    *lpszToken,
                    302:     LPPORT  lpToken)
                    303: {
                    304:     /* search for token in list */
                    305:     while (lpToken->nSize != 0)
                    306:     /* if same token */
                    307:     if (!strcmp ((char *)((char *)lpToken + lpToken->nPosToken), lpszToken))
                    308:         {
                    309:         lpToken->dwType |= PT_IGNORED;
                    310:         break;
                    311:         }
                    312:     /* increment to  next token */
                    313:     else
                    314:         (char *)lpToken += lpToken->nSize;
                    315: }
                    316: 
                    317: 
                    318: 
                    319: 
                    320: /* load tokens from a section of ini file */
                    321: BOOL WINAPI LoadSection (
                    322:     char    *lpszIniFile,
                    323:     char    *lpszSection,
                    324:     DWORD   dwType,
                    325:     int     *nOffset,
                    326:     char    *lpMMFile)
                    327: {
                    328:     char    *lpszKeyNames;
                    329:     char    *lpKey;
                    330:     char    *lpszValue;
                    331:     char    *lpVal;
                    332:     char    *lpszToken;
                    333:     char    lpszDefault[] = "Default";
                    334:     char    *lpMem = lpMMFile + *nOffset;
                    335:     int     nList;
                    336: 
                    337: 
                    338:     /* allocate lots of memory off heap to save calling applications' stack */
                    339:     if (!(lpszKeyNames = (char *)LocalAlloc (LPTR, TEN_K_LINE)))
                    340:     return FALSE;
                    341:     if (!(lpszValue = (char *)LocalAlloc (LPTR, TWO_K_LINE)))
                    342:     {
                    343:     LocalFree ((HLOCAL)lpszKeyNames);
                    344:     return FALSE;
                    345:     }
                    346:     if (!(lpszToken = (char *)LocalAlloc (LPTR, MAXTOKENLEN)))
                    347:     {
                    348:     LocalFree ((HLOCAL)lpszKeyNames);
                    349:     LocalFree ((HLOCAL)lpszValue);
                    350:     return FALSE;
                    351:     }
                    352: 
                    353:     /* get all keynames in section */
                    354:     if (((nList = GetPrivateProfileString (lpszSection,
                    355:                        NULL,
                    356:                        lpszDefault,
                    357:                        lpszKeyNames,
                    358:                        TEN_K_LINE,
                    359:                        lpszIniFile)) == (int)(strlen (lpszDefault))) &&
                    360:     !strcmp (lpszDefault, lpszKeyNames))
                    361:     {
                    362:     LocalFree ((HLOCAL)lpszKeyNames);
                    363:     LocalFree ((HLOCAL)lpszValue);
                    364:     LocalFree ((HLOCAL)lpszToken);
                    365:     return FALSE;
                    366:     }
                    367: 
                    368:     /* initialize token pointer and first token */
                    369:     lpKey = lpszKeyNames;
                    370: 
                    371:     /* loop through all keynames */
                    372:     while (TRUE)
                    373:     {
                    374:     /* get next token */
                    375:     strcpy (lpszToken, lpKey);
                    376: 
                    377:     /* get value for token */
                    378:     if ((GetPrivateProfileString (lpszSection,
                    379:                      lpszToken,
                    380:                      lpszDefault,
                    381:                      lpszValue,
                    382:                      TWO_K_LINE,
                    383:                      lpszIniFile) == strlen (lpszDefault)) &&
                    384:         !strcmp (lpszDefault, lpszValue))
                    385:         {
                    386:         LocalFree ((HLOCAL)lpszKeyNames);
                    387:         LocalFree ((HLOCAL)lpszValue);
                    388:         LocalFree ((HLOCAL)lpszToken);
                    389:         return FALSE;
                    390:         }
                    391:     else
                    392:         {
                    393:         /* break line up into components */
                    394:         ((LPPORT)lpMem)->nPosToken = sizeof (PORT);
                    395:         strcpy ((char *)(lpMem + ((LPPORT)lpMem)->nPosToken), lpszToken);
                    396: 
                    397:         ((LPPORT)lpMem)->nPosHelpStr =
                    398:             ((LPPORT)lpMem)->nPosToken + strlen ((char *)(lpMem + ((LPPORT)lpMem)->nPosToken)) + 1;
                    399:         if (lpVal = strtok (lpszValue, ";"))
                    400:         strcpy ((char *)(lpMem + ((LPPORT)lpMem)->nPosHelpStr), lpVal);
                    401:         else
                    402:         *(lpMem + ((LPPORT)lpMem)->nPosHelpStr) = 0;
                    403: 
                    404:         ((LPPORT)lpMem)->nPosIssue =
                    405:             ((LPPORT)lpMem)->nPosHelpStr + strlen ((char *)(lpMem + ((LPPORT)lpMem)->nPosHelpStr)) + 1;
                    406:         if (lpVal = strtok (NULL, ";"))
                    407:         strcpy ((char *)(lpMem + ((LPPORT)lpMem)->nPosIssue), lpVal);
                    408:         else
                    409:         *(lpMem + ((LPPORT)lpMem)->nPosIssue) = 0;
                    410: 
                    411:         ((LPPORT)lpMem)->nPosSuggest =
                    412:             ((LPPORT)lpMem)->nPosIssue + strlen ((char *)(lpMem + ((LPPORT)lpMem)->nPosIssue)) + 1;
                    413:         if (lpVal = strtok (NULL, ";"))
                    414:         strcpy ((char *)(lpMem + ((LPPORT)lpMem)->nPosSuggest), lpVal);
                    415:         else
                    416:         *(lpMem + ((LPPORT)lpMem)->nPosSuggest) = 0;
                    417: 
                    418:         /* set size of dynamic token structure */
                    419:         ((LPPORT)lpMem)->nSize = (((LPPORT)lpMem)->nPosSuggest +
                    420:          strlen ((char *)(lpMem + ((LPPORT)lpMem)->nPosSuggest)) + 1);
                    421: 
                    422:         /* adjust nSize for DWORD alignment */
                    423:             ((LPPORT)lpMem)->nSize = ((((LPPORT)lpMem)->nSize) + 3) & ~3;
                    424: 
                    425:         /* set token type */
                    426:         ((LPPORT)lpMem)->dwType = dwType;
                    427: 
                    428:         /* adjust list pointer to point beginning of next list element */
                    429:         lpMem += ((LPPORT)lpMem)->nSize;
                    430:         }
                    431: 
                    432:     /* increment token pointer and test for end of list */
                    433:     if (((lpKey += strlen (lpszToken) + 1) - lpszKeyNames) >= (nList - 1))
                    434:         {
                    435:         /* indicate end of list by setting size of next element to 0 */
                    436:         ((LPPORT)lpMem)->nSize = 0;
                    437:         break;
                    438:         }
                    439:     }
                    440: 
                    441:     /* got to end of section, clean up and go away */
                    442:     LocalFree ((HLOCAL)lpszKeyNames);
                    443:     LocalFree ((HLOCAL)lpszValue);
                    444:     LocalFree ((HLOCAL)lpszToken);
                    445: 
                    446:     /* recalculate offset */
                    447:     *nOffset = lpMem - lpMMFile;
                    448: 
                    449:     /* return successful load */
                    450:     return TRUE;
                    451: }
                    452: 
                    453: 
                    454: 
                    455: /* retrieve ini file and path */
                    456: BOOL WINAPI GetIniFile (
                    457:     HANDLE  hDLL,
                    458:     char    *lpszFile)
                    459: {
                    460:     char    lpszPath[MAX_PATH];
                    461:     char    *lpPath;
                    462:     char    lpszFileName[MAX_PATH];
                    463:     OFSTRUCT    of;
                    464: 
                    465:     /* get module directory and path */
                    466:     GetModuleFileName (hDLL, lpszPath, MAX_PATH);
                    467:     lpPath = lpszPath + strlen (lpszPath);
                    468: 
                    469:     /* find end of path by searching backwards from end to first '\' or ':' */
                    470:     while (lpPath > lpszPath)
                    471:     {
                    472:     if (*lpPath == '\\' ||
                    473:         *lpPath == ':')
                    474:         {
                    475:         lpPath++;
                    476:         break;
                    477:         }
                    478:     lpPath--;
                    479:     }
                    480: 
                    481:     /* terminate at end of path */
                    482:     *lpPath = 0;
                    483: 
                    484:     /* append ini filename to path */
                    485:     LoadString (hDLL, IDS_INIFILE, lpszFileName, MAX_PATH);
                    486:     strcat (lpPath, lpszFileName);
                    487: 
                    488:     /* test for existance */
                    489:     if (!(OpenFile (lpszPath, &of, OF_EXIST)))
                    490:     {
                    491:     GetWindowsDirectory (lpszPath, MAX_PATH);
                    492:     strcat (lpszPath, lpszFileName);
                    493:     if (!(OpenFile (lpszPath, &of, OF_EXIST)))
                    494:         return FALSE;
                    495:     else
                    496:         {
                    497:         strcpy (lpszFile, lpszPath);
                    498:         return TRUE;
                    499:         }
                    500:     }
                    501:     else
                    502:     {
                    503:     strcpy (lpszFile, lpszPath);
                    504:     return TRUE;
                    505:     }
                    506: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.