收录查询

始终闹不明白-------main()函数中的2个[in]参数到底有什么程度上的深刻意义和如何熟练使用它们

先看看MSDN中对main函数的说明:

Arguments to main

ANSI 2.1.2.2.1   The semantics of the arguments to main

In Microsoft C, the function called at program startup is called main. There is no prototype declared for main, and it can be defined with zero, two, or three parameters:

int main( void )
int main( int argc, char *argv[] )
int main( int argc, char *argv[], char *envp[] )

The third line above, where main accepts three parameters, is a Microsoft extension to the ANSI C standard. The third parameter, envp, is an array of pointers to environment variables. The envp array is terminated by a null pointer. See The main Function and Program Execution in Chapter 2 for more information about main and envp.

The variable argc never holds a negative value.

The array of strings ends with argv[argc], which contains a null pointer.

All elements of the argv array are pointers to strings.

A program invoked with no command-line arguments will receive a value of one for argc, as the name of the executable file is placed in argv[0]. (In MS-DOS versions prior to 3.0, the executable-file name is not available. The letter “C” is placed in argv[0].) Strings pointed to by argv[1] through argv[argc – 1] represent program parameters.

The parameters argc and argv are modifiable and retain their last-stored values between program startup and program termination.
===============================================
原文地址: http://www.oldlinux.org/Linux.old/Linux-0.12/sources/sbin/hostname.c

 

 1/*
 2* Set or display hostname.  Jeff Comstock - Bloomington, MN USA 1992
 3* Usage: hostname [name]
 4* Only root may change the hostname.
 5*/

 6#include <stdio.h>
 7#include <unistd.h>
 8
 9main(int argc, char **argv) {
10    struct utsname uts;
11    
12    if ( argc == 2 ) {
13        if ( sethostname(argv[1],strlen(argv[1]))) {
14            perror("sethostname");
15            exit(1);
16        }

17    }

18    else {
19        if (uname(&uts)) {
20            perror("uname");
21            exit(1);
22        }

23        else 
24            puts(uts.nodename);
25    }

26    return(0);
27}

 

