File: sfparse.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (182 lines) | stat: -rw-r--r-- 4,834 bytes parent folder | download | duplicates (2)
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
/** This tool checks that a file is a valid strings-file
   Copyright (C) 1999 Free Software Foundation, Inc.

   Written by:  Richard Frith-Macdonald <richard@brainstorm.co.uk>
   Created: February 1999

   This file is part of the GNUstep Project

   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 COPYINGv3.
   If not, write to the Free Software Foundation,
   31 Milk Street #960789 Boston, MA 02196 USA.

   */

#import "common.h"

#import	"Foundation/NSArray.h"
#import	"Foundation/NSData.h"
#import	"Foundation/NSDictionary.h"
#import	"Foundation/NSException.h"
#import	"Foundation/NSProcessInfo.h"
#import	"Foundation/NSUserDefaults.h"
#import	"Foundation/NSAutoreleasePool.h"
#import "GNUstepBase/Additions.h"

int
convert_unicode(NSArray *args)
{
  unsigned int i;

  for (i = 2; i < [args count]; i++)
    {
      NSString	*file = [args objectAtIndex: i];

      NS_DURING
	{
	  NSData        *data;
	  NSString	*myString;
	  NSString      *output;

	  data = [NSData dataWithContentsOfFile: file];
	  myString = [[NSString alloc] initWithData: data
					   encoding: NSUTF8StringEncoding];
	  IF_NO_ARC([myString autorelease];)
	  if ([myString length] == 0)
	    {
	      myString = [[[NSString alloc] initWithData: data
		encoding: [NSString defaultCStringEncoding]] autorelease];
	    }
	  output = [[file lastPathComponent]
	    stringByAppendingPathExtension: @"unicode"];
	  data = [myString dataUsingEncoding: NSUnicodeStringEncoding];
	  [data writeToFile: output atomically: YES];
	}
      NS_HANDLER
	{
	  GSPrintf(stderr, @"Converting '%@' - %@\n", file,
	    [localException reason]);
	  return 1;
	}
      NS_ENDHANDLER
    }
  return 0;
}

int
convert_utf8(NSArray *args)
{
  unsigned int i;

  for (i = 2; i < [args count]; i++)
    {
      NSString	*file = [args objectAtIndex: i];

      NS_DURING
	{
	  NSData        *data;
	  NSString	*myString;
	  NSString      *output;

	  myString = [NSString stringWithContentsOfFile: file];
	  output = [[file lastPathComponent]
	    stringByAppendingPathExtension: @"utf8"];
	  data = [myString dataUsingEncoding: NSUTF8StringEncoding];
	  [data writeToFile: output atomically: YES];
	}
      NS_HANDLER
	{
	  GSPrintf(stderr, @"Converting '%@' - %@\n", file,
	    [localException reason]);
	  return 1;
	}
      NS_ENDHANDLER
    }
  return 0;
}


/** <p>
 This tool checks that a file is a valid strings-file, and can also convert
 files to Unicode or UTF-8.  If given the '<code>--unicode</code>' option
 it converts an ASCII or UTF-8 file to unicode.  If given the
 '<code>--utf8</code>' option is converts an ASCII or unicode file to UTF-8.
 </p> */
int
main(int argc, char** argv, char **env)
{
  NSAutoreleasePool	*pool;
  NSProcessInfo		*proc;
  NSArray		*args;
  unsigned		i;
  int			retval = 0;

#ifdef GS_PASS_ARGUMENTS
  GSInitializeProcess(argc, argv, env);
#endif
  pool = [NSAutoreleasePool new];
  proc = [NSProcessInfo processInfo];
  if (proc == nil)
    {
      GSPrintf(stderr, @"defaults: unable to get process information!\n");
      [pool release];
      exit(EXIT_FAILURE);
    }

  args = [proc arguments];

  if ([args count] <= 1 || [[args objectAtIndex: 1] isEqual: @"--help"]
      || [[args objectAtIndex: 1] isEqual: @"-h"])
    {
      printf("Usage: sfparse [--utf8] filename.\n");
      printf("--unicode    - convert an ASCII or UTF8 file to Unicode\n");
      printf("--utf8       - convert an ASCII or Unicode to UTF8\n");
    }
  else if ([[args objectAtIndex: 1] isEqual: @"--unicode"])
    {
      retval = convert_unicode(args);
    }
  else if ([[args objectAtIndex: 1] isEqual: @"--utf8"])
    {
      retval = convert_utf8(args);
    }
  else
    {
      for (i = 1; i < [args count]; i++)
	{
	  NSString	*file = [args objectAtIndex: i];

	  NS_DURING
	    {
	      NSString	*myString;
	      id		result;

	      myString = [NSString stringWithContentsOfFile: file];
	      result = [myString propertyListFromStringsFileFormat];
	      if (result == nil)
		GSPrintf(stderr, @"Parsing '%@' - nil property list\n", file);
	      else if ([result isKindOfClass: [NSDictionary class]] == YES)
		GSPrintf(stderr, @"Parsing '%@' - seems ok (%d entries)\n",
                  file, [result count]);
	      else
		GSPrintf(stderr, @"Parsing '%@' - unexpected class - %@\n",
		  file, [[result class] description]);
	    }
	  NS_HANDLER
	    {
	      GSPrintf(stderr, @"Parsing '%@' - %@\n", file,
		[localException reason]);
	      retval = 1;
	    }
	  NS_ENDHANDLER
	}
    }
  [pool release];
  return retval;
}