1/** Implementation of ObjC runtime for GNUStep
2   Copyright (C) 1995 Free Software Foundation, Inc.
3
4   Written by:  Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
5   Date: Aug 1995
6
7   This file is part of the GNUstep Base Library.
8
9   This library is free software; you can redistribute it and/or
10   modify it under the terms of the GNU Lesser General Public
11   License as published by the Free Software Foundation; either
12   version 2 of the License, or (at your option) any later version.
13
14   This library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public
20   License along with this library; if not, write to the Free
21   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22   Boston, MA 02110 USA.
23
24   <title>NSObjCRuntime class reference</title>
25   $Date$ $Revision$
26   */
27
28#import "common.h"
29
30#if !defined (__GNU_LIBOBJC__)
31#  include <objc/encoding.h>
32#endif
33
34#import "Foundation/NSException.h"
35
36/**
37 * Returns a string object containing the name for
38 * aProtocol.  If aProtocol is 0, returns nil.
39 */
40NSString *
41NSStringFromProtocol(Protocol *aProtocol)
42{
43  if (aProtocol != (Protocol*)0)
44    return [NSString stringWithUTF8String: protocol_getName(aProtocol)];
45  return nil;
46}
47
48/**
49 * Returns the protocol whose name is supplied in the
50 * aProtocolName argument, or 0 if a nil string is supplied.
51 */
52Protocol *
53NSProtocolFromString(NSString *aProtocolName)
54{
55  if (aProtocolName != nil)
56    {
57      int	len = [aProtocolName length];
58      char	buf[len+1];
59
60      [aProtocolName getCString: buf
61		      maxLength: len + 1
62		       encoding: NSASCIIStringEncoding];
63      return GSProtocolFromName (buf);
64    }
65  return (Protocol*)0;
66}
67
68/**
69 * Returns a string object containing the name for
70 * aSelector.  If aSelector is 0, returns nil.
71 */
72NSString *
73NSStringFromSelector(SEL aSelector)
74{
75  if (aSelector != (SEL)0)
76    return [NSString stringWithUTF8String: sel_getName(aSelector)];
77  return nil;
78}
79
80/**
81 * Returns (creating if necessary) the selector whose name is supplied in the
82 * aSelectorName argument, or 0 if a nil string is supplied.
83 */
84SEL
85NSSelectorFromString(NSString *aSelectorName)
86{
87  if (aSelectorName != nil)
88    {
89      int	len = [aSelectorName length];
90      char	buf[len+1];
91
92      [aSelectorName getCString: buf
93		      maxLength: len + 1
94		       encoding: NSASCIIStringEncoding];
95      return sel_registerName (buf);
96    }
97  return (SEL)0;
98}
99
100/**
101 * Returns the class whose name is supplied in the
102 * aClassName argument, or Nil if a nil string is supplied.
103 * If no such class has been loaded, the function returns Nil.
104 */
105Class
106NSClassFromString(NSString *aClassName)
107{
108  if (aClassName != nil)
109    {
110      int	len = [aClassName length];
111      char	buf[len+1];
112
113      [aClassName getCString: buf
114		   maxLength: len + 1
115		    encoding: NSASCIIStringEncoding];
116      return objc_lookUpClass (buf);
117    }
118  return (Class)0;
119}
120
121/**
122 * Returns an [NSString] object containing the class name for
123 * aClass.  If aClass is 0, returns nil.
124 */
125NSString *
126NSStringFromClass(Class aClass)
127{
128  if (aClass != (Class)0)
129    return [NSString stringWithUTF8String: (char*)class_getName(aClass)];
130  return nil;
131}
132
133/**
134 * When provided with a C string containing encoded type information,
135 * this method extracts size and alignment information for the specified
136 * type into the buffers pointed to by sizep and alignp.<br />
137 * If either sizep or alignp is a null pointer, the corresponding data is
138 * not extracted.<br />
139 * The function returns a pointer into the type information C string
140 * immediately after the decoded information.
141 */
142const char *
143NSGetSizeAndAlignment(const char *typePtr,
144  NSUInteger *sizep, NSUInteger *alignp)
145{
146  if (typePtr != NULL)
147    {
148      /* Skip any offset, but don't call objc_skip_offset() as that's buggy.
149       */
150      if (*typePtr == '+' || *typePtr == '-')
151	{
152	  typePtr++;
153	}
154      while (isdigit(*typePtr))
155	{
156	  typePtr++;
157	}
158      typePtr = objc_skip_type_qualifiers (typePtr);
159      if (sizep)
160	{
161          *sizep = objc_sizeof_type (typePtr);
162	}
163      if (alignp)
164	{
165          *alignp = objc_alignof_type (typePtr);
166	}
167      typePtr = objc_skip_typespec (typePtr);
168    }
169  return typePtr;
170}
171
172