Changeset 108 for trunk/src/helpers/stringh.c
- Timestamp:
- Oct 13, 2001, 7:57:58 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/stringh.c
r105 r108 651 651 } 652 652 return (ulWords); 653 }654 655 /*656 *@@ strhThousandsULong:657 * converts a ULONG into a decimal string, while658 * inserting thousands separators into it. Specify659 * the separator character in cThousands.660 *661 * Returns pszTarget so you can use it directly662 * with sprintf and the "%s" flag.663 *664 * For cThousands, you should use the data in665 * OS2.INI ("PM_National" application), which is666 * always set according to the "Country" object.667 * You can use prfhQueryCountrySettings to668 * retrieve this setting.669 *670 * Use strhThousandsDouble for "double" values.671 */672 673 PSZ strhThousandsULong(PSZ pszTarget, // out: decimal as string674 ULONG ul, // in: decimal to convert675 CHAR cThousands) // in: separator char (e.g. '.')676 {677 USHORT ust, uss, usc;678 CHAR szTemp[40];679 sprintf(szTemp, "%lu", ul);680 681 ust = 0;682 usc = strlen(szTemp);683 for (uss = 0; uss < usc; uss++)684 {685 if (uss)686 if (((usc - uss) % 3) == 0)687 {688 pszTarget[ust] = cThousands;689 ust++;690 }691 pszTarget[ust] = szTemp[uss];692 ust++;693 }694 pszTarget[ust] = '\0';695 696 return (pszTarget);697 }698 699 /*700 *@@ strhThousandsDouble:701 * like strhThousandsULong, but for a "double"702 * value. Note that after-comma values are truncated.703 */704 705 PSZ strhThousandsDouble(PSZ pszTarget, double dbl, CHAR cThousands)706 {707 USHORT ust, uss, usc;708 CHAR szTemp[40];709 sprintf(szTemp, "%.0f", floor(dbl));710 711 ust = 0;712 usc = strlen(szTemp);713 for (uss = 0; uss < usc; uss++)714 {715 if (uss)716 if (((usc - uss) % 3) == 0)717 {718 pszTarget[ust] = cThousands;719 ust++;720 }721 pszTarget[ust] = szTemp[uss];722 ust++;723 }724 pszTarget[ust] = '\0';725 726 return (pszTarget);727 }728 729 /*730 *@@ strhVariableDouble:731 * like strhThousandsULong, but for a "double" value, and732 * with a variable number of decimal places depending on the733 * size of the quantity.734 *735 *@@added V0.9.6 (2000-11-12) [pr]736 */737 738 PSZ strhVariableDouble(PSZ pszTarget,739 double dbl,740 PSZ pszUnits,741 CHAR cThousands)742 {743 if (dbl < 100.0)744 sprintf(pszTarget, "%.2f%s", dbl, pszUnits);745 else746 if (dbl < 1000.0)747 sprintf(pszTarget, "%.1f%s", dbl, pszUnits);748 else749 strcat(strhThousandsDouble(pszTarget, dbl, cThousands),750 pszUnits);751 752 return(pszTarget);753 }754 755 /*756 *@@ strhFileDate:757 * converts file date data to a string (to pszBuf).758 * You can pass any FDATE structure to this function,759 * which are returned in those FILEFINDBUF* or760 * FILESTATUS* structs by the Dos* functions.761 *762 * ulDateFormat is the PM setting for the date format,763 * as set in the "Country" object, and can be queried using764 + PrfQueryProfileInt(HINI_USER, "PM_National", "iDate", 0);765 *766 * meaning:767 * -- 0 mm.dd.yyyy (English)768 * -- 1 dd.mm.yyyy (e.g. German)769 * -- 2 yyyy.mm.dd (Japanese, ISO)770 * -- 3 yyyy.dd.mm771 *772 * cDateSep is used as a date separator (e.g. '.').773 * This can be queried using:774 + prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');775 *776 * Alternatively, you can query all the country settings777 * at once using prfhQueryCountrySettings (prfh.c).778 *779 *@@changed V0.9.0 (99-11-07) [umoeller]: now calling strhDateTime780 */781 782 VOID strhFileDate(PSZ pszBuf, // out: string returned783 FDATE *pfDate, // in: date information784 ULONG ulDateFormat, // in: date format (0-3)785 CHAR cDateSep) // in: date separator (e.g. '.')786 {787 DATETIME dt;788 dt.day = pfDate->day;789 dt.month = pfDate->month;790 dt.year = pfDate->year + 1980;791 792 strhDateTime(pszBuf,793 NULL, // no time794 &dt,795 ulDateFormat,796 cDateSep,797 0, 0); // no time798 }799 800 /*801 *@@ strhFileTime:802 * converts file time data to a string (to pszBuf).803 * You can pass any FTIME structure to this function,804 * which are returned in those FILEFINDBUF* or805 * FILESTATUS* structs by the Dos* functions.806 *807 * ulTimeFormat is the PM setting for the time format,808 * as set in the "Country" object, and can be queried using809 + PrfQueryProfileInt(HINI_USER, "PM_National", "iTime", 0);810 * meaning:811 * -- 0 12-hour clock812 * -- >0 24-hour clock813 *814 * cDateSep is used as a time separator (e.g. ':').815 * This can be queried using:816 + prfhQueryProfileChar(HINI_USER, "PM_National", "sTime", ':');817 *818 * Alternatively, you can query all the country settings819 * at once using prfhQueryCountrySettings (prfh.c).820 *821 *@@changed V0.8.5 (99-03-15) [umoeller]: fixed 12-hour crash822 *@@changed V0.9.0 (99-11-07) [umoeller]: now calling strhDateTime823 */824 825 VOID strhFileTime(PSZ pszBuf, // out: string returned826 FTIME *pfTime, // in: time information827 ULONG ulTimeFormat, // in: 24-hour time format (0 or 1)828 CHAR cTimeSep) // in: time separator (e.g. ':')829 {830 DATETIME dt;831 dt.hours = pfTime->hours;832 dt.minutes = pfTime->minutes;833 dt.seconds = pfTime->twosecs * 2;834 835 strhDateTime(NULL, // no date836 pszBuf,837 &dt,838 0, 0, // no date839 ulTimeFormat,840 cTimeSep);841 }842 843 /*844 *@@ strhDateTime:845 * converts Control Program DATETIME info846 * into two strings. See strhFileDate and strhFileTime847 * for more detailed parameter descriptions.848 *849 *@@added V0.9.0 (99-11-07) [umoeller]850 */851 852 VOID strhDateTime(PSZ pszDate, // out: date string returned (can be NULL)853 PSZ pszTime, // out: time string returned (can be NULL)854 DATETIME *pDateTime, // in: date/time information855 ULONG ulDateFormat, // in: date format (0-3); see strhFileDate856 CHAR cDateSep, // in: date separator (e.g. '.')857 ULONG ulTimeFormat, // in: 24-hour time format (0 or 1); see strhFileTime858 CHAR cTimeSep) // in: time separator (e.g. ':')859 {860 if (pszDate)861 {862 switch (ulDateFormat)863 {864 case 0: // mm.dd.yyyy (English)865 sprintf(pszDate, "%02d%c%02d%c%04d",866 pDateTime->month,867 cDateSep,868 pDateTime->day,869 cDateSep,870 pDateTime->year);871 break;872 873 case 1: // dd.mm.yyyy (e.g. German)874 sprintf(pszDate, "%02d%c%02d%c%04d",875 pDateTime->day,876 cDateSep,877 pDateTime->month,878 cDateSep,879 pDateTime->year);880 break;881 882 case 2: // yyyy.mm.dd (Japanese)883 sprintf(pszDate, "%04d%c%02d%c%02d",884 pDateTime->year,885 cDateSep,886 pDateTime->month,887 cDateSep,888 pDateTime->day);889 break;890 891 default: // yyyy.dd.mm892 sprintf(pszDate, "%04d%c%02d%c%02d",893 pDateTime->year,894 cDateSep,895 pDateTime->day,896 cDateSep,897 pDateTime->month);898 break;899 }900 }901 902 if (pszTime)903 {904 if (ulTimeFormat == 0)905 {906 // for 12-hour clock, we need additional INI data907 CHAR szAMPM[10] = "err";908 909 if (pDateTime->hours > 12)910 {911 // > 12h: PM.912 913 // Note: 12:xx noon is 12 AM, not PM (even though914 // AM stands for "ante meridiam", but English is just915 // not logical), so that's handled below.916 917 PrfQueryProfileString(HINI_USER,918 "PM_National",919 "s2359", // key920 "PM", // default921 szAMPM, sizeof(szAMPM)-1);922 sprintf(pszTime, "%02d%c%02d%c%02d %s",923 // leave 12 == 12 (not 0)924 pDateTime->hours % 12,925 cTimeSep,926 pDateTime->minutes,927 cTimeSep,928 pDateTime->seconds,929 szAMPM);930 }931 else932 {933 // <= 12h: AM934 PrfQueryProfileString(HINI_USER,935 "PM_National",936 "s1159", // key937 "AM", // default938 szAMPM, sizeof(szAMPM)-1);939 sprintf(pszTime, "%02d%c%02d%c%02d %s",940 pDateTime->hours,941 cTimeSep,942 pDateTime->minutes,943 cTimeSep,944 pDateTime->seconds,945 szAMPM);946 }947 }948 else949 // 24-hour clock950 sprintf(pszTime, "%02d%c%02d%c%02d",951 pDateTime->hours,952 cTimeSep,953 pDateTime->minutes,954 cTimeSep,955 pDateTime->seconds);956 }957 653 } 958 654
Note:
See TracChangeset
for help on using the changeset viewer.