================================================
再看一段MS的代码:

  1/****************************************************************************/
  2DWORD __cdecl
  3main (
  4    IN SHORT argc,
  5    IN PSZ argv[],
  6    IN PSZ envp[]
  7    )
  8/*++
  9*
 10* Routine Description:
 11*
 12*
 13*
 14* Arguments:
 15*
 16*
 17*
 18*
 19* Return Value:
 20*
 21*
 22*
 23--*/

 24/****************************************************************************/
 25{
 26
 27    DWORD   ReadFlags;
 28
 29    Initialize();           // Init any data
 30
 31    if ( argc < 2 ) {
 32        printf( "Not enough parameters\n" );
 33        return Usage( );
 34    }

 35
 36    if ( stricmp( argv[1], "-c" ) == 0 ) {
 37
 38        if ( argc < 3 ) {
 39            return TestClearLogFile();
 40        }

 41    }

 42    else if ( stricmp( argv[1], "-b" ) == 0 ) {
 43
 44        if ( argc < 3 ) {
 45            printf("You must supply a filename to backup to\n");
 46            return(FALSE);
 47        }

 48
 49            return TestBackupLogFile(argv[2]);
 50
 51    }
 else if (stricmp ( argv[1], "-rsf" ) == 0 ) {
 52
 53        ReadFlags = EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ;
 54        if ( argc < 3 ) {
 55            return TestReadEventLog(1,ReadFlags,0 );
 56        }
 else  {
 57            return Usage();
 58        }

 59    }
 else if (stricmp ( argv[1], "-rsb" ) == 0 ) {
 60
 61        ReadFlags = EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ;
 62        if ( argc < 3 ) {
 63            return TestReadEventLog(1,ReadFlags,0 );
 64        }
 else  {
 65            return Usage();
 66        }

 67    }
 else if (stricmp ( argv[1], "-n" ) == 0 ) {
 68        TestChangeNotify();
 69
 70    }
 else if (stricmp ( argv[1], "-rrf" ) == 0 ) {
 71
 72        ReadFlags = EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ;
 73        if ( argc < 3 ) {
 74            return TestReadEventLog(1,ReadFlags ,1);
 75        }
 else if (argc == 3{
 76            return (TestReadEventLog (1, ReadFlags, atoi(argv[2])));
 77        }

 78    }
 else if (stricmp ( argv[1], "-rrb" ) == 0 ) {
 79
 80        ReadFlags = EVENTLOG_SEEK_READ | EVENTLOG_BACKWARDS_READ;
 81        if ( argc < 3 ) {
 82            return TestReadEventLog(1,ReadFlags, 1);
 83        }
 else if (argc == 3{
 84            return (TestReadEventLog (1, ReadFlags, atoi(argv[2])));
 85        }

 86    }
 else if (stricmp ( argv[1], "-w" ) == 0 ) {
 87
 88        if ( argc < 3 ) {
 89            return TestWriteEventLog(1);
 90        }
 else if (argc == 3{
 91            return (TestWriteEventLog (atoi(argv[2])));
 92        }

 93
 94    }
 else {
 95
 96        return Usage();
 97    }

 98
 99    UNREFERENCED_PARAMETER(argc);
100    UNREFERENCED_PARAMETER(argv);
101    UNREFERENCED_PARAMETER(envp);
102
103
104}

105
106


============================================================

  1//+---------------------------------------------------------------------------
  2//
  3//  Microsoft Forms
  4//  Copyright (C) Microsoft Corporation, 1994-1997
  5//
  6//  File:       ascparse.cxx
  7//
  8//  Contents:   Tool to build .hsc files from .asc files.
  9//
 10//              Does the work of precomputing hash tables for associative
 11//              arrays of strings.
 12//
 13//----------------------------------------------------------------------------
 14
 15#define INCMSG(x)
 16#include "headers.hxx"
 17
 18#ifndef X_LIMITS_H_
 19#define X_LIMITS_H_
 20#include <limits.h>
 21#endif
 22
 23#ifndef X_PLATFORM_H_
 24#define X_PLATFORM_H_
 25#include <platform.h>
 26#endif
 27
 28#ifndef X_MSHTMDBG_H_
 29#define X_MSHTMDBG_H_
 30#undef PERFMETER
 31#include <mshtmdbg.h>
 32#endif
 33
 34#define ASCPARSE
 35
 36// The following macro definitions allow us to use assoc.cxx
 37// without bringing in the whole CORE directory
 38
 39#ifndef X_TCHAR_H_
 40#define X_TCHAR_H_
 41#include "tchar.h"
 42#endif
 43
 44#define THR(x) (x)
 45#define RRETURN(x) return(x)
 46#define _MemAlloc(cb) malloc(cb)
 47#define _MemAllocClear(cb) calloc(1,cb)
 48#define _MemFree(x) free(x)
 49#define MemAlloc(mt,cb) _MemAlloc(cb)
 50#define MemAllocClear(mt,cb) _MemAllocClear(cb)
 51#define MemFree(x) _MemFree(x)
 52#define MemRealloc(mt, ppv, cb) _MemRealloc(ppv, cb)
 53#define _tcsequal(x,y) (!_tcscmp(x,y))
 54#define Assert(x) if (!(x)) { fprintf(stderr, "%s", #x); exit(1); }
 55#define Verify(x) if (!(x)) { fprintf(stderr, "%s", #x); exit(1); }
 56#define ARRAY_SIZE(x)   (sizeof(x) / sizeof(x[0]))
 57
 58HRESULT 
 59_MemRealloc(void **ppv, size_t cb)
 60{
 61    void *pv;
 62
 63    if (*ppv == NULL)
 64    {
 65        *ppv = _MemAlloc(cb);
 66        if (*ppv == NULL)
 67            return E_OUTOFMEMORY;
 68    }

 69    else
 70    {       
 71        pv = realloc(*ppv, cb);
 72        if (pv == NULL)
 73            return E_OUTOFMEMORY;
 74        *ppv = pv;
 75    }

 76    return S_OK;
 77}
;
 78
 79void GetSuffix(LPCSTR pAssocString, LPSTR pSuffix, int nNumber)
 80{
 81    pSuffix[0= '\0'// NULL terminate the suffix
 82    // check if the First Character is a Captial letter.
 83    if ( islower(pAssocString[0]) )
 84        return;
 85
 86    _itoa(nNumber, pSuffix, 10);
 87}

 88
 89#include "assoc.cxx"
 90
 91// end of stubs
 92
 93
 94#define MAX_WORD 64
 95#define MAX_LINE 4096
 96
 97class CAscParser
 98{
 99public:
100    CAscParser() { memset(this0sizeof(*this)); }
101    
102    class CAscEntry {
103    public:
104        CAscEntry() { memset(this0sizeof(*this)); }
105        CAscEntry *_pEntryNext;
106        char _achString[MAX_WORD];
107        int  _number;
108        char _achNumber[MAX_WORD];
109        char _achStringName[MAX_WORD];
110        char _achAssoc[MAX_WORD];
111        char _achEnum[MAX_WORD];
112        BOOL _fNoassoc;
113        BOOL _fNostring;
114        BOOL _fNoenum;
115        CAssoc *_pAssoc;
116    }
;
117
118    HRESULT ProcessAscFile(char *pchInputFile, char *pchOutputFile);
119    char _achAssocArray[MAX_WORD];
120    char _achAssocPrefix[MAX_WORD];
121    char _achEnumType[MAX_WORD];
122    char _achEnumPrefix[MAX_WORD];
123    char _achStringNamePrefix[MAX_WORD];
124    BOOL _fInsensitive;
125    BOOL _fReversible;
126    CAscEntry *_pEntryFirst;
127    CAscEntry *_pEntryLast;
128}
;
129    
130static BOOL ReadLine(FILE *fp, char *pchBuf, int cchBuf, int *pcchRead);
131static void SkipSpace(char **ppch);
132static void SkipNonspace(char **ppch);
133static void ChopComment(char *pch);
134static void GetWord(char **ppch, char **ppchWord);
135
136
137int __cdecl
138main  ( int argc, char *argv[] )
139{
140    HRESULT hr = E_FAIL;
141    CAscParser np;
142    
143    if (argc != 3)
144        goto Cleanup;
145
146    hr = np.ProcessAscFile(argv[1], argv[2]);
147
148Cleanup:
149
150    if (hr)
151        printf ( "Error %lx building ASC file\n", hr);
152    exit(hr);
153    return hr;
154}

155
156HRESULT
157CAscParser::ProcessAscFile(char *pchInputFile, char *pchOutputFile)
158{
159    HRESULT hr;
160    FILE   *fpInput = NULL;
161    FILE   *fpOutput = NULL;
162    char    achBuf[MAX_LINE];
163    char   *pch;
164    char   *pchWord;
165    CAscEntry *pEntryNew;
166    CAscEntry *pEntry;
167    CAssocArray nt;
168
169    nt.Init();
170    _fReversible = FALSE;
171
172    // open input file
173    fpInput = fopen(pchInputFile, "r");
174    if (!fpInput)
175    {
176        hr = E_FAIL;
177        goto Cleanup;
178    }

179
180    // open output file
181    fpOutput = fopen(pchOutputFile, "w");
182    if (!fpOutput)
183    {
184        hr = E_FAIL;
185        goto Cleanup;
186    }

187
188    // phase 1: read the header section
189    hr = E_FAIL;
190    
191    for (;;)
192    {
193        if (!ReadLine(fpInput, achBuf, MAX_LINE, NULL))
194            goto Cleanup;
195        pch = achBuf;
196        ChopComment(pch);
197        GetWord(&pch, &pchWord);
198        if (!*pchWord)
199            continue;
200        if (!strcmp(pchWord, "assocarray"))
201        {
202            GetWord(&pch, &pchWord);
203            strcpy(_achAssocArray, pchWord);
204            GetWord(&pch, &pchWord);
205            strcpy(_achAssocPrefix, pchWord);
206        }

207        else
208        if (!strcmp(pchWord, "enum"))
209        {
210            GetWord(&pch, &pchWord);
211            strcpy(_achEnumType, pchWord);
212            GetWord(&pch, &pchWord);
213            strcpy(_achEnumPrefix, pchWord);
214        }

215        else
216        if (!strcmp(pchWord, "string"))
217        {
218            GetWord(&pch, &pchWord);
219            strcpy(_achStringNamePrefix, pchWord);
220        }

221        else
222        if (!strcmp(pchWord, "case-insensitive"))
223        {
224            _fInsensitive = TRUE;
225        }

226        else
227        if (!strcmp(pchWord, "case-sensitive"))
228        {
229            _fInsensitive = FALSE;
230        }

231        else
232        if (!strcmp(pchWord, "reversible"))
233        {
234            _fReversible = TRUE;
235        }

236        else
237        if (!strcmp(pchWord, "start"))
238            break;
239    }

240    
241    // phase 2: read the assoc table section
242    hr = S_OK;
243    
244    while (ReadLine(fpInput, achBuf, MAX_LINE, NULL))
245    {
246        pch = achBuf;
247        
248        ChopComment(pch);
249        GetWord(&pch, &pchWord);
250        if (!*pchWord)
251            continue;
252
253        // allocate
254        pEntryNew = new CAscEntry;
255        if (!pEntryNew)
256            return E_OUTOFMEMORY;
257
258        // link up
259        if (!_pEntryLast)
260        {
261            pEntryNew->_number = 0;
262            _pEntryLast = _pEntryFirst = pEntryNew;
263        }

264        else
265        {
266            pEntryNew->_number = _pEntryLast->_number+1;
267            _pEntryLast->_pEntryNext = pEntryNew;
268            _pEntryLast = pEntryNew;
269        }

270
271        // fill in assoc
272        strcpy(pEntryNew->_achString, pchWord);
273
274        // fill in other fields
275        for (;;)
276        {
277            GetWord(&pch, &pchWord);
278            if (!*pchWord)
279                break;
280
281            if (!strcmp(pchWord, "number"))
282            {
283                GetWord(&pch, &pchWord);
284                if (*pchWord == '=')
285                {
286                    for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
287                    {
288                        if (!strcmp(pchWord+1, pEntry->_achString))
289                        {
290                            break;
291                        }

292                    }

293                    
294                    if (!pEntry)
295                    {
296                        hr = E_FAIL;
297                        goto Cleanup;
298                    }

299                    
300                    pEntryNew->_number = pEntry->_number;
301                    strcpy(pEntryNew->_achNumber, pEntry->_achNumber);
302                }

303                else if (*pchWord >= '0' && *pchWord <= '9' || *pchWord == '-')
304                {
305                    pEntryNew->_number = atol(pchWord);
306                    *pEntryNew->_achNumber = '\0';
307                }

308                else
309                {
310                    pEntryNew->_number = 0;
311                    strcpy(pEntryNew->_achNumber, pchWord);
312                }

313            }

314            else
315            if (!strcmp(pchWord, "string"))
316            {
317                GetWord(&pch, &pchWord);
318                strcpy(pEntryNew->_achStringName, pchWord);
319            }

320            else
321            if (!strcmp(pchWord, "enum"))
322            {
323                GetWord(&pch, &pchWord);
324                strcpy(pEntryNew->_achEnum, pchWord);
325            }

326            else
327            if (!strcmp(pchWord, "assoc"))
328            {
329                GetWord(&pch, &pchWord);
330                strcpy(pEntryNew->_achAssoc, pchWord);
331            }

332            else
333            if (!strcmp(pchWord, "noassoc"))
334            {
335                pEntryNew->_fNoassoc = TRUE;
336            }

337            else
338            if (!strcmp(pchWord, "nostring"))
339            {
340                pEntryNew->_fNostring = TRUE;
341            }

342            else
343            if (!strcmp(pchWord, "noenum"))
344            {
345                pEntryNew->_fNoenum = TRUE;
346            }

347        }

348    }

349
350    // compute assocs
351    for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
352    {
353        if (!pEntry->_fNoassoc)
354        {
355            WCHAR awch[MAX_WORD];
356            WCHAR *pwch = awch;
357            char *pch = pEntry->_achString;
358            DWORD len;
359            DWORD hash;
360            CAssoc *passoc;
361            
362            do *pwch++ = *pch; } while (*pch++);
363
364            len = _tcslen(awch);
365            
366            if (_fInsensitive)
367                hash = HashStringCi(awch, len, 0);
368            else
369                hash = HashString(awch, len, 0);
370
371            passoc = nt.AddAssoc((DWORD_PTR)pEntry, awch, len, hash);
372            if (!passoc)
373            {
374                hr = E_FAIL;
375                goto Cleanup;
376            }

377            pEntry->_pAssoc = passoc;
378        }

379    }

380            
381    // phase 3: output header decls enums
382    fprintf(fpOutput, "// %s\n", pchOutputFile);
383    fprintf(fpOutput, "// Generated by ascparse.exe from %s\n", pchInputFile);
384    fprintf(fpOutput, "// Do not modify by hand!\n");
385    fprintf(fpOutput, "\n#ifndef _cxx_\n\n");
386
387    // assocarray and hash
388    fprintf(fpOutput, "class CAssoc;\n");
389    fprintf(fpOutput, "class CAssocArray;\n\n");
390    
391    fprintf(fpOutput, "extern CAssoc *%s_HashTable[%d];\n", _achAssocArray, nt._mHash);
392    if (_fReversible)
393    {
394        fprintf(fpOutput, "extern CAssoc *%s_RevSearch[];\n", _achAssocArray);
395    }

396    fprintf(fpOutput, "extern CAssocArray %s;\n\n", _achAssocArray);
397    
398    // enums
399    if (*_achEnumType)
400    {
401        fprintf(fpOutput, "enum %s\n{\n", _achEnumType);
402        for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
403        {
404            if (!pEntry->_fNoenum)
405            {
406                if (*pEntry->_achEnum)
407                {
408                    fprintf(fpOutput, "    %s = %d,\n", pEntry->_achEnum, pEntry->_number);
409                }

410                else
411                {
412                    fprintf(fpOutput, "    %s%s = %d,\n", _achEnumPrefix, pEntry->_achString, pEntry->_number);
413                }

414            }

415        }

416        fprintf(fpOutput, "    %s_FORCE_LONG = LONG_MAX\n", _achEnumType);
417        fprintf(fpOutput, "};\n\n");
418    }

419    
420    // assocs
421    for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
422    {
423        if (!pEntry->_fNoassoc)
424        {
425            if (!*pEntry->_achAssoc)
426            {
427                char suffix[32];
428                GetSuffix(pEntry->_achString, suffix, pEntry->_number); 
429                fprintf(fpOutput, "extern CAssoc %s%s%s;\n",
430                    _achAssocPrefix, pEntry->_achString, suffix);
431            }

432            else
433            {
434                fprintf(fpOutput, "extern CAssoc %s;\n",
435                    pEntry->_achAssoc);
436            }

437        }

438    }

439    
440    fprintf(fpOutput, "\n\n");
441
442    // strings
443    for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
444    {
445        if (!pEntry->_fNostring)
446        {
447            if (!pEntry->_fNoassoc && (*_achStringNamePrefix || *pEntry->_achStringName))
448            {
449                if (*pEntry->_achStringName)
450                {
451                    fprintf(fpOutput, "#define %s ",
452                        pEntry->_achStringName);
453                }

454                else
455                {
456                    fprintf(fpOutput, "#define %s%s ",
457                        _achStringNamePrefix, pEntry->_achString);
458                }

459                
460                if (!*pEntry->_achAssoc)
461                {
462                    char suffix[32];
463                    GetSuffix(pEntry->_achString, suffix, pEntry->_number); 
464                    fprintf(fpOutput, "(%s%s%s._ach)\n",
465                        _achAssocPrefix, pEntry->_achString, suffix);
466                }

467                else
468                {
469                    fprintf(fpOutput, "(%s._ach)\n",
470                        pEntry->_achAssoc);
471                }

472            }

473            else
474            {
475                if (*pEntry->_achStringName)
476                {
477                    fprintf(fpOutput, "#define %s (_T(\"%s\"))\n",
478                        pEntry->_achStringName, pEntry->_achString);
479                }

480                else if (*_achStringNamePrefix)
481                {
482                    fprintf(fpOutput, "#define *%s%s (_T(\"%s\"))\n",
483                        _achStringNamePrefix, pEntry->_achString, pEntry->_achString);
484                }

485            }

486        }

487    }

488
489    // end of header section; start of cxx section
490    fprintf(fpOutput, "\n#else _cxx_\n\n");
491    fprintf(fpOutput, "\n#undef _cxx_\n\n");
492
493    
494    // phase 4: output assocs
495    for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
496    {
497        if (!pEntry->_fNoassoc)
498        {
499            if (!*pEntry->_achAssoc)
500            {
501                char suffix[32];
502                GetSuffix(pEntry->_achString, suffix, pEntry->_number); 
503                fprintf(fpOutput, "CAssoc %s%s%s\t\t\t= ",
504                    _achAssocPrefix, pEntry->_achString, suffix);
505            }

506            else
507            {
508                fprintf(fpOutput, "CAssoc %s = ",
509                    pEntry->_achAssoc);
510            }

511            if (*pEntry->_achNumber)
512            {
513                fprintf(fpOutput, "{ %12s, 0x%08x, _T(\"%s\") };\n",
514                    pEntry->_achNumber, pEntry->_pAssoc->_hash, pEntry->_achString);
515            }

516            else
517            {
518                fprintf(fpOutput, "{ %5d, 0x%08x, _T(\"%s\") };\n",
519                    pEntry->_number, pEntry->_pAssoc->_hash, pEntry->_achString);
520            }

521        }

522    }

523
524    // phase 5: output table
525    // output hash table
526    fprintf(fpOutput, "\n\nCAssoc *%s_HashTable[%d] =\n{\n", _achAssocArray, nt._mHash);
527    {
528        int i;
529        int c;
530        int s;
531        int d;
532        
533        CAssoc **ppAssoc;
534        for (ppAssoc = nt._pHashTable, c=nt._mHash; c; ppAssoc++, c--)
535        {
536            if (!*ppAssoc)
537            {
538                fprintf(fpOutput, "        NULL,\n");
539            }

540            else
541            {
542                i = (*ppAssoc)->Hash() % nt._mHash;
543                s = ((*ppAssoc)->Hash() & nt._sHash) + 1;
544                d = 0;
545                while (nt._pHashTable + i != ppAssoc)
546                {
547                    if (i < s)
548                        i += nt._mHash;
549                    i -= s;
550                    d++;
551                }

552                    
553                fprintf(fpOutput, "/*%2d */ ",d);
554                
555                pEntry = (CAscEntry*)(*ppAssoc)->Number();
556                
557                if (!*pEntry->_achAssoc)
558                {
559                    char suffix[32];
560                    GetSuffix(pEntry->_achString, suffix, pEntry->_number); 
561                    fprintf(fpOutput, "&%s%s%s,\n",
562                        _achAssocPrefix, pEntry->_achString, suffix);
563                }

564                else
565                {
566                    char suffix[32];
567                    GetSuffix(pEntry->_achAssoc, suffix, pEntry->_number); 
568                    fprintf(fpOutput, "&%s%s,\n",
569                        pEntry->_achAssoc, suffix);
570                }

571
572            }

573        }

574    }

575
576    fprintf(fpOutput, "};\n\n");
577
578    // phase 6: output table for reverse search (if requested)
579    if (_fReversible)
580    {
581        CAscParser::CAscEntry *    pFound;
582        CAscParser::CAscEntry *    pEntry;
583        int nCurrMin;
584        int nPrevMin;
585        int nCurrVal;
586
587        fprintf(fpOutput, "CAssoc *%s_RevSearch[] =\n{\n", _achAssocArray);
588
589        // find and print the entries in order from numeric min to max
590        nPrevMin = 0;
591        for(;;)
592        {
593            // find the next entry
594
595            nCurrMin = INT_MAX;
596            pFound = NULL;
597
598            for (pEntry = _pEntryFirst; pEntry; pEntry = pEntry->_pEntryNext)
599            {
600                if (*pEntry->_achNumber)
601                {
602                    nCurrVal = atol(pEntry->_achNumber);
603                }

604                else
605                {
606                    nCurrVal = pEntry->_number;
607                }

608
609                if (nCurrVal > nPrevMin && nCurrVal < nCurrMin)
610                {
611                    nCurrMin = nCurrVal;
612                    pFound = pEntry;
613                }

614            }

615
616            // break out once we've done everything
617            if (pFound == NULL)
618            {
619                break;
620            }

621
622            // output a pointer to the assoc
623            if (!*pFound->_achAssoc)
624            {
625                char suffix[32];
626                GetSuffix(pFound->_achString, suffix, pFound->_number); 
627                fprintf(fpOutput, "\t&%s%s%s,\n",
628                    _achAssocPrefix, pFound->_achString, suffix);
629            }

630            else
631            {
632                fprintf(fpOutput, "\t%s,\n", pFound->_achAssoc);
633            }

634
635            nPrevMin = nCurrMin;
636        }

637
638        fprintf(fpOutput, "};\n\n");
639    }

640
641    // output assoc table struct itself
642    fprintf(fpOutput, "CAssocArray %s = {\n", _achAssocArray);
643    fprintf(fpOutput, "    %s_HashTable,\n", _achAssocArray);
644    fprintf(fpOutput, "    %d,\n", nt._cHash);
645    fprintf(fpOutput, "    %d,\n", nt._mHash);
646    fprintf(fpOutput, "    %d,\n", nt._sHash);
647    fprintf(fpOutput, "    %d,\n", nt._cHash);
648    fprintf(fpOutput, "    %d,\n", nt._iSize);
649    fprintf(fpOutput, "    TRUE,\n");
650    fprintf(fpOutput, "};\n\n");
651        
652    fprintf(fpOutput, "\n#endif _cxx_\n\n");
653    
654    
655Cleanup:
656    nt.Deinit();
657    
658    if (fpInput)
659        fclose(fpInput);
660    if (fpOutput)
661        fclose(fpOutput);
662
663    return hr;
664}

665
666static
667BOOL
668ReadLine(FILE *fp, char *pchBuf, int cchBuf, int *pcchRead)
669{
670    int cchRead;
671    
672    if (!fgets(pchBuf, cchBuf, fp))
673        return FALSE;
674
675    cchRead = strlen(pchBuf);
676    if (!cchRead)
677        return FALSE;
678
679    if (pcchRead)
680        *pcchRead = cchRead;
681        
682    return TRUE;
683}

684
685static
686void
687SkipSpace(char **ppch)
688{
689    char *pch = *ppch;
690    while (*pch && (*pch == ' ' || *pch == '\t' || *pch == '\r' || *pch == '\n'))
691        pch++;
692    *ppch = pch;
693}

694
695
696static
697void
698SkipNonspace(char **ppch)
699{
700    char *pch = *ppch;
701    while (*pch && (*pch != ' ' && *pch != '\t' && *pch != '\r' && *pch != '\n'))
702        pch++;
703    *ppch = pch;
704}

705
706static
707void
708ChopComment(char *pch)
709{
710    while (*pch)
711    {
712        if (*pch == '/' && *(pch+1== '/')
713        {
714            *pch = '\0';
715            return;
716        }

717        pch++;
718    }

719}

720
721static
722void
723GetWord(char **ppch, char **ppchWord)
724{
725    SkipSpace(ppch);
726    *ppchWord = *ppch;
727    SkipNonspace(ppch);
728    if (**ppch)
729    {
730        **ppch = '\0';
731        if (*ppch - *ppchWord > MAX_WORD)
732            *(*ppchWord + MAX_WORD-1= '\0';
733        (*ppch)++;
734    }

735}

736
737


============================================================
再看一段MS的源代码:

 1int __cdecl
 2main  ( int argc, char *argv[] )
 3{
 4    int nReturnCode;
 5    char szInputFile [ MAX_PATH+1 ];
 6    char szOutputFileRoot [ MAX_PATH+1 ];
 7    char szPDLFileName [ MAX_PATH+1 ];
 8    char szOutputPath [ MAX_PATH+1 ];
 9    BOOL fDebugging = FALSE;
10
11    {
12    CPDLParser Parser;
13
14    // argv[1] is the name of a file containing build args
15    // arg1 of this file is the full path/filename of the input file
16    // arg2 is the full name of the output file, minus the file extension
17    // arg3 is the 8.3 pdl file name
18    // arg4 is a log file name
19    if ( argc == 5 )
20    {
21        strcpy ( szInputFile, argv [ 1 ] );
22        strcpy ( szOutputFileRoot, argv [ 2 ] );
23        strcpy ( szPDLFileName, argv [ 3 ] );
24        strcpy ( szOutputPath, argv [ 4 ] );
25        fDebugging = TRUE;
26    }

27    else if ( argc == 3 && _stricmp( argv[1], "/g"== 0 ||
28                           _stricmp( argv[1], "-g"== 0 )
29    {
30        // Process the funcsig.dat file and produce custsig.hxx file:
31        if (!ProcessDatFile ( argv [ 2 ] ))
32        {
33            nReturnCode = 4;
34            goto Cleanup;
35        }

36
37        if (!GenerateVTableHeader ( argv [ 2 ] ))
38        {
39            nReturnCode = 4;
40            goto Cleanup;
41        }

42
43        if (!GenerateVTableCXX ( argv [ 2 ] ))
44        {
45            nReturnCode = 4;
46            goto Cleanup;
47        }

48
49        FILE *fpMaxLenFile = NULL;
50
51        strcpy(szOutputPath, argv [ 2 ]);
52        strcat(szOutputPath, FILENAME_SEPARATOR_STR "maxlen.txt");
53
54        fpMaxLenFile = fopen(szOutputPath, "r+");
55        if (fpMaxLenFile)
56        {
57            char chMarker[6];
58            strcpy(chMarker, "Const");
59            fwrite(chMarker, sizeof(char), 5, fpMaxLenFile);
60            fclose(fpMaxLenFile);
61        }

62
63        nReturnCode = 0;
64        goto Cleanup;
65    }

66    else if ( argc > 1 )
67    {
68        if ( !ScanBuildFile ( argv[ 1 ],
69                              szInputFile,
70                              szOutputFileRoot,
71                              szPDLFileName,
72                              szOutputPath ) )
73        {
74            printf ( "Cant scan build file\n" );
75            nReturnCode = 2;
76            goto Cleanup;
77        }

78    }

79    else
80    {
81        printf ( "Invalid command line params\n" );
82        nReturnCode = 3;
83        goto Cleanup;
84    }

85
86    nReturnCode = Parser.Parse ( szInputFile, szOutputFileRoot, 
87        szPDLFileName, szOutputPath, fDebugging  );
88
89    }

90
91Cleanup:
92    if ( nReturnCode != 0 )
93        printf ( "Error %d building PDL file\n", nReturnCode );
94    exit ( nReturnCode );
95    return nReturnCode;
96}

97
98

 

posted @ 2006-09-23 16:06  ->  阅读(408)  评论(0)    收藏  举报