/[cvs]/joko/Uni/BSArch/03/win32/utils_misc.c
ViewVC logotype

Annotation of /joko/Uni/BSArch/03/win32/utils_misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Fri Jun 16 19:39:07 2006 UTC (18 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.2: +64 -1 lines
File MIME type: text/plain
finally got "trim" working

1 joko 1.3 /* $Id: utils_misc.c,v 1.2 2006/06/16 00:06:11 joko Exp $ */
2 joko 1.1
3     #include <stdio.h>
4     #include <windows.h>
5    
6     // if last char is newline, "strip" it
7     void chomp(char * string) {
8     int string_length = strlen(string);
9     if (string[string_length-1] == '\n') {
10     string[string_length-1] = '\0';
11     }
12     }
13    
14 joko 1.3 void trim(char ** string) {
15    
16     // string length and loop variable
17     int string_length, i;
18    
19     // contains current character at string position i
20     char current_char[2];
21    
22     // pointers to begin end tail of new (stripped) string
23     char * begin;
24     char * tail;
25    
26     // prepare to be used with "strcmp" (comparison string should be terminated properly)
27     current_char[1] = '\0';
28    
29    
30     /*****************************
31     * 1. strip leading spaces
32     **/
33     begin = *string;
34     string_length = strlen(*string);
35    
36     // loop through all string characters (from left to right)
37     for (i = 0; i < string_length; i++) {
38    
39     // get character at position i
40     current_char[0] = *(*string + i);
41    
42     // is it a space? then increase pointer to begin of "new" string
43     if (strcmp(current_char, " ") == 0) {
44     begin++;
45     } else {
46     break;
47     }
48     }
49     *string = begin;
50    
51    
52     /*****************************
53     * 2. strip trailing spaces
54     **/
55    
56     // calculate new string length
57     string_length = strlen(*string);
58    
59     // loop through all string characters (from right to left)
60     for (i = 0; i < string_length - 1; i++) {
61    
62     // get character at position string_length - i
63     tail = *string + string_length - 1 - i;
64     current_char[0] = *tail;
65    
66     // is it a space? then set null-byte to indicate end of "new" string at current position
67     if (strcmp(current_char, " ") == 0) {
68     *tail = '\0';
69     } else {
70     break;
71     }
72     }
73    
74     }
75    
76    
77 joko 1.1 // checks for errors and writes its message to stderr
78     BOOL check_alert_error(const char * error_source) {
79     DWORD dwError = GetLastError();
80    
81     HLOCAL message = NULL;
82     BOOL fOk;
83    
84     if (dwError == 0)
85     return FALSE;
86    
87     fOk = FormatMessage(
88     FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
89     NULL,
90     dwError,
91     MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
92     (PTSTR)&message,
93     0,
94     NULL
95     );
96    
97     if (message != NULL) {
98 joko 1.2 fprintf(stderr, "ERROR with '%s': %s (Code %i)\n", error_source, message, dwError);
99 joko 1.1 LocalFree(message);
100     }
101    
102     return TRUE;
103    
104     }

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed