File: vutil.cpp

package info (click to toggle)
libv1.22 1.22-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,380 kB
  • ctags: 9,765
  • sloc: cpp: 58,097; ansic: 6,814; makefile: 1,597; java: 515; sh: 24
file content (197 lines) | stat: -rw-r--r-- 4,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//===============================================================
// General Purpose V Utilities
//
// Copyright (C) 1995,1996, 1997, 1998  Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================

#include <v/vwin32.h>           // for Win 32 stuff
#include <v/v_defs.h>

#include <v/vutil.h>

#define __USELOCALES__
#include <time.h>

//#include <sys/time.h>


//=============================>>> vTextLen  <<<============================
  int vTextLen(const char *str, int& numLines)
  {
    // calculate length of multiline text string
    int l, maxl;
    maxl = 0;           // no length so far
    numLines = 1;
    for (l = 0 ; *str; str++)
      {
        if (*str != '\n' && *str != '\r')
          {
            ++l;
          }
        else
          {
            ++numLines;
            if (l > maxl)
                maxl = l;
            l = 0;
          }

      }
    if (l > maxl)
        maxl = l;
    return maxl;
  }

//=============================>>> ByteToStr   <<<============================
  void  ByteToStr(unsigned char intg, char* str)
  {  // convert byte intg to char string in str
 
    int i;
    int d, intval, j;
    char k;
    static char digits[] = "0123456789ABCDEF";
 
    intval = intg;
    str[0] = '\0';
    i = 0;
    do
      {                         // generate digits
        i++;
        d = intval % 16;        // mod 10
        str[i] = digits[d];
        intval = intval / 16;
      }
    while (intval != 0);

    for (j = 0 ; j < i ; j++ )
      {                         // then reverse
        k = str[i];
        str[i--] = str[j];
        str[j] = k;
      }
    if (str[1] == 0)            // one char only
      {
        str[1] = str[0]; str[2] = 0; str[0] = '0';
      }
  }

//=============================>>> vLblLen  <<<============================
  int vLblLen(const char *str)
  {
    int l;
    for (l = 0 ; *str; str++)
      if (*str != '&')
          ++l;
    return l;
  }

//=============================>>> l_to_a   <<<============================
  long StrToLong(VCONST char* str)
  {  // convert int intg to char string in str

    long val = 0;
    long neg  = 1;
    if (*str == '-')
      {
        neg = -1;
        ++str;
      }
    while (*str)
      {
	if (*str >= '0' && *str <= '9')
	    val = (val*10) + (*str++ - '0');
	else
	    break;
      }
    return val * neg;
  }

//=============================>>> l_to_a   <<<============================
  void IntToStr(int intg, char* str)
  {  // convert int intg to char string in str

    LongToStr((long)intg, str);
  }

//=============================>>> l_to_a   <<<============================
  void LongToStr(long intg, char* str)
  {  // convert long intg to char string in str
 
    long i;
    long d, intval, j;
    char k;
    static char digits[] = "0123456789";

    intval = intg >= 0L ? intg : (-intg);
    str[0] = '\0';
    i = 0;
    do
      {                         // generate digits
        i++;
        d = intval % 10L;       // mod 10
        str[i] = digits[d];
        intval = intval / 10L;
      }
    while (intval != 0L);

    if (intg < 0L)
      {                         // then sign
        str[++i] = '-';
      }
    for (j = 0 ; j < i ; j++ )
      {                         // then reverse
        k = str[i];
        str[i--] = str[j];
        str[j] = k;
      }
  }

//=============================>>> vGetLocalTime <<<==========================
  void vGetLocalTime(char* tm)
  {

    char buff[20];
    time_t t;
    t = time(0);
    strftime(buff, 20, "%X", localtime(&t));
    strcpy(tm,buff);
  }

//=============================>>> vGetLocalDate <<<==========================
  void vGetLocalDate(char* tm)
  {

    char buff[20];
    time_t t;
    t = time(0);
    strftime(buff, 20, "%d %b %Y", localtime(&t));
    strcpy(tm,buff);
  }

//=============================>>> vBeep <<<==========================
  void vBeep()
  {
     ::MessageBeep(MB_OK);
  }

//==================================================================
//
//    Some special case utils for working with CommandObjects
//
//=============================>>> vGetcmdIdIndex <<<==========================
  int vGetcmdIdIndex(ItemVal cmdId, CommandObject *cmdObj)
  {
    //    Return the index to the CommandObject array for the
    //    I which has the matching cmdId.
    for (int i = 0 ; cmdObj[i].cmdType != C_EndOfList ; ++i)
      {
        if (cmdObj[i].cmdId == cmdId)
            return i;
      }
    return -1;
  }