home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / odbc / convdsn / convdsn.c next >
Encoding:
C/C++ Source or Header  |  1996-11-01  |  3.7 KB  |  147 lines

  1. /*
  2. ** CONVDSN.C - This is the ODBC sample code for
  3. ** creating File DSN pointers to machine DSNs.
  4. **
  5. **    This code is furnished on an as-is basis as part of the ODBC SDK and is
  6. **    intended for example purposes only.
  7. **
  8. */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include "sql.h"
  13. #include "sqlext.h"
  14. #include "odbcinst.h"
  15.  
  16. // prototypes
  17.  
  18. void ExpandFileName(LPSTR szFileDSNName, LPCSTR szDSNName);
  19. void MakeLegalName(LPSTR szLegalDSNName, LPCSTR szDSNName);
  20.  
  21. // main routine:  Iterate through the user and system DSNs, creating a pointer
  22. //                to each.
  23.  
  24. int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  25.    LPSTR lpszCmdLine, int nCmdShow)
  26. {
  27.     HENV    henv;
  28.     RETCODE retcode;
  29.     CHAR    szDSNName[SQL_MAX_DSN_LENGTH + 1];
  30.     CHAR    szLegalDSNName[SQL_MAX_DSN_LENGTH + 1];
  31.     CHAR    szFileDSNName[MAX_PATH + 1];
  32.  
  33.     if(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv)))
  34.     {
  35.         // set the ODBC behavior version.
  36.         (void) SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
  37.                     (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
  38.  
  39.         // Enumerate the user data sources.
  40.         retcode = SQLDataSources(henv, SQL_FETCH_FIRST_SYSTEM, szDSNName,
  41.             sizeof(szDSNName), NULL, NULL, 0, NULL);
  42.  
  43.         while(SQL_SUCCEEDED(retcode))
  44.         {
  45.             // Make a version of the name acceptable for use as a filename.
  46.             MakeLegalName(szLegalDSNName, szDSNName);
  47.             
  48.             // Build the full filename.
  49.             ExpandFileName(szFileDSNName, szLegalDSNName);
  50.  
  51.             // Write the DSN= keyword into the File DSN.
  52.             SQLWriteFileDSN(szFileDSNName, "ODBC", "DSN", szDSNName);
  53.  
  54.             // Do the next one, if it exists.
  55.             retcode = SQLDataSources(henv, SQL_FETCH_NEXT, szDSNName,
  56.                 sizeof(szDSNName), NULL, NULL, 0, NULL);        
  57.         }
  58.  
  59.         // Enumerate the system data sources.
  60.         retcode = SQLDataSources(henv, SQL_FETCH_FIRST_USER, szDSNName,
  61.             sizeof(szDSNName), NULL, NULL, 0, NULL);
  62.  
  63.         while(SQL_SUCCEEDED(retcode))
  64.         {
  65.             // Make a version of the name acceptable for use as a filename.
  66.             MakeLegalName(szLegalDSNName, szDSNName);
  67.  
  68.             // Build the full filename.
  69.             ExpandFileName(szFileDSNName, szLegalDSNName);
  70.  
  71.             // Write the DSN= keyword into the File DSN.
  72.             SQLWriteFileDSN(szFileDSNName, "ODBC", "DSN", szDSNName);
  73.             
  74.             // Do the next one, if it exists.
  75.             retcode = SQLDataSources(henv, SQL_FETCH_NEXT, szDSNName,
  76.                 sizeof(szDSNName), NULL, NULL, 0, NULL);        
  77.         }
  78.  
  79.         SQLFreeHandle(SQL_HANDLE_ENV, henv);
  80.     }
  81.  
  82.     return 0;
  83. }
  84.  
  85. // ExpandFileName:  Take the pieces of the filename, and form a complete
  86. //                  filename.
  87.  
  88. void ExpandFileName(LPSTR szFileDSNName, LPCSTR szDSNName)
  89. {
  90.     strcpy(szFileDSNName, szDSNName);
  91.  
  92.     // Hint that we're not sharable
  93.     strcat(szFileDSNName, " (not sharable)");
  94. }
  95.  
  96. // MakeLegalName:  Make a version of the DSN name suitable for use as a 
  97. //                 filename.
  98.  
  99. void MakeLegalName(LPSTR szLegalDSNName, LPCSTR szDSNName)
  100. {
  101.     CHAR   szIllegalChars[] = "\\/:*?\"<>|";
  102.     LPSTR  pchIllegalChar;
  103.     LPCSTR pchDSNSource     = szDSNName;
  104.     LPSTR  pchDSNTarget     = szLegalDSNName;
  105.     BOOL   fIllegal;
  106.  
  107.     while(*pchDSNSource)
  108.     {
  109.         pchIllegalChar = szIllegalChars;
  110.         
  111.         fIllegal = FALSE;
  112.         
  113.         // if the character is double byte, copy it and move ahead
  114.         if(IsDBCSLeadByte(*pchDSNSource))
  115.         {
  116.             *(pchDSNTarget++) = *(pchDSNSource++);
  117.             *(pchDSNTarget++) = *(pchDSNSource++);
  118.         }
  119.         else
  120.         {
  121.             // single byte
  122.             if(!isalpha(*pchDSNSource) && !isdigit(*pchDSNSource))
  123.             {
  124.                 while(*pchIllegalChar)
  125.                 {
  126.                     if(*pchDSNSource == *(pchIllegalChar++))
  127.                     {
  128.                         fIllegal = TRUE;
  129.                         break;
  130.                     }
  131.                 }
  132.             }
  133.  
  134.             if(fIllegal)
  135.             {
  136.                 pchDSNSource++;
  137.             }
  138.             else
  139.             {
  140.                 *(pchDSNTarget++) = *(pchDSNSource++);
  141.             }
  142.         }
  143.     }
  144.     *pchDSNTarget = '\0';
  145. }
  146.  
  147.