home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 December (Special) / DOSV2002_12.iso / utility / tcl230ja95.lzh / source.lzh / exe / utl.c < prev   
C/C++ Source or Header  |  2001-02-10  |  13KB  |  626 lines

  1. /*-------------------------------------------
  2.   utl.c
  3.     その他の関数
  4.     KAZUBON 1997-1998
  5. ---------------------------------------------*/
  6.  
  7. #include "tclock.h"
  8.  
  9. /*-------------------------------------------
  10.   ランタイム関数の代用
  11. ---------------------------------------------*/
  12. int atoi(const char *p)
  13. {
  14.     int r = 0;
  15.     while(*p)
  16.     {
  17.         if('0' <= *p && *p <= '9')
  18.             r = r * 10 + *p - '0';
  19.         p++;
  20.  
  21.     }
  22.     return r;
  23. }
  24.  
  25. int atox(const char *p)
  26. {
  27.     int r = 0;
  28.     while(*p)
  29.     {
  30.         if('0' <= *p && *p <= '9')
  31.             r = r * 16 + *p - '0';
  32.         else if('A' <= *p && *p <= 'F')
  33.             r = r * 16 + *p - 'A' + 10;
  34.         else if('a' <= *p && *p <= 'f')
  35.             r = r * 16 + *p - 'a' + 10;
  36.         p++;
  37.  
  38.     }
  39.     return r;
  40. }
  41.  
  42. __inline int toupper(int c)
  43. {
  44.     if('a' <= c && c <= 'z')
  45.         c -= 'a' - 'A';
  46.     return c;
  47. }
  48.  
  49. int _strnicmp(const char* d, const char* s, size_t n)
  50. {
  51.     int c1, c2;
  52.     unsigned int i;
  53.     for(i = 0; i < n; i++)
  54.     {
  55.         if(*s == 0 && *d == 0) break;
  56.         c1 = toupper(*d); c2 = toupper(*s);
  57.         if(c1 != c2) return (c1 - c2);
  58.         d++; s++;
  59.     }
  60.     return 0;
  61. }
  62.  
  63. int _stricmp(const char* d, const char* s)
  64. {
  65.     int c1, c2;
  66.     while(1)
  67.     {
  68.         if(*s == 0 && *d == 0) break;
  69.         c1 = toupper(*d); c2 = toupper(*s);
  70.         if(c1 != c2) return (c1 - c2);
  71.         d++; s++;
  72.     }
  73.     return 0;
  74. }
  75.  
  76. /*-------------------------------------------
  77.  パス名にファイル名をつける
  78. ---------------------------------------------*/
  79. void add_title(char *path, char *title)
  80. {
  81.     char *p;
  82.     
  83.     p = path;
  84.  
  85.     if(*title && *(title + 1) == ':') ;
  86.     else if(*title == '\\')
  87.     {
  88.         if(*p && *(p + 1) == ':') p += 2;
  89.     }
  90.     else
  91.     {
  92.         while(*p)
  93.         {
  94.             if((*p == '\\' || *p == '/') && *(p + 1) == 0)
  95.             {
  96.                 break;
  97.             }
  98.             p = CharNext(p);
  99.         }
  100.         *p++ = '\\';
  101.     }
  102.     while(*title) *p++ = *title++;
  103.     *p = 0;
  104. }
  105.  
  106. /*-------------------------------------------
  107.  パス名からファイル名をとりのぞく
  108. ---------------------------------------------*/
  109. void del_title(char *path)
  110. {
  111.     char *p, *ep;
  112.  
  113.     p = ep = path;
  114.     while(*p)
  115.     {
  116.         if(*p == '\\' || *p == '/')
  117.         {
  118.             if(p > path && *(p - 1) == ':') ep = p + 1;
  119.             else ep = p;
  120.         }
  121.         p = CharNext(p);
  122.     }
  123.     *ep = 0;
  124. }
  125.  
  126. /*-------------------------------------------
  127.  パス名からファイル名を得る
  128. ---------------------------------------------*/
  129. void get_title(char* dst, const char *path)
  130. {
  131.     const char *p, *ep;
  132.  
  133.     p = ep = path;
  134.     while(*p)
  135.     {
  136.         if(*p == '\\' || *p == '/')
  137.         {
  138.             if(p > path && *(p - 1) == ':') ep = p + 1;
  139.             else ep = p;
  140.         }
  141.         p = CharNext(p);
  142.     }
  143.     
  144.     if(*ep == '\\' || *ep == '/') ep++;
  145.     
  146.     while(*ep) *dst++ = *ep++;
  147.     *dst = 0;
  148. }
  149.  
  150. /*------------------------------------------------
  151.  ファイルの拡張子の比較
  152. --------------------------------------------------*/
  153. int ext_cmp(const char *fname, const char *ext)
  154. {
  155.     const char* p, *sp;
  156.     
  157.     sp = NULL; p = fname;
  158.     while(*p)
  159.     {
  160.         if(*p == '.') sp = p;
  161.         else if(*p == '\\' || *p == '/') sp = NULL;
  162.         p = CharNext(p);
  163.     }
  164.     
  165.     if(sp == NULL) sp = p;
  166.     if(*sp == '.') sp++;
  167.     
  168.     while(1)
  169.     {
  170.         if(*sp == 0 && *ext == 0) return 0;
  171.         if(toupper(*sp) != toupper(*ext))
  172.             return (toupper(*sp) - toupper(*ext));
  173.         sp++; ext++;
  174.     }
  175.     return 0;
  176. }
  177.  
  178. /*------------------------------------------------
  179.     カンマで区切られた文字列を取り出す
  180. --------------------------------------------------*/
  181. void parse(char *dst, char *src, int n)
  182. {
  183.     char *dp;
  184.     int i;
  185.  
  186.     for(i = 0; i < n; i++)
  187.     {
  188.         while(*src && *src != ',') src++;
  189.         if(*src == ',') src++;
  190.     }
  191.     if(*src == 0) 
  192.     {
  193.         *dst = 0; return;
  194.     }
  195.     
  196.     while(*src == ' ') src++;
  197.     
  198.     dp = dst;
  199.     while(*src && *src != ',') *dst++ = *src++;
  200.     *dst = 0;
  201.     
  202.     while(dst != dp)
  203.     {
  204.         dst--;
  205.         if(*dst == ' ') *dst = 0;
  206.         else break;
  207.     }
  208. }
  209.  
  210. /*------------------------------------------------
  211.     文字で区切られた文字列を取り出す
  212. --------------------------------------------------*/
  213. void parsechar(char *dst, char *src, char ch, int n)
  214. {
  215.     char *dp;
  216.     int i;
  217.  
  218.     for(i = 0; i < n; i++)
  219.     {
  220.         while(*src && *src != ch) src++;
  221.         if(*src == ch) src++;
  222.     }
  223.     if(*src == 0) 
  224.     {
  225.         *dst = 0; return;
  226.     }
  227.     
  228.     while(*src == ' ') src++;
  229.     
  230.     dp = dst;
  231.     while(*src && *src != ch) *dst++ = *src++;
  232.     *dst = 0;
  233.     
  234.     while(dst != dp)
  235.     {
  236.         dst--;
  237.         if(*dst == ' ') *dst = 0;
  238.         else break;
  239.     }
  240. }
  241.  
  242. /*------------------------------------------------
  243.  '\0'で終了する文字列を追加する
  244.  最後は"\0\0"で終了
  245. --------------------------------------------------*/
  246. void str0cat(char* dst, const char* src)
  247. {
  248.     char* p;
  249.     p = dst;
  250.     while(*p) { while(*p) p++; p++; }
  251.     strcpy(p, src);
  252.     while(*p) p++; p++; *p = 0;
  253. }
  254.  
  255. /*-------------------------------------------
  256.   returns a resource string
  257. ---------------------------------------------*/
  258. char* MyString(UINT id)
  259. {
  260.     static char buf[80];
  261.     HINSTANCE hInst;
  262.     
  263.     buf[0] = 0;
  264.     hInst = GetLangModule();
  265.     if(hInst) LoadString(hInst, id, buf, 80);
  266.     
  267.     return buf;
  268. }
  269.  
  270. /*-------------------------------------------
  271.   アイコンつきメッセージボックス
  272. ---------------------------------------------*/
  273. int MyMessageBox(HWND hwnd, char* msg, char* title, UINT uType, UINT uBeep)
  274. {
  275.     MSGBOXPARAMS mbp;
  276.     
  277.     memset(&mbp, 0, sizeof(MSGBOXPARAMS));
  278.     mbp.cbSize = sizeof(MSGBOXPARAMS);
  279.     mbp.hwndOwner = hwnd;
  280.     mbp.hInstance = g_hInst;
  281.     mbp.lpszText = msg;
  282.     mbp.lpszCaption = title;
  283.     mbp.dwStyle = MB_USERICON | uType;
  284.     mbp.lpszIcon = MAKEINTRESOURCE(IDI_ICON1);
  285.     mbp.dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
  286.     if(uBeep != 0xFFFFFFFF)
  287.         MessageBeep(uBeep);
  288.     return MessageBoxIndirect(&mbp);
  289. }
  290.  
  291. /*------------------------------------------------
  292.   get locale info for 95/NT
  293. --------------------------------------------------*/
  294. int GetLocaleInfoWA(int ilang, LCTYPE LCType, char* dst, int n)
  295. {
  296.     int r;
  297.     LCID Locale;
  298.     
  299.     *dst = 0;
  300.     Locale = MAKELCID((WORD)ilang, SORT_DEFAULT);
  301.     if(GetVersion() & 0x80000000) // 95
  302.         r = GetLocaleInfoA(Locale, LCType, dst, n);
  303.     else  // NT
  304.     {
  305.         WCHAR* pw;
  306.         pw = (WCHAR*)GlobalAllocPtr(GHND, sizeof(WCHAR)*(n+1));
  307.         r = GetLocaleInfoW(Locale, LCType, pw, n);
  308.         if(r)
  309.             WideCharToMultiByte(CP_ACP, 0, pw, -1, dst, n,
  310.                 NULL, NULL);
  311.         GlobalFreePtr(pw);
  312.     }
  313.     return r;
  314. }
  315.  
  316. /*-------------------------------------------
  317.   32bit x 32bit = 64bit
  318. ---------------------------------------------*/
  319. DWORDLONG M32x32to64(DWORD a, DWORD b)
  320. {
  321.     ULARGE_INTEGER r;
  322.     DWORD *p1, *p2, *p3;
  323.     memset(&r, 0, 8);
  324.     p1 = &r.LowPart;
  325.     p2 = (DWORD*)((BYTE*)p1 + 2);
  326.     p3 = (DWORD*)((BYTE*)p2 + 2);
  327.     *p1 = LOWORD(a) * LOWORD(b);
  328.     *p2 += LOWORD(a) * HIWORD(b) + HIWORD(a) * LOWORD(b);
  329.     *p3 += HIWORD(a) * HIWORD(b);
  330.     return *(DWORDLONG*)(&r);
  331. }
  332.  
  333. /*-------------------------------------------
  334.   SetForegroundWindow for Windows98
  335. ---------------------------------------------*/
  336. void SetForegroundWindow98(HWND hwnd)
  337. {
  338.     DWORD dwVer;
  339.     
  340.     dwVer = GetVersion();
  341.     if(((dwVer & 0x80000000) && 
  342.            LOBYTE(LOWORD(dwVer)) >= 4 && HIBYTE(LOWORD(dwVer)) >= 10) ||
  343.        (!(dwVer & 0x80000000) && LOBYTE(LOWORD(dwVer)) >= 5)) // Win98/2000
  344.     {
  345.         DWORD thread1, thread2;
  346.         DWORD pid;
  347.         thread1 = GetWindowThreadProcessId(
  348.             GetForegroundWindow(), &pid);
  349.         thread2 = GetCurrentThreadId();
  350.         AttachThreadInput(thread2, thread1, TRUE);
  351.         SetForegroundWindow(hwnd);
  352.         AttachThreadInput(thread2, thread1, FALSE);
  353.         BringWindowToTop(hwnd);
  354.     }
  355.     else  // Win95/NT
  356.         SetForegroundWindow(hwnd);
  357. }
  358.  
  359. /*-------------------------------------------
  360.   for debugging
  361. ---------------------------------------------*/
  362. void WriteDebug(const char* s)
  363. {
  364.     HFILE hf;
  365.     char fname[] = "DEBUG.TXT";
  366.     
  367.     hf = _lopen(fname, OF_WRITE);
  368.     if(hf == HFILE_ERROR)
  369.         hf = _lcreat(fname, 0);
  370.     if(hf == HFILE_ERROR) return;
  371.     _llseek(hf, 0, 2);
  372.     _lwrite(hf, s, strlen(s));
  373.     _lwrite(hf, "\x0d\x0a", 2);
  374.     _lclose(hf);
  375. }
  376.  
  377. //TClock用のレジストリのキー
  378. char mykey[] = "Software\\Kazubon\\TClock";
  379.  
  380. /*------------------------------------------------
  381.  自分のレジストリから文字列を得る
  382. --------------------------------------------------*/
  383. int GetMyRegStr(char* section, char* entry, char* val, int cbData,
  384.     char* defval)
  385. {
  386.     char keystr[80];
  387.     HKEY hkey;
  388.     DWORD regtype;
  389.     DWORD size;
  390.     BOOL b;
  391.     int r;
  392.     
  393.     strcpy(keystr, mykey);
  394.     if(section && *section)
  395.     {
  396.         strcat(keystr, "\\"); strcat(keystr, section);
  397.     }
  398.     b = FALSE;
  399.     if(RegOpenKey(HKEY_CURRENT_USER, keystr, &hkey) == 0)
  400.     {
  401.         size = cbData;
  402.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  403.             (LPBYTE)val, &size) == 0)
  404.         {
  405.             if(size == 0) *val = 0;
  406.             r = size;
  407.             b = TRUE;
  408.         }
  409.         RegCloseKey(hkey);
  410.     }
  411.     if(b == FALSE)
  412.     {
  413.         strcpy(val, defval);
  414.         r = strlen(defval);
  415.     }
  416.     return r;
  417. }
  418.  
  419. /*------------------------------------------------
  420.  自分のレジストリからLONG値を得る
  421. --------------------------------------------------*/
  422. LONG GetMyRegLong(char* section, char* entry, LONG defval)
  423. {
  424.     char keystr[80];
  425.     HKEY hkey;
  426.     DWORD regtype;
  427.     DWORD size;
  428.     BOOL b;
  429.     LONG r;
  430.  
  431.     strcpy(keystr, mykey);
  432.     if(section && *section)
  433.     {
  434.         strcat(keystr, "\\"); strcat(keystr, section);
  435.     }
  436.     b = FALSE;
  437.     if(RegOpenKey(HKEY_CURRENT_USER, keystr, &hkey) == 0)
  438.     {
  439.         size = 4;
  440.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  441.             (LPBYTE)&r, &size) == 0)
  442.         {
  443.             if(size == 4) b = TRUE;
  444.         }
  445.         RegCloseKey(hkey);
  446.     }
  447.     if(b == FALSE) r = defval;
  448.     return r;
  449. }
  450.  
  451. /*------------------------------------------------
  452.   get DWORD value from registry
  453. --------------------------------------------------*/
  454. LONG GetRegLong(HKEY rootkey, char*subkey, char* entry, LONG defval)
  455. {
  456.     HKEY hkey;
  457.     DWORD regtype;
  458.     DWORD size;
  459.     BOOL b;
  460.     int r;
  461.     
  462.     b = FALSE;
  463.     if(RegOpenKey(rootkey, subkey, &hkey) == 0)
  464.     {
  465.         size = 4;
  466.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  467.             (LPBYTE)&r, &size) == 0)
  468.         {
  469.             if(size == 4) b = TRUE;
  470.         }
  471.         RegCloseKey(hkey);
  472.     }
  473.     if(b == FALSE) r = defval;
  474.     return r;
  475. }
  476.  
  477. /*------------------------------------------------
  478.  レジストリから文字列を得る
  479. --------------------------------------------------*/
  480. int GetRegStr(HKEY rootkey, char*subkey, char* entry,
  481.     char* val, int cbData, char* defval)
  482. {
  483.     HKEY hkey;
  484.     DWORD regtype;
  485.     DWORD size;
  486.     BOOL b;
  487.     int r;
  488.     
  489.     b = FALSE;
  490.     if(RegOpenKey(rootkey, subkey, &hkey) == 0)
  491.     {
  492.         size = cbData;
  493.         if(RegQueryValueEx(hkey, entry, 0, ®type,
  494.             (LPBYTE)val, &size) == 0)
  495.         {
  496.             if(size == 0) *val = 0;
  497.             b = TRUE;
  498.         }
  499.         RegCloseKey(hkey);
  500.     }
  501.     if(b == FALSE)
  502.     {
  503.         strcpy(val, defval);
  504.         r = strlen(defval);
  505.     }
  506.     return r;
  507. }
  508.  
  509. /*-------------------------------------------
  510.  レジストリに文字列を書き込む
  511. ---------------------------------------------*/
  512. BOOL SetMyRegStr(char* subkey, char* entry, char* val)
  513. {
  514.     HKEY hkey;
  515.     BOOL r;
  516.     char key[80];
  517.     
  518.     strcpy(key, mykey);
  519.     if(*subkey)
  520.     {
  521.         strcat(key, "\\"); strcat(key, subkey);
  522.     }
  523.     r = FALSE;
  524.     if(RegCreateKey(HKEY_CURRENT_USER, key, &hkey) == 0)
  525.     {
  526.         if(RegSetValueEx(hkey, entry, 0, REG_SZ,
  527.             (CONST BYTE*)val, strlen(val)) == 0)
  528.         {
  529.             r = TRUE;
  530.         }
  531.         RegCloseKey(hkey);
  532.     }
  533.     return r;
  534. }
  535.  
  536. /*-------------------------------------------
  537.  レジストリに文字列を書き込む
  538. ---------------------------------------------*/
  539. BOOL SetRegStr(HKEY rootkey, char* subkey, char* entry, char* val)
  540. {
  541.     HKEY hkey;
  542.     BOOL r;
  543.  
  544.     if(RegCreateKey(rootkey, subkey, &hkey) == 0)
  545.     {
  546.         if(RegSetValueEx(hkey, entry, 0, REG_SZ,
  547.             (CONST BYTE*)val, strlen(val)) == 0)
  548.         {
  549.             r = TRUE;
  550.         }
  551.         RegCloseKey(hkey);
  552.     }
  553.     return r;
  554. }
  555.  
  556. /*-------------------------------------------
  557.  レジストリにDWORD値を書き込む
  558. ---------------------------------------------*/
  559. BOOL SetMyRegLong(char* subkey, char* entry, DWORD val)
  560. {
  561.     HKEY hkey;
  562.     BOOL r;
  563.     char key[80];
  564.     
  565.     strcpy(key, mykey);
  566.     if(*subkey)
  567.     {
  568.         strcat(key, "\\"); strcat(key, subkey);
  569.     }
  570.     r = FALSE;
  571.     if(RegCreateKey(HKEY_CURRENT_USER, key, &hkey) == 0)
  572.     {
  573.         if(RegSetValueEx(hkey, entry, 0, REG_DWORD,
  574.             (CONST BYTE*)&val, 4) == 0)
  575.         {
  576.             r = TRUE;
  577.         }
  578.         RegCloseKey(hkey);
  579.     }
  580.     return r;
  581. }
  582.  
  583.  
  584. /*-------------------------------------------
  585.  レジストリの値を削除
  586. ---------------------------------------------*/
  587. BOOL DelMyReg(char* subkey, char* entry)
  588. {
  589.     BOOL r;
  590.     char key[80];
  591.     HKEY hkey;
  592.     
  593.     strcpy(key, mykey);
  594.     if(*subkey)
  595.     {
  596.         strcat(key, "\\"); strcat(key, subkey);
  597.     }
  598.     r = FALSE;
  599.     if(RegOpenKey(HKEY_CURRENT_USER, key, &hkey) == 0)
  600.     {
  601.         if(RegDeleteValue(hkey, entry) == 0)
  602.             r = TRUE;
  603.         RegCloseKey(hkey);
  604.     }
  605.     return r;
  606. }
  607.  
  608. /*-------------------------------------------
  609.  レジストリのキーを削除
  610. ---------------------------------------------*/
  611. BOOL DelMyRegKey(char* subkey)
  612. {
  613.     BOOL r;
  614.     char key[80];
  615.     
  616.     strcpy(key, mykey);
  617.     if(subkey && *subkey)
  618.     {
  619.         strcat(key, "\\"); strcat(key, subkey);
  620.     }
  621.     r = FALSE;
  622.     if(RegDeleteKey(HKEY_CURRENT_USER, key) == 0)
  623.         r = TRUE;
  624.     return r;
  625. }
  626.