home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / gotype.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-29  |  5.1 KB  |  173 lines

  1. /*
  2.  *  Shangjie - here is some code from the Gopher Server which should be helpful.
  3.  */
  4.  
  5. #include <windows.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. int ReadGopherConfigInfo(void);
  11.  
  12. #define MAXEXTENSIONLENGTH           10
  13. #define MAXNOEXTENSIONS              128
  14. #define GOPHERDEFAULTFILETYPEKEY    "DefaultFileType"
  15. #define GOPHERSERVERKEY             "GOPHERS\\Parameters"
  16. #define EXTENSIONMAPKEY             "ExtensionMapping" /* Subkey for extension mappings */
  17. #define DEFAULTDEFAULTFILETYPE      '0'
  18. #define DEFAULTGOPHERTYPES    {                 \
  19.     {'0',"TXT"},    /* Text file */             \
  20.     {'5',"ZIP"},    /* Binary archive */        \
  21.     {'5',"ARC"},    /* Binary archive */        \
  22.     {'6',"UUE"},    /* UUencoded */             \
  23.     {'7',"SRC"},    /* WAIS index */            \
  24.     {'9',"EXE"},    /* Binary */                \
  25.     {'9',"DLL"},    /* Binary */                \
  26.     {'g',"GIF"},    /* GIF image */             \
  27.     {'I',"BMP"},    /* Windows bitmap */        \
  28.     {'s',"AU"},     /* Sound */                 \
  29.     {'h',"HTM"},    /* HTML */                  \
  30.     {'h',"HTML"},   /* HTML */                  \
  31.     {'\0',""}       /* Marks end of table */    \
  32. }
  33.  
  34. /* Gopher type table entry */
  35. typedef struct _FileTypeMap {
  36.     char    GopherType;
  37.     char    FileExtension[MAXEXTENSIONLENGTH];
  38. } tFileTypeMap;
  39.  
  40.  
  41. char DefaultFileType = DEFAULTDEFAULTFILETYPE; /* For unrecognised file extensions */
  42. tFileTypeMap FileTypeTable[MAXNOEXTENSIONS] = DEFAULTGOPHERTYPES;
  43.  
  44.  
  45. /*
  46.  *  Return a pointer to the extension in the supplied file name.
  47.  */
  48. static char *GetFileNameExtension(FileName) 
  49. char *FileName;
  50. {
  51.     int i;
  52.  
  53.     i = strlen(FileName)-1;
  54.     while (i>0) {
  55.         if (FileName[i]=='.') break;
  56.         if (FileName[i]=='\\') break;
  57.         i--;
  58.     }
  59.     if (i==0 || FileName[i]=='\\') {
  60.         /* No dot in the name, so point at the '\0' at the end of the name. */
  61.         return &(FileName[strlen(FileName)]);
  62.     } else {
  63.         /* Found file extension */
  64.         return &(FileName[i+1]);
  65.     }
  66. }
  67.  
  68.  
  69. /*
  70.  *  Given the name of a file, look up the extension in the FileTypeTable
  71.  *  and return the corresponding Gopher file type character.
  72.  */
  73. char FileTypeFromTable(FileName) 
  74. LPSTR FileName;
  75. {
  76.     LPSTR pExt;
  77.     int i;
  78.  
  79.     /* Get filename extension */
  80.     pExt = GetFileNameExtension(FileName);
  81.     
  82.     /* Set up the table */
  83.     ReadGopherConfigInfo();
  84.         
  85.     /* Look through the table for the type to use */
  86.     i = 0;
  87.     while (FileTypeTable[i].GopherType!='\0') {
  88.         if (stricmp(pExt,FileTypeTable[i].FileExtension)==0)
  89.             return FileTypeTable[i].GopherType;
  90.         i++;
  91.     }
  92.  
  93.     /* Not found in table, so use the default */
  94.     return DefaultFileType;
  95. }
  96.  
  97.  
  98. /*
  99.  *  This function reads the file type configuration information from the Registry
  100.  */
  101. int ReadGopherConfigInfo(void) 
  102. {
  103.     LONG lResult;
  104.     int index;
  105.     DWORD dwSize;
  106.     DWORD dwSizeExt;
  107.     HKEY hkGopherServer;
  108.     HKEY hkExtensionMapping;
  109.     char KeyName[100];
  110.  
  111.      
  112.     /* Open the HKEY_LOCAL_MACHINE \System\CurrentControlSet\Services\GOPHERS\Parameters key */
  113.     strncpy(KeyName,"SYSTEM\\CurrentControlSet\\Services\\",sizeof(KeyName));
  114.     strncat(KeyName,GOPHERSERVERKEY,sizeof(KeyName));
  115.     lResult = RegOpenKeyEx(
  116.                 HKEY_LOCAL_MACHINE,
  117.                 KeyName,
  118.                 0,
  119.                 KEY_READ,
  120.                 &hkGopherServer);
  121.     if (lResult!=0) {
  122.         /* No key - we'll use the defaults */
  123.         return 0;
  124.     }
  125.  
  126.     /* Get DefaultFileType */
  127.     dwSize = sizeof(DefaultFileType);
  128.     lResult = RegQueryValueEx(
  129.                 hkGopherServer,
  130.                 GOPHERDEFAULTFILETYPEKEY,
  131.                 NULL,
  132.                 NULL,
  133.                 &DefaultFileType,
  134.                 &dwSize);
  135.     if (lResult!=0 || DefaultFileType=='\0') {
  136.         /* Not found, so ensure we're using the default value */
  137.         DefaultFileType = DEFAULTDEFAULTFILETYPE;
  138.     }
  139.  
  140.     /* Now the rest of the table */
  141.     lResult = RegOpenKeyEx(hkGopherServer,EXTENSIONMAPKEY,0,KEY_READ,&hkExtensionMapping);
  142.     if (lResult==0) {
  143.         /* Opened the key successfully - load the table. */
  144.         index = 0;
  145.         while (index<MAXNOEXTENSIONS-1) {
  146.             dwSizeExt = MAXEXTENSIONLENGTH;
  147.             dwSize = 1;
  148.             lResult = RegEnumValue(
  149.                             hkExtensionMapping,
  150.                             index,
  151.                             FileTypeTable[index].FileExtension,
  152.                             &dwSizeExt,
  153.                             NULL,
  154.                             NULL,
  155.                             &(FileTypeTable[index].GopherType),
  156.                             &dwSize);
  157.             if (lResult!=0) break;
  158.             index++;
  159.         }
  160.         /* Tie off the table */
  161.         FileTypeTable[index].GopherType = '\0';
  162.         FileTypeTable[index].FileExtension[0] = '\0';
  163.         /* Close the key */
  164.         RegCloseKey(hkExtensionMapping);
  165.     }
  166.  
  167.     /* Close the registry */
  168.     RegCloseKey(hkGopherServer);
  169.  
  170.     return 0;
  171. }
  172.  
  173.