comparison src/win32/libc_interface.c @ 13362:f33077f19b60

[gaim-migrate @ 15735] TZ abbreviation mapping: - Change a couple NULL returns to the empty string, so things don't blow up. - Cache the lookup value for a standard comparison. By copying the value to the front of the array, we'll save a lot of unnecessary string comparisons. - Add newlines to the end of all the debug statements - Disable the debugging statements for normal cases, as this code is pretty solid I'd love it if someone running Windows could verify their newly created log files still have a TZ abbreviation in the name after these changes. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Wed, 01 Mar 2006 06:57:54 +0000
parents c6ab9c1cf28d
children cf292e05a6cc
comparison
equal deleted inserted replaced
13361:9b4a80566fd5 13362:f33077f19b60
810 HKEY rootKey; 810 HKEY rootKey;
811 int idx; 811 int idx;
812 812
813 if (!tm) 813 if (!tm)
814 { 814 {
815 gaim_debug_warning("wgaim", "could not determine current date/time: localtime failed"); 815 gaim_debug_warning("wgaim", "could not determine current date/time: localtime failed\n");
816 return NULL; 816 return "";
817 } 817 }
818 818
819 memset(tzname, 0, sizeof(tzname)); 819 if (strftime(tzname, sizeof(tzname) - 1, "%Z", tm) == 0)
820 strftime(tzname, sizeof(tzname) - 1, "%Z", tm); 820 {
821 gaim_debug_error("wgaim", "timezone name is too long for the buffer\n");
822 return "";
823 }
821 824
822 for (i = 0; win32_tzmap[i].wstd != NULL; i++) 825 for (i = 0; win32_tzmap[i].wstd != NULL; i++)
823 { 826 {
824 if (strcmp(tzname, win32_tzmap[i].wstd) == 0) 827 if (strcmp(tzname, win32_tzmap[i].wstd) == 0)
825 { 828 {
826 gaim_debug_info("wgaim", "TZ \"%s\" matches Windows timezone \"%s\"", 829 #if 0
830 gaim_debug_info("wgaim", "TZ \"%s\" matches Windows timezone \"%s\"\n",
827 win32_tzmap[i].ustd, tzname); 831 win32_tzmap[i].ustd, tzname);
832 #endif
833 /* Cache the Result */
834 if (win32_tzmap[0].wstd[0] != '\0')
835 g_free(win32_tzmap[0].wstd);
836 win32_tzmap[0].wstd = g_strdup(tzname);
837 win32_tzmap[0].ustd = win32_tzmap[i].ustd;
838
828 return win32_tzmap[i].ustd; 839 return win32_tzmap[i].ustd;
829 } 840 }
830 if (strcmp(tzname, win32_tzmap[i].wdst) == 0) 841 if (strcmp(tzname, win32_tzmap[i].wdst) == 0)
831 { 842 {
832 gaim_debug_info("wgaim", "TZ \"%s\" matches Windows timezone \"%s\"", 843 #if 0
844 gaim_debug_info("wgaim", "TZ \"%s\" matches Windows timezone \"%s\"\n",
833 win32_tzmap[i].udst, tzname); 845 win32_tzmap[i].udst, tzname);
846 #endif
847 /* Cache the Result */
848 if (win32_tzmap[0].wdst[0] != '\0')
849 g_free(win32_tzmap[0].wdst);
850 win32_tzmap[0].wdst = g_strdup(tzname);
851 win32_tzmap[0].udst = win32_tzmap[i].udst;
852
834 return win32_tzmap[i].udst; 853 return win32_tzmap[i].udst;
835 } 854 }
836 } 855 }
837 856
838 /* 857 /*
845 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 864 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
846 0, 865 0,
847 KEY_READ, 866 KEY_READ,
848 &rootKey) != ERROR_SUCCESS) 867 &rootKey) != ERROR_SUCCESS)
849 { 868 {
850 gaim_debug_warning("wgaim", "could not open registry key to identify Windows timezone: %i", (int) GetLastError()); 869 gaim_debug_warning("wgaim", "could not open registry key to identify Windows timezone: %i\n", (int) GetLastError());
851 return NULL; 870 return "";
852 } 871 }
853 872
854 for (idx = 0;; idx++) 873 for (idx = 0;; idx++)
855 { 874 {
856 char keyname[256]; 875 char keyname[256];
871 NULL, 890 NULL,
872 &lastwrite)) != ERROR_SUCCESS) 891 &lastwrite)) != ERROR_SUCCESS)
873 { 892 {
874 if (r == ERROR_NO_MORE_ITEMS) 893 if (r == ERROR_NO_MORE_ITEMS)
875 break; 894 break;
876 gaim_debug_warning("wgaim", "could not enumerate registry subkeys to identify Windows timezone: %i", (int) r); 895 gaim_debug_warning("wgaim", "could not enumerate registry subkeys to identify Windows timezone: %i\n", (int) r);
877 break; 896 break;
878 } 897 }
879 898
880 if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS) 899 if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS)
881 { 900 {
882 gaim_debug_warning("wgaim", "could not open registry subkey to identify Windows timezone: %i", (int) r); 901 gaim_debug_warning("wgaim", "could not open registry subkey to identify Windows timezone: %i\n", (int) r);
883 break; 902 break;
884 } 903 }
885 904
886 memset(zonename, 0, sizeof(zonename)); 905 memset(zonename, 0, sizeof(zonename));
887 namesize = sizeof(zonename); 906 namesize = sizeof(zonename);
888 if ((r = RegQueryValueEx(key, "Std", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS) 907 if ((r = RegQueryValueEx(key, "Std", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS)
889 { 908 {
890 gaim_debug_warning("wgaim", "could not query value for 'std' to identify Windows timezone: %i", (int) r); 909 gaim_debug_warning("wgaim", "could not query value for 'std' to identify Windows timezone: %i\n", (int) r);
891 RegCloseKey(key); 910 RegCloseKey(key);
892 break; 911 break;
893 } 912 }
894 if (strcmp(tzname, zonename) == 0) 913 if (strcmp(tzname, zonename) == 0)
895 { 914 {
896 /* Matched zone */ 915 /* Matched zone */
897 strcpy(localtzname, keyname); 916 gaim_strlcpy(localtzname, keyname);
898 RegCloseKey(key); 917 RegCloseKey(key);
899 break; 918 break;
900 } 919 }
901 memset(zonename, 0, sizeof(zonename)); 920 memset(zonename, 0, sizeof(zonename));
902 namesize = sizeof(zonename); 921 namesize = sizeof(zonename);
903 if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS) 922 if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS)
904 { 923 {
905 gaim_debug_warning("wgaim", "could not query value for 'dlt' to identify Windows timezone: %i", (int) r); 924 gaim_debug_warning("wgaim", "could not query value for 'dlt' to identify Windows timezone: %i\n", (int) r);
906 RegCloseKey(key); 925 RegCloseKey(key);
907 break; 926 break;
908 } 927 }
909 if (strcmp(tzname, zonename) == 0) 928 if (strcmp(tzname, zonename) == 0)
910 { 929 {
911 /* Matched DST zone */ 930 /* Matched DST zone */
912 strcpy(localtzname, keyname); 931 gaim_strlcpy(localtzname, keyname);
913 RegCloseKey(key); 932 RegCloseKey(key);
914 break; 933 break;
915 } 934 }
916 935
917 RegCloseKey(key); 936 RegCloseKey(key);
924 /* Found a localized name, so scan for that one too */ 943 /* Found a localized name, so scan for that one too */
925 for (i = 0; win32_tzmap[i].wstd != NULL; i++) 944 for (i = 0; win32_tzmap[i].wstd != NULL; i++)
926 { 945 {
927 if (strcmp(localtzname, win32_tzmap[i].wstd) == 0) 946 if (strcmp(localtzname, win32_tzmap[i].wstd) == 0)
928 { 947 {
929 gaim_debug_info("wgaim", "TZ \"%s\" matches localized Windows timezone \"%s\" (\"%s\")", 948 #if 0
949 gaim_debug_info("wgaim", "TZ \"%s\" matches localized Windows timezone \"%s\" (\"%s\")\n",
930 win32_tzmap[i].ustd, tzname, localtzname); 950 win32_tzmap[i].ustd, tzname, localtzname);
931 951 #endif
932 /* Cache the Result */ 952 /* Cache the Result */
933 if (win32_tzmap[0].wstd[0] != '\0') 953 if (win32_tzmap[0].wstd[0] != '\0')
934 g_free(win32_tzmap[0].wstd); 954 g_free(win32_tzmap[0].wstd);
935 win32_tzmap[0].wstd = g_strdup(tzname); 955 win32_tzmap[0].wstd = g_strdup(tzname);
936 win32_tzmap[0].ustd = win32_tzmap[i].ustd; 956 win32_tzmap[0].ustd = win32_tzmap[i].ustd;
937 957
938 return win32_tzmap[i].ustd; 958 return win32_tzmap[i].ustd;
939 } 959 }
940 if (strcmp(localtzname, win32_tzmap[i].wdst) == 0) 960 if (strcmp(localtzname, win32_tzmap[i].wdst) == 0)
941 { 961 {
942 gaim_debug_info("wgaim", "TZ \"%s\" matches localized Windows timezone \"%s\" (\"%s\")", 962 #if 0
963 gaim_debug_info("wgaim", "TZ \"%s\" matches localized Windows timezone \"%s\" (\"%s\")\n",
943 win32_tzmap[i].udst, tzname, localtzname); 964 win32_tzmap[i].udst, tzname, localtzname);
944 965 #endif
945 /* Cache the Result */ 966 /* Cache the Result */
946 if (win32_tzmap[0].wdst[0] != '\0') 967 if (win32_tzmap[0].wdst[0] != '\0')
947 g_free(win32_tzmap[0].wdst); 968 g_free(win32_tzmap[0].wdst);
948 969
949 win32_tzmap[0].wdst = g_strdup(tzname); 970 win32_tzmap[0].wdst = g_strdup(tzname);
952 return win32_tzmap[i].udst; 973 return win32_tzmap[i].udst;
953 } 974 }
954 } 975 }
955 } 976 }
956 977
957 gaim_debug_warning("wgaim", "could not find a match for Windows timezone \"%s\"", tzname); 978 gaim_debug_warning("wgaim", "could not find a match for Windows timezone \"%s\"\n", tzname);
958 return ""; 979 return "";
959 } 980 }
960 981