File: dk_make_protocol.m

package info (click to toggle)
dbuskit 0.1.1-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,956 kB
  • sloc: objc: 10,543; sh: 9,463; ansic: 200; makefile: 32
file content (207 lines) | stat: -rw-r--r-- 5,105 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/** Small tool to generate protocol declarations from introspection data.

   Copyright (C) 2010 Free Software Foundation, Inc.

   Written by:  Niels Grewe <niels.grewe@halbordnung.de>
   Created: August 2010

   This program 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.

   You should have received a copy of the GNU General Public
   License along with this program; see the file COPYING.
   If not, write to the Free Software Foundation,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

   */
#import <Foundation/Foundation.h>
#import "../Source/DKProxy+Private.h"
#import "../Source/DKIntrospectionParserDelegate.h"
#import "../Source/DKInterface.h"

#include <fcntl.h>
@interface DKIntrospector: NSObject <DKObjectPathNode>
{
  NSMutableArray *nodes;
  NSMutableDictionary *interfaces;
}
@end

@implementation DKIntrospector
- (id)init
{
  if (nil == (self = [super init]))
  {
    return nil;
  }
  interfaces = [NSMutableDictionary new];
  nodes = [NSMutableArray new];
  return self;
}
- (NSString*)_path
{
  return @"/";
}

- (void)_addChildNode: (DKObjectPathNode*)node
{
  if (nil != node)
  {
    [nodes addObject: node];
  }
}

- (void)_addInterface: (DKInterface*)interface
{
  NSString *name = [interface name];
  if (nil != name)
  {
    [interfaces setObject: interface
    forKey: name];
  }
}

- (NSDictionary*)_interfaces
{
  return interfaces;
}

- (void)dealloc
{
  [interfaces release];
  [nodes release];
  [super dealloc];
}
@end
enum
{
  EXPECT_SWITCH,
  EXPECT_PATH
};

int main (int argc, char **argv, char **env)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSProcessInfo *info = [NSProcessInfo processInfo];
  NSArray *args = [info arguments];
  NSUInteger argCount = [args count];
  NSUInteger argIndex = 1;
  NSUInteger argState = EXPECT_SWITCH;
  BOOL useObjC2 = NO;
  NSString *inPath = nil;
  NSString *outPath = nil;
  NSString **pathAddr = NULL;
  NSURL *inURL = nil;
  NSXMLParser *parser = nil;
  DKIntrospectionParserDelegate *delegate = nil;
  DKIntrospector *spector = nil;
  NSFileHandle *outHandle = nil;
  NSDictionary *interfaces = nil;
  DKInterface *thisIf = nil;
  NSEnumerator *ifEnum = nil;

  if (argCount == 1)
  {
    GSPrintf(stderr, @"Usage: Use '-i' to specify the input file and '-o' to specify the output file.\nIf no output file is given, stdout is used.\n");
    return 1;
  }
  for (argIndex = 1; argIndex < argCount; argIndex++)
  {
    NSString *thisArg = [args objectAtIndex: argIndex];
    if (([thisArg hasPrefix: @"-"]) && (EXPECT_SWITCH == argState))
    {
      if ([thisArg isEqualToString: @"-2"])
      {
	useObjC2 = YES;
	argState = EXPECT_SWITCH;
      }
      else if ([thisArg isEqualToString: @"-i"])
      {
	pathAddr = &inPath;
	argState = EXPECT_PATH;
      }
      else if ([thisArg isEqualToString: @"-o"])
      {
	pathAddr = &outPath;
	argState = EXPECT_PATH;
      }
    }
    else if (EXPECT_PATH == argState)
    {
      if (pathAddr != NULL)
      {
        *pathAddr = thisArg;
      }
      argState = EXPECT_SWITCH;
    }
    else
    {
      pathAddr = NULL;
    }
  }

  if (nil == inPath)
  {
    return 1;
  }

  inURL = [NSURL fileURLWithPath: [inPath stringByStandardizingPath]];
  if (nil == inURL)
  {
    return 1;
  }

  spector = [[[DKIntrospector alloc] init] autorelease];

  delegate = [[[DKIntrospectionParserDelegate alloc] initWithParentForNodes: spector] autorelease];

  parser = [[[NSXMLParser alloc] initWithContentsOfURL: inURL] autorelease];
  [parser setDelegate: delegate];
  [parser parse];

  interfaces = [spector _interfaces];
  if (0 == [interfaces count])
  {
    GSPrintf(stderr, @"No interfaces found.\n");
    return 1;
  }

  if (outPath == nil)
  {
    outHandle = [NSFileHandle fileHandleWithStandardOutput];
  }
  else
  {
    int fd = -1;
    outPath = [outPath stringByStandardizingPath];
    fd = creat([outPath UTF8String], 0644);
    if (-1 == fd)
    {
      GSPrintf(stderr,@"Could not open '%@'.\n", outPath);
      return 1;
    }
    outHandle = [[[NSFileHandle alloc] initWithFileDescriptor: fd
                                               closeOnDealloc: NO] autorelease];
  }
  if (outHandle == nil)
  {
    GSPrintf(stderr, @"Could write data.\n");
    return 1;
  }

  ifEnum = [interfaces objectEnumerator];
  while (nil != (thisIf = [ifEnum nextObject]))
  {
    NSString *preamble = [NSString stringWithFormat: @"#import <Foundation/Foundation.h>\n\n/*\n * Objective-C protocol declaration for the D-Bus %@ interface.\n */\n",
      [thisIf name]];
    [outHandle writeData: [preamble dataUsingEncoding: NSUTF8StringEncoding
                                 allowLossyConversion: YES]];
    [outHandle writeData: [[thisIf protocolDeclaration] dataUsingEncoding: NSUTF8StringEncoding
                                                     allowLossyConversion: YES]];
  }
  [outHandle closeFile];
  [pool release];
  return 0;
}