home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 June / VPR0106A.BIN / OLS / TCL228 / TCL228.lzh / DLLSRC.lzh / utl.c < prev    next >
C/C++ Source or Header  |  1999-09-18  |  6KB  |  324 lines

  1. /*-------------------------------------------
  2.   utl.c
  3.   misc functions
  4.   KAZUBON 1997-1999
  5. ---------------------------------------------*/
  6.  
  7. #include <windows.h>
  8.  
  9. void WriteDebug(LPSTR s);
  10.  
  11. extern HANDLE hmod;
  12.  
  13. int _strncmp(const char* d, const char* s, size_t n)
  14. {
  15.     unsigned int i;
  16.     for(i = 0; i < n; i++)
  17.     {
  18.         if(*s == 0 && *d == 0) break;
  19.         if(*d != *s) return (*d - *s);
  20.         d++; s++;
  21.     }
  22.     return 0;
  23. }
  24.  
  25. /*-------------------------------------------
  26.  パス名にファイル名をつける
  27. ---------------------------------------------*/
  28. void add_title(char *path, char *title)
  29. {
  30.     char *p;
  31.     
  32.     p = path;
  33.  
  34.     if(*title && *(title + 1) == ':') ;
  35.     else if(*title == '\\')
  36.     {
  37.         if(*p && *(p + 1) == ':') p += 2;
  38.     }
  39.     else
  40.     {
  41.         while(*p)
  42.         {
  43.             if((*p == '\\' || *p == '/') && *(p + 1) == 0)
  44.             {
  45.                 break;
  46.             }
  47.             p = CharNext(p);
  48.         }
  49.         *p++ = '\\';
  50.     }
  51.     while(*title) *p++ = *title++;
  52.     *p = 0;
  53. }
  54.  
  55. /*-------------------------------------------
  56.  パス名からファイル名をとりのぞく
  57. ---------------------------------------------*/
  58. void del_title(char *path)
  59. {
  60.     char *p, *ep;
  61.  
  62.     p = ep = path;
  63.     while(*p)
  64.     {
  65.         if(*p == '\\' || *p == '/')
  66.         {
  67.             if(p > path && *(p - 1) == ':') ep = p + 1;
  68.             else ep = p;
  69.         }
  70.         p = CharNext(p);
  71.     }
  72.     *ep = 0;
  73. }
  74.  
  75. /*------------------------------------------------
  76.     カンマで区切られた文字列を取り出す
  77. --------------------------------------------------*/
  78. void parse(char *dst, char *src, int n)
  79. {
  80.     char *dp;
  81.     int i;
  82.  
  83.     for(i = 0; i < n; i++)
  84.     {
  85.         while(*src && *src != ',') src++;
  86.         if(*src == ',') src++;
  87.     }
  88.     if(*src == 0) 
  89.     {
  90.         *dst = 0; return;
  91.     }
  92.     
  93.     while(*src == ' ') src++;
  94.     
  95.     dp = dst;
  96.     while(*src && *src != ',') *dst++ = *src++;
  97.     *dst = 0;
  98.     
  99.     while(dst != dp)
  100.     {
  101.         dst--;
  102.         if(*dst == ' ') *dst = 0;
  103.         else break;
  104.     }
  105. }
  106.  
  107. /*-------------------------------------------
  108.   returns a resource string
  109. ---------------------------------------------*/
  110. char* MyString(UINT id)
  111. {
  112.     static char buf[80];
  113.     
  114.     if(LoadString(hmod, id, buf, 80) == 0)
  115.         buf[0] = 0;
  116.     
  117.     return buf;
  118. }
  119.  
  120. char mykey[] = "Software\\Kazubon\\TClock";
  121.  
  122. /*------------------------------------------------
  123.  自分のレジストリから文字列を得る
  124. --------------------------------------------------*/
  125. int GetMyRegStr(char* section, char* entry, char* val, int cbData,
  126.     char* defval)
  127. {
  128.     char keystr[80];
  129.     HKEY hkey;
  130.     DWORD regtype;
  131.     DWORD size;
  132.     BOOL b;
  133.     int r;
  134.     
  135.     strcpy(keystr, mykey);
  136.     if(section && *section)
  137.     {
  138.         strcat(keystr, "\\"); strcat(keystr, section);
  139.     }
  140.     b = FALSE;
  141.     if(RegOpenKey(HKEY_CURRENT_USER, keystr, &hkey) == 0)
  142.     {
  143.         size = cbData;
  144.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  145.             (LPBYTE)val, &size) == 0)
  146.         {
  147.             if(size == 0) *val = 0;
  148.             r = size;
  149.             b = TRUE;
  150.         }
  151.         RegCloseKey(hkey);
  152.     }
  153.     if(b == FALSE)
  154.     {
  155.         strcpy(val, defval);
  156.         r = strlen(defval);
  157.     }
  158.     return r;
  159. }
  160.  
  161. /*------------------------------------------------
  162.  自分のレジストリからLONG値を得る
  163. --------------------------------------------------*/
  164. LONG GetMyRegLong(char* section, char* entry, LONG defval)
  165. {
  166.     char keystr[80];
  167.     HKEY hkey;
  168.     DWORD regtype;
  169.     DWORD size;
  170.     BOOL b;
  171.     LONG r;
  172.  
  173.     strcpy(keystr, mykey);
  174.     if(section && *section)
  175.     {
  176.         strcat(keystr, "\\"); strcat(keystr, section);
  177.     }
  178.     b = FALSE;
  179.     if(RegOpenKey(HKEY_CURRENT_USER, keystr, &hkey) == 0)
  180.     {
  181.         size = 4;
  182.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  183.             (LPBYTE)&r, &size) == 0)
  184.         {
  185.             if(size == 4) b = TRUE;
  186.         }
  187.         RegCloseKey(hkey);
  188.     }
  189.     if(b == FALSE) r = defval;
  190.     return r;
  191. }
  192.  
  193. /*------------------------------------------------
  194.   get DWORD value from registry
  195. --------------------------------------------------*/
  196. LONG GetRegLong(HKEY rootkey, char*subkey, char* entry, LONG defval)
  197. {
  198.     HKEY hkey;
  199.     DWORD regtype;
  200.     DWORD size;
  201.     BOOL b;
  202.     int r;
  203.     
  204.     b = FALSE;
  205.     if(RegOpenKey(rootkey, subkey, &hkey) == 0)
  206.     {
  207.         size = 4;
  208.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  209.             (LPBYTE)&r, &size) == 0)
  210.         {
  211.             if(size == 4) b = TRUE;
  212.         }
  213.         RegCloseKey(hkey);
  214.     }
  215.     if(b == FALSE) r = defval;
  216.     return r;
  217. }
  218.  
  219. /*------------------------------------------------
  220.  レジストリから文字列を得る
  221. --------------------------------------------------*/
  222. int GetRegStr(HKEY rootkey, char*subkey, char* entry,
  223.     char* val, int cbData, char* defval)
  224. {
  225.     HKEY hkey;
  226.     DWORD regtype;
  227.     DWORD size;
  228.     BOOL b;
  229.     int r;
  230.     
  231.     b = FALSE;
  232.     if(RegOpenKey(rootkey, subkey, &hkey) == 0)
  233.     {
  234.         size = cbData;
  235.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  236.             (LPBYTE)val, &size) == 0)
  237.         {
  238.             if(size == 0) *val = 0;
  239.             b = TRUE;
  240.         }
  241.         RegCloseKey(hkey);
  242.     }
  243.     if(b == FALSE)
  244.     {
  245.         strcpy(val, defval);
  246.         r = strlen(defval);
  247.     }
  248.     return r;
  249. }
  250.  
  251. /*-------------------------------------------
  252.  レジストリに文字列を書き込む
  253. ---------------------------------------------*/
  254. BOOL SetMyRegStr(char* subkey, char* entry, char* val)
  255. {
  256.     HKEY hkey;
  257.     BOOL r;
  258.     char key[80];
  259.     
  260.     strcpy(key, mykey);
  261.     if(*subkey)
  262.     {
  263.         strcat(key, "\\"); strcat(key, subkey);
  264.     }
  265.     r = FALSE;
  266.     if(RegCreateKey(HKEY_CURRENT_USER, key, &hkey) == 0)
  267.     {
  268.         if(RegSetValueEx(hkey, entry, 0, REG_SZ,
  269.             (CONST BYTE*)val, strlen(val)) == 0)
  270.         {
  271.             r = TRUE;
  272.         }
  273.         RegCloseKey(hkey);
  274.     }
  275.     return r;
  276. }
  277.  
  278. /*-------------------------------------------
  279.  レジストリにDWORD値を書き込む
  280. ---------------------------------------------*/
  281. BOOL SetMyRegLong(char* subkey, char* entry, DWORD val)
  282. {
  283.     HKEY hkey;
  284.     BOOL r;
  285.     char key[80];
  286.     
  287.     strcpy(key, mykey);
  288.     if(*subkey)
  289.     {
  290.         strcat(key, "\\"); strcat(key, subkey);
  291.     }
  292.     r = FALSE;
  293.     if(RegCreateKey(HKEY_CURRENT_USER, key, &hkey) == 0)
  294.     {
  295.         if(RegSetValueEx(hkey, entry, 0, REG_DWORD,
  296.             (CONST BYTE*)&val, 4) == 0)
  297.         {
  298.             r = TRUE;
  299.         }
  300.         RegCloseKey(hkey);
  301.     }
  302.     return r;
  303. }
  304.  
  305. /*------------------------------------------------
  306.  デバッグ用
  307. --------------------------------------------------*/
  308. void WriteDebug(LPSTR s)
  309. {
  310.     HFILE hf;
  311.     char fname[MAX_PATH], title[] = "DEBUG.TXT";
  312.  
  313.     GetModuleFileName(hmod, fname, MAX_PATH);
  314.     del_title(fname);
  315.     add_title(fname, title);
  316.     hf = _lopen(fname, OF_WRITE);
  317.     if(hf == HFILE_ERROR) hf = _lcreat(fname, 0);
  318.     if(hf == HFILE_ERROR) return;
  319.     _llseek(hf, 0, 2);
  320.     _lwrite(hf, s, lstrlen(s));
  321.     _lwrite(hf, "\x0d\x0a", 2);
  322.     _lclose(hf);
  323. }
  324.