Subversion Repositories SvarDOS

Rev

Rev 369 | Rev 392 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
352 mateuszvis 1
/*
2
 * a variety of helper functions
3
 * Copyright (C) 2021 Mateusz Viste
4
 */
5
 
6
#include "helpers.h"
7
 
8
/* case-insensitive comparison of strings, returns non-zero on equality */
9
int imatch(const char *s1, const char *s2) {
10
  for (;;) {
11
    char c1, c2;
12
    c1 = *s1;
13
    c2 = *s2;
14
    if ((c1 >= 'a') && (c1 <= 'z')) c1 -= ('a' - 'A');
15
    if ((c2 >= 'a') && (c2 <= 'z')) c2 -= ('a' - 'A');
16
    /* */
17
    if (c1 != c2) return(0);
18
    if (c1 == 0) return(1);
19
    s1++;
20
    s2++;
21
  }
22
}
23
 
24
 
25
/* returns zero if s1 starts with s2 */
26
int strstartswith(const char *s1, const char *s2) {
27
  while (*s2 != 0) {
28
    if (*s1 != *s2) return(-1);
29
    s1++;
30
    s2++;
31
  }
32
  return(0);
33
}
369 mateuszvis 34
 
35
 
36
/* outputs a NULL-terminated string to stdout */
37
void output_internal(const char *s, unsigned short nl) {
38
  _asm {
39
    mov ah, 0x02 /* AH=9 - write character in DL to stdout */
40
    mov si, s
41
    cld          /* clear DF so lodsb increments SI */
42
    NEXTBYTE:
43
    lodsb /* load byte from DS:SI into AL, SI++ */
44
    mov dl, al
45
    or al, 0  /* is al == 0? */
46
    jz DONE
47
    int 0x21
48
    jmp NEXTBYTE
49
    DONE:
50
    or nl, 0
51
    jz FINITO
52
    /* print out a CR/LF trailer if nl set */
53
    mov dl, 0x0D /* CR */
54
    int 0x21
55
    mov dl, 0x0A /* LF */
56
    int 0x21
57
    FINITO:
58
  }
59
}
388 mateuszvis 60
 
61
 
62
/* find first matching files using a FindFirst DOS call
63
 * returns 0 on success or a DOS err code on failure */
64
unsigned short findfirst(struct DTA *dta, const char *pattern, unsigned short attr) {
65
  unsigned short res = 0;
66
  _asm {
67
    /* set DTA location */
68
    mov ah, 0x1a
69
    mov dx, dta
70
    int 0x21
71
    /* */
72
    mov ah, 0x4e    /* FindFirst */
73
    mov dx, pattern
74
    mov cx, attr
75
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
76
    jnc DONE
77
    mov [res], ax
78
    DONE:
79
  }
80
  return(res);
81
}
82
 
83
 
84
/* find next matching, ie. continues an action intiated by findfirst() */
85
unsigned short findnext(struct DTA *dta) {
86
  unsigned short res = 0;
87
  _asm {
88
    mov ah, 0x4f    /* FindNext */
89
    mov dx, dta
90
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
91
    jnc DONE
92
    mov [res], ax
93
    DONE:
94
  }
95
  return(res);
96
}