home *** CD-ROM | disk | FTP | other *** search
/ What PC? 1996 April / WHAT_PC_APR_96.ISO / internet / twinsock / src / getentry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-24  |  2.1 KB  |  102 lines

  1. /*
  2.  *  TwinSock - "Troy's Windows Sockets"
  3.  *
  4.  *  Copyright (C) 1995  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the license in the file LICENSE.TXT included
  8.  *  with the TwinSock distribution.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  13.  */
  14. #ifdef _Windows
  15. #include <windows.h>
  16. #else
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #endif
  20.  
  21. int
  22. GetTwinSockSetting(    char    *pchSection,
  23.             char    *pchItem,
  24.             char    *pchDefault,
  25.             char    *pchBuffer,
  26.             int    nChars)
  27. {
  28. #ifdef _Windows
  29.     return GetPrivateProfileString(    pchSection,
  30.                     pchItem,
  31.                     pchDefault,
  32.                     pchBuffer,
  33.                     nChars,
  34.                     "TWINSOCK.INI");
  35. #else
  36.     static    FILE    *fpIni = 0;
  37.     static    int    iDone = 0;
  38.     char    achBuffer[1024];
  39.     int    iInSection = 0;
  40.     char    *c;
  41.  
  42.     if (!iDone)
  43.     {
  44.         strcpy(achBuffer, getenv("HOME"));
  45.         strcat(achBuffer, "/.twinsock");
  46.         fpIni = fopen(achBuffer, "r");
  47.         iDone = 1;
  48.     }
  49.     if (!fpIni)
  50.     {
  51.         strcpy(pchBuffer, pchDefault);
  52.         return strlen(pchDefault);
  53.     }
  54.     rewind(fpIni);
  55.     while (fgets(achBuffer, 1024, fpIni))
  56.     {
  57.         if (*achBuffer == ';' || *achBuffer == '\n')
  58.             continue;
  59.         if (*achBuffer == '[')
  60.         {
  61.             c = achBuffer + 1;
  62.             while (isspace(*c))
  63.                 c++;
  64.             if (strncmp(c, pchSection, strlen(pchSection)))
  65.             {
  66.                 iInSection = 0;
  67.                 continue;
  68.             }
  69.             c += strlen(pchSection);
  70.             while (isspace(*c))
  71.                 c++;
  72.             if (*c != ']')
  73.             {
  74.                 iInSection = 0;
  75.                 continue;
  76.             }
  77.             iInSection = 1;
  78.             continue;
  79.         }
  80.         if (!iInSection)
  81.             continue;
  82.         if (strncmp(achBuffer, pchItem, strlen(pchItem)))
  83.             continue;
  84.         c = achBuffer + strlen(pchItem);
  85.         while (isspace(*c))
  86.             c++;
  87.         if (*c++ != '=')
  88.             continue;
  89.         while (isspace(*c))
  90.             c++;
  91.         c[strlen(c)] = 0;
  92.         strncpy(pchBuffer, c, nChars);
  93.             pchBuffer[nChars - 1] = 0;
  94.         pchBuffer[strlen(pchBuffer) - 1] = 0;
  95.         return strlen(pchBuffer);
  96.     }
  97.     strcpy(pchBuffer, pchDefault);
  98.     return strlen(pchDefault);
  99. #endif
  100. }
  101.  
  102.