Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- #import <stdio.h>
- @interface PWGen : NSObject {
- }
- +returnUnique: aString;
- +(BOOL)allElementsIn: (int*)anArray ofSize: (int)aLength are: (int)anInt;
- +stringWithMapping: (int*)anArray ofSize: (int)anInt andCharacters: aString;
- +makeList: (int)length withCharacters: availableCharacters;
- @end
- @implementation PWGen
- +returnUnique: aString {
- int i;
- id newString = [NSMutableString string];
- id charSet = [[NSMutableCharacterSet alloc] init];
- for(i=0; i<[aString length]; i++) {
- char c = [aString characterAtIndex:i];
- if(![charSet characterIsMember: c]) {
- [charSet addCharactersInString:[NSString stringWithFormat:@"%c", c]];
- [newString appendFormat:@"%c", c];
- }
- }
- [charSet release];
- return newString;
- }
- +(BOOL)allElementsIn: (int*)anArray ofSize: (int)aLength are: (int)anInt {
- int i;
- for(i=0; i<aLength; i++) {
- if(anArray[i] != anInt) {
- return NO;
- }
- }
- return YES;
- }
- +stringWithMapping: (int*)anArray ofSize: (int)anInt andCharacters: aString {
- int i;
- id newString = [NSMutableString string];
- for(i=0; i<anInt; i++) {
- [newString appendFormat:@"%c", [aString characterAtIndex: anArray[i]]];
- }
- return newString;
- }
- +makeList: (int)length withCharacters: availableCharacters
- {
- int max,i;
- id availChars = [[PWGen returnUnique: availableCharacters] retain];
- int newString[100]={0};
- max = [availChars length]-1;
- if(max < 0) {
- NSBeep();
- NSLog(@"Not enough characters.");
- return nil;
- }
- while(![PWGen allElementsIn: newString ofSize: length are: max]) {
- printf("%s\n", [[PWGen stringWithMapping: newString ofSize: length andCharacters: availChars] cString]);
- for(i=length-1; i>=0; i--) {
- if(newString[i] != max) {
- newString[i]++;
- break;
- } else {
- newString[i] = 0;
- }
- }
- }
- printf("%s\n",[[PWGen stringWithMapping: newString ofSize: length andCharacters: availChars] cString]);
- [availChars release];
- return nil;
- }
- @end
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- if(argc < 3) {
- printf("usage: %s length \"characters\"\n", argv[0]);
- return 0;
- }
- [PWGen makeList: atoi(argv[1]) withCharacters: [NSString stringWithCString: argv[2]]];
- [pool release];
- return 0;
- }
Add Comment
Please, Sign In to add comment