Subversion Repositories SvarDOS

Rev

Rev 416 | Rev 421 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 416 Rev 420
Line 2... Line 2...
2
 * a variety of helper functions
2
 * a variety of helper functions
3
 * Copyright (C) 2021 Mateusz Viste
3
 * Copyright (C) 2021 Mateusz Viste
4
 */
4
 */
5
 
5
 
6
#include <i86.h>    /* MK_FP() */
6
#include <i86.h>    /* MK_FP() */
-
 
7
#include <stdio.h>  /* sprintf() */
7
 
8
 
8
#include "helpers.h"
9
#include "helpers.h"
9
 
10
 
-
 
11
 
10
/* case-insensitive comparison of strings, returns non-zero on equality */
12
/* case-insensitive comparison of strings, returns non-zero on equality */
11
int imatch(const char *s1, const char *s2) {
13
int imatch(const char *s1, const char *s2) {
12
  for (;;) {
14
  for (;;) {
13
    char c1, c2;
15
    char c1, c2;
14
    c1 = *s1;
16
    c1 = *s1;
Line 250... Line 252...
250
/* converts a 8+3 filename into 11-bytes FCB format (MYFILE  EXT) */
252
/* converts a 8+3 filename into 11-bytes FCB format (MYFILE  EXT) */
251
void file_fname2fcb(char *dst, const char *src) {
253
void file_fname2fcb(char *dst, const char *src) {
252
  unsigned short i;
254
  unsigned short i;
253
 
255
 
254
  /* fill dst with 11 spaces and a NULL terminator */
256
  /* fill dst with 11 spaces and a NULL terminator */
255
  for (i = 0; i < 12; i++) dst[i] = ' ';
257
  for (i = 0; i < 11; i++) dst[i] = ' ';
256
  dst[12] = 0;
258
  dst[11] = 0;
257
 
259
 
258
  /* copy fname until dot (.) or 8 characters */
260
  /* copy fname until dot (.) or 8 characters */
259
  for (i = 0; i < 8; i++) {
261
  for (i = 0; i < 8; i++) {
260
    if ((src[i] == '.') || (src[i] == 0)) break;
262
    if ((src[i] == '.') || (src[i] == 0)) break;
261
    dst[i] = src[i];
263
    dst[i] = src[i];
Line 397... Line 399...
397
    DONE:
399
    DONE:
398
  }
400
  }
399
 
401
 
400
  return(r);
402
  return(r);
401
}
403
}
-
 
404
 
-
 
405
 
-
 
406
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
-
 
407
unsigned short nls_getpatterns(struct nls_patterns *p) {
-
 
408
  unsigned short r = 0;
-
 
409
 
-
 
410
  _asm {
-
 
411
    mov ax, 0x3800  /* DOS 2+ -- Get Country Info for current country */
-
 
412
    mov dx, p       /* DS:DX points to the CountryInfoRec buffer */
-
 
413
    int 0x21
-
 
414
    jnc DONE
-
 
415
    mov [r], ax     /* copy DOS err code to r */
-
 
416
    DONE:
-
 
417
  }
-
 
418
 
-
 
419
  return(r);
-
 
420
}
-
 
421
 
-
 
422
 
-
 
423
/* computes a formatted date based on NLS patterns found in p
-
 
424
 * returns length of result */
-
 
425
unsigned short nls_format_date(char *s, unsigned short yr, unsigned char mo, unsigned char dy, const struct nls_patterns *p) {
-
 
426
  unsigned short items[3];
-
 
427
  /* preset date/month/year in proper order depending on date format */
-
 
428
  switch (p->dateformat) {
-
 
429
    case 0:  /* USA style: m d y */
-
 
430
      items[0] = mo;
-
 
431
      items[1] = dy;
-
 
432
      items[2] = yr;
-
 
433
      break;
-
 
434
    case 1:  /* EU style: d m y */
-
 
435
      items[0] = dy;
-
 
436
      items[1] = mo;
-
 
437
      items[2] = yr;
-
 
438
      break;
-
 
439
    case 2:  /* Japan-style: y m d */
-
 
440
    default:
-
 
441
      items[0] = yr;
-
 
442
      items[1] = mo;
-
 
443
      items[2] = dy;
-
 
444
      break;
-
 
445
  }
-
 
446
  /* compute the string */
-
 
447
  return(sprintf(s, "%02u%s%02u%s%02u", items[0], p->datesep, items[1], p->datesep, items[2]));
-
 
448
}
-
 
449
 
-
 
450
 
-
 
451
/* computes a formatted time based on NLS patterns found in p
-
 
452
 * returns length of result */
-
 
453
unsigned short nls_format_time(char *s, unsigned char ho, unsigned char mn, const struct nls_patterns *p) {
-
 
454
  char ampm[2] = {0, 0};
-
 
455
  const char *fmt = "%02u%s%02u%s";
-
 
456
  if (p->timefmt == 0) {
-
 
457
    if (ho == 12) {
-
 
458
      ampm[0] = 'p';
-
 
459
    } else if (ho > 12) {
-
 
460
      ho -= 12;
-
 
461
      ampm[0] = 'p';
-
 
462
    } else { /* ho < 12 */
-
 
463
      if (ho == 0) ho = 12;
-
 
464
      ampm[0] = 'a';
-
 
465
    }
-
 
466
    fmt = "%2u%s%02u%s";
-
 
467
  }
-
 
468
  return(sprintf(s, fmt, ho, p->timesep, mn, ampm));
-
 
469
}
-
 
470
 
-
 
471
 
-
 
472
/* computes a formatted integer number based on NLS patterns found in p
-
 
473
 * returns length of result */
-
 
474
unsigned short nls_format_number(char *s, long num, const struct nls_patterns *p) {
-
 
475
  unsigned short sl = 0, res, i;
-
 
476
  unsigned char thcount = 0;
-
 
477
 
-
 
478
  /* handle negative values */
-
 
479
  if (num < 0) {
-
 
480
    s[sl++] = '-';
-
 
481
    num = 0 - num;
-
 
482
  }
-
 
483
 
-
 
484
  /* write the absolute value now */
-
 
485
  do {
-
 
486
    if ((thcount == 3) && (p->thousep[0] != 0)) {
-
 
487
      s[sl++] = p->thousep[0];
-
 
488
      thcount = 0;
-
 
489
    }
-
 
490
    s[sl++] = '0' + num % 10;
-
 
491
    num /= 10;
-
 
492
    thcount++;
-
 
493
  } while (num > 0);
-
 
494
 
-
 
495
  /* terminate the string and remember its size */
-
 
496
  s[sl] = 0;
-
 
497
  res = sl;
-
 
498
 
-
 
499
  /* reverse the string now (it has been build in reverse) */
-
 
500
  if (s[0] == '-') {
-
 
501
    sl--;
-
 
502
    s++;
-
 
503
  }
-
 
504
  for (i = 0; i <= (sl / 2); i++) {
-
 
505
    thcount = s[i];
-
 
506
    s[i] = s[sl - (i + 1)];
-
 
507
    s[sl - (i + 1)] = thcount;
-
 
508
  }
-
 
509
 
-
 
510
  return(res);
-
 
511
}