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

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

unix.superglobalmegacorp.com

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