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

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

Made it build on darwin - leaving a couple of things for later...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1/* $Id: kHlpBareAssert.c 3594 2007-10-02 21:35:22Z 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
34extern kHlpSys_write(int fd, const void *buf, KSIZE);
35
36#elif K_OS == K_OS_OS2
37# define INCL_BASE
38# define INCL_ERRORS
39# include <os2.h>
40
41#elif K_OS == K_OS_WINDOWS
42# include <Windows.h>
43
44#else
45# error "port me"
46#endif
47
48
49/**
50 * Writes a assert string with unix lineendings.
51 *
52 * @param pszMsg The string.
53 */
54static void kHlpAssertWrite(const char *pszMsg)
55{
56#if K_OS == K_OS_DARWIN
57 KSIZE cchMsg = kHlpStrLen(pszMsg);
58 kHlpSys_write(2 /* stderr */, pszMsg, cchMsg);
59
60#elif K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
61 /*
62 * Line by line.
63 */
64 ULONG cbWritten;
65 const char *pszNl = kHlpStrChr(pszMsg, '\n');
66 while (pszNl)
67 {
68 cbWritten = pszNl - pszMsg;
69
70#if K_OS == K_OS_OS2
71 if (cbWritten)
72 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
73 DosWrite((HFILE)2, "\r\n", 2, &cbWritten);
74#else /* K_OS == K_OS_WINDOWS */
75 if (cbWritten)
76 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
77 WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL);
78#endif
79
80 /* next */
81 pszMsg = pszNl + 1;
82 pszNl = kHlpStrChr(pszMsg, '\n');
83 }
84
85 /*
86 * Remaining incomplete line.
87 */
88 if (*pszMsg)
89 {
90 cbWritten = kHlpStrLen(pszMsg);
91#if K_OS == K_OS_OS2
92 DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
93#else /* K_OS == K_OS_WINDOWS */
94 WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
95#endif
96 }
97
98#else
99# error "port me"
100#endif
101}
102
103
104KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
105{
106 char szLine[16];
107
108 kHlpAssertWrite("\n!!!kLdr Assertion Failed!!!\nExpression: ");
109 kHlpAssertWrite(pszExpr);
110 kHlpAssertWrite("\nAt: ");
111 kHlpAssertWrite(pszFile);
112 kHlpAssertWrite("(");
113 kHlpAssertWrite(kHlpInt2Ascii(szLine, sizeof(szLine), iLine, 10));
114 kHlpAssertWrite(") ");
115 kHlpAssertWrite(pszFunction);
116 kHlpAssertWrite("\n");
117}
118
119
120KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...)
121{
122 kHlpAssertWrite(pszFormat);
123}
124
Note: See TracBrowser for help on using the repository browser.