source: trunk/kStuff/kHlp/Bare/kHlpBareAssert.c@ 3599

Last change on this file since 3599 was 3599, checked in by bird, 18 years ago

Built on linux.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/* $Id: kHlpBareAssert.c 3599 2007-10-04 19:09:39Z bird $ */
2/** @file
3 * kHlpBare - Assert Backend.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <k/kHlpAssert.h>
31#include <k/kHlpString.h>
32
33#if K_OS == K_OS_DARWIN \
34 || K_OS == K_OS_FREEBSD \
35 || K_OS == K_OS_LINUX \
36 || K_OS == K_OS_NETBSD \
37 || K_OS == K_OS_OPENBSD \
38 || K_OS == K_OS_SOLARIS
39# include <k/kHlpSys.h>
40
41#elif K_OS == K_OS_OS2
42# define INCL_BASE
43# define INCL_ERRORS
44# include <os2.h>
45
46#elif K_OS == K_OS_WINDOWS
47# include <Windows.h>
48
49#else
50# error "port me"
51#endif
52
53
54/**
55 * Writes a assert string with unix lineendings.
56 *
57 * @param pszMsg The string.
58 */
59static void kHlpAssertWrite(const char *pszMsg)
60{
61#if K_OS == K_OS_DARWIN \
62 || K_OS == K_OS_FREEBSD \
63 || K_OS == K_OS_LINUX \
64 || K_OS == K_OS_NETBSD \
65 || K_OS == K_OS_OPENBSD \
66 || K_OS == K_OS_SOLARIS
67 KSIZE cchMsg = kHlpStrLen(pszMsg);
68 kHlpSys_write(2 /* stderr */, pszMsg, cchMsg);
69
70#elif K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
71 /*
72 * Line by line.
73 */
74 ULONG cbWritten;
75 const char *pszNl = kHlpStrChr(pszMsg, '\n');
76 while (pszNl)
77 {
78 cbWritten = pszNl - pszMsg;
79
80#if K_OS == K_OS_OS2
81 if (cbWritten)
82 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
83 DosWrite((HFILE)2, "\r\n", 2, &cbWritten);
84#else /* K_OS == K_OS_WINDOWS */
85 if (cbWritten)
86 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
87 WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL);
88#endif
89
90 /* next */
91 pszMsg = pszNl + 1;
92 pszNl = kHlpStrChr(pszMsg, '\n');
93 }
94
95 /*
96 * Remaining incomplete line.
97 */
98 if (*pszMsg)
99 {
100 cbWritten = kHlpStrLen(pszMsg);
101#if K_OS == K_OS_OS2
102 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
103#else /* K_OS == K_OS_WINDOWS */
104 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
105#endif
106 }
107
108#else
109# error "port me"
110#endif
111}
112
113
114KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
115{
116 char szLine[16];
117
118 kHlpAssertWrite("\n!!!kLdr Assertion Failed!!!\nExpression: ");
119 kHlpAssertWrite(pszExpr);
120 kHlpAssertWrite("\nAt: ");
121 kHlpAssertWrite(pszFile);
122 kHlpAssertWrite("(");
123 kHlpAssertWrite(kHlpInt2Ascii(szLine, sizeof(szLine), iLine, 10));
124 kHlpAssertWrite(") ");
125 kHlpAssertWrite(pszFunction);
126 kHlpAssertWrite("\n");
127}
128
129
130KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...)
131{
132 kHlpAssertWrite(pszFormat);
133}
134
Note: See TracBrowser for help on using the repository browser.