Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2. #import <stdio.h>
  3.  
  4. @interface PWGen : NSObject {
  5.  
  6. }
  7.  
  8. +returnUnique: aString;
  9. +(BOOL)allElementsIn: (int*)anArray ofSize: (int)aLength are: (int)anInt;
  10. +stringWithMapping: (int*)anArray ofSize: (int)anInt andCharacters: aString;
  11. +makeList: (int)length withCharacters: availableCharacters;
  12.  
  13. @end
  14.  
  15. @implementation PWGen
  16.  
  17. +returnUnique: aString {
  18. int i;
  19. id newString = [NSMutableString string];
  20. id charSet = [[NSMutableCharacterSet alloc] init];
  21. for(i=0; i<[aString length]; i++) {
  22. char c = [aString characterAtIndex:i];
  23. if(![charSet characterIsMember: c]) {
  24. [charSet addCharactersInString:[NSString stringWithFormat:@"%c", c]];
  25. [newString appendFormat:@"%c", c];
  26. }
  27. }
  28. [charSet release];
  29. return newString;
  30. }
  31.  
  32. +(BOOL)allElementsIn: (int*)anArray ofSize: (int)aLength are: (int)anInt {
  33. int i;
  34. for(i=0; i<aLength; i++) {
  35. if(anArray[i] != anInt) {
  36. return NO;
  37. }
  38. }
  39. return YES;
  40. }
  41.  
  42. +stringWithMapping: (int*)anArray ofSize: (int)anInt andCharacters: aString {
  43. int i;
  44. id newString = [NSMutableString string];
  45. for(i=0; i<anInt; i++) {
  46. [newString appendFormat:@"%c", [aString characterAtIndex: anArray[i]]];
  47. }
  48. return newString;
  49. }
  50.  
  51. +makeList: (int)length withCharacters: availableCharacters
  52. {
  53. int max,i;
  54. id availChars = [[PWGen returnUnique: availableCharacters] retain];
  55. int newString[100]={0};
  56.  
  57. max = [availChars length]-1;
  58. if(max < 0) {
  59. NSBeep();
  60. NSLog(@"Not enough characters.");
  61. return nil;
  62. }
  63.  
  64. while(![PWGen allElementsIn: newString ofSize: length are: max]) {
  65. printf("%s\n", [[PWGen stringWithMapping: newString ofSize: length andCharacters: availChars] cString]);
  66. for(i=length-1; i>=0; i--) {
  67. if(newString[i] != max) {
  68. newString[i]++;
  69. break;
  70. } else {
  71. newString[i] = 0;
  72. }
  73. }
  74. }
  75. printf("%s\n",[[PWGen stringWithMapping: newString ofSize: length andCharacters: availChars] cString]);
  76. [availChars release];
  77. return nil;
  78. }
  79.  
  80. @end
  81.  
  82. int main (int argc, const char * argv[]) {
  83. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  84. if(argc < 3) {
  85. printf("usage: %s length \"characters\"\n", argv[0]);
  86. return 0;
  87. }
  88. [PWGen makeList: atoi(argv[1]) withCharacters: [NSString stringWithCString: argv[2]]];
  89. [pool release];
  90. return 0;
  91. }
Add Comment
Please, Sign In to add comment