File: Knox_Stddef.cpp

package info (click to toggle)
parsinsert 1.04-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 105,628 kB
  • sloc: cpp: 8,678; makefile: 108; sh: 15
file content (171 lines) | stat: -rw-r--r-- 5,082 bytes parent folder | download | duplicates (6)
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
/////////////////////////////////////////////////////////////////////////////////////////////////// 
//  File      : Knox_Stddef.cpp
//  Purpose   : Set of routines and old habit data types
//
//  Developer : David Knox (david.knox@colorado.edu) Jan 2011
//  Copyright : Copyright (C) 2007-2011 David Knox
//
//  Web site  : http://parsinsert.sourceforge.net/
//
/////////////////////////////////////////////////////////////////////////////////////////////////// 
//	  This file is part of ParsInsert.
//
//    ParsInsert is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    ParsInsert is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with ParsInsert.  If not, see <http://www.gnu.org/licenses/>.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "Knox_Stddef.h"
#include <ctype.h>

// File open for the application log
// File is always current (Flushed after each write)
FILE				*appLog = NULL;

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Application Log: Manages a log file.  Display functions will print to 
//					multiple locations (Stdout, Application log)
//

void OpenAppLog(LPCSTR filename, LPCSTR mode)
	{
	if (appLog != NULL)
		CloseAppLog();
		
	appLog = fopen(filename, mode);
	}

void CloseAppLog()
	{
	if (appLog != NULL)
		fclose(appLog);
		
	appLog = NULL;
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Display functions will write to multiple locations (Stdout, Applog, ...)

void DisplayT(LPSTR format, ...)
	{
	char		msg[64*1024];
	va_list args;
	va_start(args, format);     /* Initialize variable arguments. */
	vsprintf(msg, format, args);
	printf("%s", msg);
	fflush(stdout);
	//TRACE("%s", msg);
	va_end(args);              /* Reset variable arguments.      */
	}

void Display(LPSTR format, ...)
	{
	char		msg[64*1024];
	va_list args;
	va_start(args, format);     /* Initialize variable arguments. */
	vsprintf(msg, format, args);
	printf("%s", msg);
	va_end(args);              /* Reset variable arguments.      */
	}

void DisplayL(LPSTR format, ...)
	{
	char		msg[64*1024];
	va_list args;
	va_start(args, format);     /* Initialize variable arguments. */
	vsprintf(msg, format, args);
	printf("%s", msg);
	if (appLog != NULL)
		{
		int		last = strlen(msg);
		if (last > 0) --last;

		fprintf(appLog, "%s%s", msg, (msg[last]!='\n'?"\n":""));
		fflush(appLog);
		}

	va_end(args);              /* Reset variable arguments.      */
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

// Read the next line from a file.  Skips empty lines or lines begining 
//		with "//" or "#".  Removes the trailing CR/LF/space.
//
BOOL ReadNextLine(LPSTR line, int len, FILE *f)
	{
	while ((f != NULL) && !feof(f) && fgets(line, len-1, f))
		{
		// skip empty lines, commented lines, lines without enough data
		if (strlen(line) == 0)	continue;
		if ((line[0] == '/') && (line[1] == '/')) continue;
		if (line[0] == '#') continue;

		// remove ending CR/LF and spaces
		for (int i=strlen(line) ; i > 0 ; i--)
			{
			if (   (line[i-1] == '\r') 
				|| (line[i-1] == '\n') 
				|| (isspace(line[i-1])))
				line[i-1] = 0;
			else 
				break;
			}
		return TRUE;
		}

	return FALSE;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Trim unwanted characters from ends of strings
//
void Trim(string& s, LPCSTR unwanted)
	{
	TrimLeft(s, unwanted);
	TrimRight(s, unwanted);
	}
	
void TrimLeft(string& s, LPCSTR unwanted)
	{
	int start = s.find_first_not_of(unwanted);
	s = s.substr(start, s.length());
	}
	
void TrimRight(string& s, LPCSTR unwanted)
	{
	int		end = s.length();
	while ((end > 0) &&  (strchr(unwanted, s[end-1]) != NULL))
		--end;
	s = s.substr(0, end);
	}

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Convert string to all one case	
//
void ToLower(string& s)
	{
	for (int i=0 ; i < s.length() ; ++i)
		s[i] = tolower(s[i]);
	}
	
void ToUpper(string& s)
	{
	for (int i=0 ; i < s.length() ; ++i)
		s[i] = toupper(s[i]);
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////