<commit_before>#import "include/cef_application_mac.h"
#import "native/atom_cef_client.h"
#import "native/atom_application.h"
#import "native/atom_window_controller.h"
#import "native/atom_cef_app.h"
#import <getopt.h>

@implementation AtomApplication

@synthesize arguments=_arguments;

+ (AtomApplication *)sharedApplication {
  return (AtomApplication *)[super sharedApplication];
}

+ (id)applicationWithArguments:(char **)argv count:(int)argc {
  AtomApplication *application = [self sharedApplication];
  CefInitialize(CefMainArgs(argc, argv), [self createCefSettings], new AtomCefApp);
  application.arguments = [self parseArguments:argv count:argc];
  
  return application;
}

+ (NSDictionary *)parseArguments:(char **)argv count:(int)argc {
  NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init];
  
  // Defaults
  #ifdef RESOURCE_PATH
    [arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"];
  #endif
  
  int opt;
  int longindex;
  
  if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732
    argc -= 2; // Ignore last two arguments
  }
  
  static struct option longopts[] = {
    { "executed-from",      optional_argument,      NULL,  'K' },
    { "resource-path",      optional_argument,      NULL,  'r' },
    { "benchmark",          optional_argument,      NULL,  'b' },
    { "test",               optional_argument,      NULL,  't' },
    { NULL,                 0,                      NULL,  0 }
  };
  
  while ((opt = getopt_long(argc, argv, "r:K:bth?", longopts, &longindex)) != -1) {
    NSString *key = [NSString stringWithUTF8String:longopts[longindex].name];
    NSString *value = [NSString stringWithUTF8String:optarg];

    switch (opt) {
      case 'K':
      case 'r':
      case 'b':
      case 't':
        [arguments setObject:value forKey:key];
        break;
      default:
        printf("usage: atom [--resource-path=<path>] [<path>]");
    }
  }
  
  argc -= optind;
  argv += optind;
  
  if (argc > 0) {
    NSString *path = [NSString stringWithUTF8String:argv[0]];
    NSString *executedFromPath =[arguments objectForKey:@"executed-from"];
    if (![path isAbsolutePath] && executedFromPath) {
      path = [executedFromPath stringByAppendingPathComponent:path];
    }
    path = [path stringByStandardizingPath];
    [arguments setObject:path forKey:@"path"];
  }
  
  return arguments;
}

+ (NSString *)supportDirectory {
  NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
  NSString *supportDirectory = [cachePath stringByAppendingPathComponent:executableName];

  NSFileManager *fs = [NSFileManager defaultManager];
  NSError *error;
  BOOL success = [fs createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error];
  if (!success) {
    NSLog(@"Warning: Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]);
    supportDirectory = @"";
  }

  return supportDirectory;
}
  
+ (CefSettings)createCefSettings {
  CefSettings settings;

  CefString(&settings.cache_path) = [[self supportDirectory] UTF8String];
  CefString(&settings.user_agent) = "";
  CefString(&settings.log_file) = "";
  CefString(&settings.javascript_flags) = "";
  settings.remote_debugging_port = 9090;
  settings.log_severity = LOGSEVERITY_ERROR;
  return settings;
}

- (void)dealloc {
  [_backgroundWindowController release];
  [_arguments release];
  [super dealloc];
}

- (void)open:(NSString *)path {
  [[AtomWindowController alloc] initWithPath:path];
}

- (IBAction)runSpecs:(id)sender {
  [self runSpecsThenExit:NO];
}

- (void)runSpecsThenExit:(BOOL)exitWhenDone {
  [[AtomWindowController alloc] initSpecsThenExit:exitWhenDone];
}

- (IBAction)runBenchmarks:(id)sender {
  [self runBenchmarksThenExit:NO];
}

- (void)runBenchmarksThenExit:(BOOL)exitWhenDone {
  [[AtomWindowController alloc] initBenchmarksThenExit:exitWhenDone];
}

# pragma mark NSApplicationDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
  _backgroundWindowController = [[AtomWindowController alloc] initInBackground];
    
  if ([self.arguments objectForKey:@"benchmark"]) {
    [self runBenchmarksThenExit:true];
  }
  else if ([self.arguments objectForKey:@"test"]) {
    [self runSpecsThenExit:true];<commit_msg>Command line arguments with no variable don't crash the app.<commit_after>#import "include/cef_application_mac.h"
#import "native/atom_cef_client.h"
#import "native/atom_application.h"
#import "native/atom_window_controller.h"
#import "native/atom_cef_app.h"
#import <getopt.h>

@implementation AtomApplication

@synthesize arguments=_arguments;

+ (AtomApplication *)sharedApplication {
  return (AtomApplication *)[super sharedApplication];
}

+ (id)applicationWithArguments:(char **)argv count:(int)argc {
  AtomApplication *application = [self sharedApplication];
  CefInitialize(CefMainArgs(argc, argv), [self createCefSettings], new AtomCefApp);
  application.arguments = [self parseArguments:argv count:argc];
  
  return application;
}

+ (NSDictionary *)parseArguments:(char **)argv count:(int)argc {
  NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init];
  
  // Defaults
  #ifdef RESOURCE_PATH
    [arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"];
  #endif
  
  int opt;
  int longindex;
  if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732
    argc -= 2; // Ignore last two arguments
  }
  
  static struct option longopts[] = {
    { "executed-from",      optional_argument,      NULL,  'K' },
    { "resource-path",      optional_argument,      NULL,  'r' },
    { "benchmark",          optional_argument,      NULL,  'b' },
    { "test",               optional_argument,      NULL,  't' },
    { NULL,                 0,                      NULL,  0 }
  };

  while ((opt = getopt_long(argc, argv, "r:K:bth?", longopts, &longindex)) != -1) {
    NSString *key = [NSString stringWithUTF8String:longopts[longindex].name];
    NSString *value = optarg ? [NSString stringWithUTF8String:optarg] : @"YES";

    switch (opt) {
      case 'K':
      case 'r':
      case 'b':
      case 't':
        [arguments setObject:value forKey:key];
        break;
      default:
        NSLog(@"usage: atom [--resource-path=<path>] [<path>]");
    }
  }
  
  argc -= optind;
  argv += optind;
  
  if (argc > 0) {
    NSString *path = [NSString stringWithUTF8String:argv[0]];
    NSString *executedFromPath =[arguments objectForKey:@"executed-from"];
    if (![path isAbsolutePath] && executedFromPath) {
      path = [executedFromPath stringByAppendingPathComponent:path];
    }
    path = [path stringByStandardizingPath];
    [arguments setObject:path forKey:@"path"];
  }
  
  
  return arguments;
}

+ (NSString *)supportDirectory {
  NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
  NSString *supportDirectory = [cachePath stringByAppendingPathComponent:executableName];

  NSFileManager *fs = [NSFileManager defaultManager];
  NSError *error;
  BOOL success = [fs createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error];
  if (!success) {
    NSLog(@"Warning: Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]);
    supportDirectory = @"";
  }

  return supportDirectory;
}
  
+ (CefSettings)createCefSettings {
  CefSettings settings;

  CefString(&settings.cache_path) = [[self supportDirectory] UTF8String];
  CefString(&settings.user_agent) = "";
  CefString(&settings.log_file) = "";
  CefString(&settings.javascript_flags) = "";
  settings.remote_debugging_port = 9090;
  settings.log_severity = LOGSEVERITY_ERROR;
  return settings;
}

- (void)dealloc {
  [_backgroundWindowController release];
  [_arguments release];
  [super dealloc];
}

- (void)open:(NSString *)path {
  [[AtomWindowController alloc] initWithPath:path];
}

- (IBAction)runSpecs:(id)sender {
  [self runSpecsThenExit:NO];
}

- (void)runSpecsThenExit:(BOOL)exitWhenDone {
  [[AtomWindowController alloc] initSpecsThenExit:exitWhenDone];
}

- (IBAction)runBenchmarks:(id)sender {
  [self runBenchmarksThenExit:NO];
}

- (void)runBenchmarksThenExit:(BOOL)exitWhenDone {
  [[AtomWindowController alloc] initBenchmarksThenExit:exitWhenDone];
}

# pragma mark NSApplicationDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
  _backgroundWindowController = [[AtomWindowController alloc] initInBackground];
  
  if ([self.arguments objectForKey:@"benchmark"]) {
    [self runBenchmarksThenExit:true];
  }
  else if ([self.arguments objectForKey:@"test"]) {
    [self runSpecsThenExit:true];
{'fraction_non_alphanumeric': 0.12943587531010678, 'fraction_numerical': 0.005177435012404271, 'mean_word_length': 4.542139868499701, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1330352', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2860, 'n_words': 570}