📄 gbx_local.c
字号:
if (len_fmt >= COMMON_BUF_MAX) return TRUE; /* on recherche les formats de nombre n�atif et nul */ pos = search(fmt, len_fmt, ";", 0, FALSE); if (number <= 0.0) { if (pos < len_fmt) { if (number < 0.0) { if ((pos < (len_fmt - 1)) && fmt[pos + 1] != ';') { fmt = &fmt[pos + 1]; len_fmt -= pos + 1; } else len_fmt = pos; } else /* nombre �al �0 */ { pos2 = search(fmt, len_fmt, ";", pos + 1, FALSE); if (pos2 < len_fmt) { if ((pos2 < (len_fmt - 1)) && fmt[pos2 + 1] != ';') { fmt = &fmt[pos2 + 1]; len_fmt -= pos2 + 1; } else len_fmt = pos; } } } } else if (pos < len_fmt) len_fmt = pos; /* on transcrit le format du basic en format pour sprintf */ sign = 0; comma = FALSE; before = 0; before_zero = 0; point = FALSE; after = 0; after_zero = 0; exposant = 0; exp_sign = 0; exp_zero = 0; begin(); /* Recherche du '%' */ pos = search(fmt, len_fmt, "%", 0, FALSE); if (pos < len_fmt) number *= 100; /* pr�ixe de formatage */ pos = search(fmt, len_fmt, "-+#0.,", 0, FALSE); if (pos >= len_fmt) return TRUE; if (pos > 0) add_string(fmt, pos, NULL); /* on d�ermine le signe */ if (fmt[pos] == '-') { sign = ' '; pos++; } else if (fmt[pos] == '+') { sign = '+'; pos++; } if (pos >= len_fmt) return TRUE; /* Les chiffres avant la virgule */ for(; pos < len_fmt; pos++) { c = fmt[pos]; if (c == ',') { comma = TRUE; continue; } if (c == '#' || c == '0') { before++; if (c == '0' || before_zero > 0) before_zero++; continue; } break; } if (pos >= len_fmt) goto _FORMAT; /* La virgule */ if (fmt[pos] != '.') goto _FORMAT; pos++; point = TRUE; if (pos >= len_fmt) goto _FORMAT; /* Les chiffres apr� la virgule */ for(; pos < len_fmt; pos++) { c = fmt[pos]; if (c == '#' || c == '0') { after++; if (c == '0') after_zero = after; continue; } break; } if (pos >= len_fmt) goto _FORMAT; /* L'exposant */ if (fmt[pos] == 'e' || fmt[pos] == 'E') { exposant = fmt[pos]; exp_sign = ' '; pos++; if (pos >= len_fmt) return TRUE; if (fmt[pos] == '-') { pos++; } else if (fmt[pos] == '+') { exp_sign = '+'; pos++; } if (pos >= len_fmt) return TRUE; for(; pos < len_fmt; pos++) { c = fmt[pos]; if (c == '#' || c == '0') { exp_digit++; if (c == '0' || exp_zero > 0) exp_zero++; continue; } break; } }_FORMAT: if (before == 0 && after == 0) return TRUE; /* le signe */ number_sign = fsgn(number); add_sign(sign, number_sign); /* le nomber */ number_mant = frexp10(fabs(number), &number_exp); number_exp++; /* simplifie les choses */ number_real_exp = number_exp; if (exposant != 0) number_exp = 1; ndigit = sprintf(buf, "%.*f", MinMax(after + number_exp - 1 + 1, 0, DBL_DIG - 2), number_mant / 10); // should return "0[.]...", or "1[.]..." if the number is rounded up. buf_start = buf; if (buf_start[0] == '1') // the number has been rounded up. number_exp++; if (ndigit > 1) // so there is a point { if (buf_start[0] == '0') { buf_start += 2; ndigit -= 2; } else { buf_start[1] = buf_start[0]; ndigit--; buf_start++; } while (ndigit > 1 && buf_start[ndigit - 1] == '0') ndigit--; } /* les chiffres avant la virgule */ thousand = Max(before_zero, number_exp); thousand_ptr = comma ? &thousand : NULL; if (number_exp > 0) { if (before_zero > number_exp) add_zero(before_zero - number_exp, thousand_ptr); add_string(buf_start, Min(number_exp, ndigit), thousand_ptr); if (number_exp > ndigit) add_zero(number_exp - ndigit, thousand_ptr); } else { if (before_zero > 0) add_zero(before_zero, thousand_ptr); } /* la virgule */ if (point) put_char(local_current->decimal_point); /* les chiffres apr� la virgule */ if ((ndigit - number_exp) > 0) { if (number_exp < 0) { n = Min(after, (- number_exp)); if (n == after) { add_zero(after_zero, NULL); goto _EXPOSANT; } else { add_zero(n, NULL); after -= n; after_zero -= n; } } if (number_exp > 0) { buf_start += number_exp; ndigit -= number_exp; } n = Min(ndigit, after); add_string(buf_start, n, NULL); after -= n; after_zero -= n; if (after_zero > 0) add_zero(after_zero, NULL); } else add_zero(after_zero, NULL);_EXPOSANT: /* exposant */ if (exposant != 0 && number != 0.0) { put_char(exposant); n = sprintf(buf, "%+.*d", exp_zero, number_real_exp - 1); add_string(buf, n, NULL); } /* suffixe de formatage */ if (pos < len_fmt) add_string(&fmt[pos], len_fmt - pos, NULL); /* On enl�e la virgule si elle se trouve �la fin */ buffer_pos--; if (look_char() != local_current->decimal_point) buffer_pos++; /* on retourne le r�ultat */ end(str, len_str); return FALSE;}#endifPRIVATE void add_strftime(const char *format, struct tm *tm){ int n; n = strftime(get_current(), get_size_left(), format, tm); buffer_pos += n;}PRIVATE void add_date_token(DATE_SERIAL *date, char *token, int count){ struct tm tm; char buf[8]; int n; if (*token == 0) return; switch (*token) { case 'd': if (count <= 2) { n = sprintf(buf, (count == 1 ? "%d" : "%02d"), date->day); add_string(buf, n, NULL); } else if (count >= 3) { tm.tm_wday = date->weekday; add_strftime(count == 3 ? "%a" : "%A", &tm); } break; case 'm': if (count <= 2) { n = sprintf(buf, (count == 1 ? "%d" : "%02d"), date->month); add_string(buf, n, NULL); } else if (count >= 3) { tm.tm_mon = date->month - 1; add_strftime(count == 3 ? "%b" : "%B", &tm); } break; case 'y': if (count <= 2 && date->year >= 1939 && date->year <= 2038) n = sprintf(buf, "%02d", date->year - (date->year >= 2000 ? 2000 : 1900)); else n = sprintf(buf, "%d", date->year); add_string(buf, n, NULL); break; case 'h': case 'n': case 's': n = sprintf(buf, (count == 1) ? "%d" : "%02d", (*token == 'h') ? date->hour : ((*token == 'n') ? date->min : date->sec)); add_string(buf, n, NULL); break; } *token = 0;}PUBLIC boolean LOCAL_format_date(DATE_SERIAL *date, int fmt_type, const char *fmt, long len_fmt, char **str, long *len_str){ char c; long pos; long pos_ampm = -1; struct tm date_tm; char real_hour = 0; char token; int token_count; local_current = &LOCAL_local; switch(fmt_type) { case LF_USER: break; case LF_STANDARD: case LF_GENERAL_DATE: if (date->year == 0) fmt = local_current->long_time; else if (date->hour == 0 && date->min == 0 && date->sec == 0) fmt = local_current->short_date; else fmt = local_current->general_date; break; case LF_LONG_DATE: fmt = local_current->long_date; break; case LF_MEDIUM_DATE: fmt = local_current->medium_date; break; case LF_SHORT_DATE: fmt = local_current->short_date; break; case LF_LONG_TIME: fmt = local_current->long_time; break; case LF_MEDIUM_TIME: fmt = local_current->medium_time; break; case LF_SHORT_TIME: fmt = local_current->short_time; break; default: return TRUE; } if (len_fmt == 0) len_fmt = strlen(fmt); if (len_fmt >= COMMON_BUF_MAX) return TRUE; /* recherche de AM/PM */ for (pos = 0; pos < len_fmt - 4; pos++) { if (fmt[pos] == '\\') { pos++; continue; } if (strncasecmp(&fmt[pos], "am/pm", 5) == 0) { pos_ampm = pos; real_hour = date->hour; if (date->hour >= 12) date->hour -= 12; if (date->hour == 0) date->hour = 12; break; } } /* Formatage */ begin(); token = 0; token_count = 0; for (pos = 0; pos < len_fmt; pos++) { c = fmt[pos]; if (pos == pos_ampm) { /* passage en struct tm */ date_tm.tm_sec = date->sec; date_tm.tm_min = date->min; date_tm.tm_hour = real_hour; date_tm.tm_mday = 1; date_tm.tm_mon = 0; date_tm.tm_year = 0; add_strftime((c == 'a' ? "%P" : "%p"), &date_tm); pos += 4; continue; } if (index("dmyhns", c) != NULL) { if (c != token) { add_date_token(date, &token, token_count); if (token == 'h' && c == 'm') c = 'n'; token = c; token_count = 0; } token_count++; } else { add_date_token(date, &token, token_count); if (c == '/') put_char(local_current->date_sep); else if (c == ':') put_char(local_current->time_sep); else put_char(c); } } add_date_token(date, &token, token_count); /* on retourne le r�ultat */ end(str, len_str); return FALSE;}PUBLIC const char *LOCAL_gettext(const char *msgid){ const char *tr; if (local_trans) load_translation(); tr = gettext(msgid); /*printf("tr: %s -> %s\n", msgid, tr);*/ if (tr[0] == '-' && (tr[1] == 0 || (tr[1] == '\n' && tr[2] == 0))) return msgid; else return tr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -