File: utils.m

package info (click to toggle)
gtamsanalyzer.app 0.42-6
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 6,112 kB
  • ctags: 866
  • sloc: objc: 34,338; ansic: 8,762; cpp: 384; makefile: 59; sh: 47
file content (156 lines) | stat: -rwxr-xr-x 3,115 bytes parent folder | download | duplicates (7)
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
//
//  utils.m
//  CocoaTams
//
//  Created by matthew on Mon Apr 15 2002.
//  Copyright (c) 2001 Matthew Weinstein. All rights reserved.
//

#import "utils.h"
int menuStringComp(id first, id second, void *key)
{
    return [[first lowercaseString] compare: [second lowercaseString]];//[first caseInsensitiveCompare: second];
}

NSString *charToNSS(char c)
{
    NSString *tmp;
    char ss[2];
    ss[0] = c;
    ss[1] = '\0';
    tmp = [NSString stringWithCString: ss];
    //[tmp retain];
    return tmp;
}
NSMutableString *trimNSS(NSMutableString *s)
{
    char c;
    //keep checking the first character
    for(;;)
    {
	if([s length] == 0) break;
	c = [s characterAtIndex: 0];
	if(c == ' ' || c == '\t' || c == '\n' || c == '\r')
	    [s deleteCharactersInRange: NSMakeRange(0, 1)];
	else
	    break;
    }
    for(;;)
    {
	if([s length] == 0) break;
	c = [s characterAtIndex: ([s length] - 1)];
	if(c == ' ' || c == '\t' || c == '\n' || c == '\r')
	    [s deleteCharactersInRange: NSMakeRange([s length] - 1, 1)];
	else
	    break;
    }
    return s;
    
}
NSMutableString *trimCharNSS(NSMutableString *s, unichar ch)
{
    unichar c;
    //keep checking the first character
    for(;;)
    {
	if([s length] == 0) break;
	c = [s characterAtIndex: 0];
	if(c == ch)
	    [s deleteCharactersInRange: NSMakeRange(0, 1)];
	else
	    break;
    }
    for(;;)
    {
	if([s length] == 0) break;
	c = [s characterAtIndex: ([s length] - 1)];
	if(c == ch)
	    [s deleteCharactersInRange: NSMakeRange([s length] - 1, 1)];
	else
	    break;
    }
    return s;
    
}

NSMutableArray *convertStringToArray(NSString *theString, char breakChar)
{
    int i, len;
    int inString;
    char c;
    NSMutableString *ss = [[NSMutableString alloc] init];
    NSMutableArray *ans = [[NSMutableArray alloc] init];
    len = [theString length];
    [ss setString: @""];
    inString = 0;
    for(i = 0; i < len; i++)
    {
	c = [theString characterAtIndex: i];
	if(c == '\"' && !inString)
	{
	    [ss ADDCHAR(c)];
	    inString = 1;
	}
	else if(inString)
	{
	    [ss ADDCHAR(c)];
	    if(c == '\"') inString = 0;
	}
	else if (c == breakChar)
	{
	    if([ss length] > 0)
	    {
		[ans addObject: [ss copy]];
		[ss setString: @""];
	    }
	}
	else
	{
	    [ss ADDCHAR(c)];
	}
    }
    if([ss length])
    {
	[ans addObject: [ss copy]];
    }
    [ss release];
    return [ans autorelease];
}

BOOL addUniqueToArray(NSMutableArray *who, NSString *what)
{
    int cnt;
    int i;
    cnt = [who count];
    for(i = 0; i < cnt; i++)
    {
        if([[who objectAtIndex: i] isEqualToString: what] == YES) return NO;
    }
    [who addObject: what];
    return YES;
}
int nsSubString(NSString *first, NSString *sec, BOOL caseInsensitive)
{
    NSRange rng;
    NSRange nulRange = {NSNotFound, 0};
    
    
    if(caseInsensitive == YES)
    {
	if((rng = [first rangeOfString: sec options: caseInsensitive]).location  == NSNotFound)
	    return -1;
	else
	    return rng.location;
    }
    else
    {
	if((rng = [first rangeOfString: sec ]).location  == NSNotFound)
	    return -1;
	else
	    return rng.location;
    }
}