<commit_before>// Filename: winStats.cxx
// Created by:  drose (02Dec03)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "pandatoolbase.h"

#include "winStatsServer.h"
#include "config_pstats.h"

#include <windows.h>

static const char *toplevel_class_name = "pstats";
static WinStatsServer *server = NULL;

////////////////////////////////////////////////////////////////////
//     Function: toplevel_window_proc
//  Description: 
////////////////////////////////////////////////////////////////////
static LONG WINAPI
toplevel_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
  switch (msg) {
  case WM_TIMER:
    server->poll();
    break;

  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  default:
    break;
  }

  return DefWindowProc(hwnd, msg, wparam, lparam);
}

  
////////////////////////////////////////////////////////////////////
//     Function: create_toplevel_window
//  Description: Creates the initial, toplevel window for the
//               application.
////////////////////////////////////////////////////////////////////
static HWND
create_toplevel_window(HINSTANCE application) {
  WNDCLASS wc;

  ZeroMemory(&wc, sizeof(WNDCLASS));
  wc.lpfnWndProc = (WNDPROC)toplevel_window_proc;
  wc.hInstance = application;
  wc.lpszClassName = toplevel_class_name;

  if (!RegisterClass(&wc)) {
    nout << "Could not register window class!\n";
    exit(1);
  }

  DWORD window_style = WS_POPUP | WS_SYSMENU | WS_ICONIC;

  char window_name[128];
  sprintf(window_name, "PStats %d", pstats_port);

  HWND toplevel_window = 
    CreateWindow(toplevel_class_name, window_name, window_style,
                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                 NULL, NULL, application, 0);
  if (!toplevel_window) {
    nout << "Could not create toplevel window!\n";
    exit(1);
  }
  
  return toplevel_window;
}


// WinMain() is the correct way to start a Windows-only application,
// but it is sometimes more convenient during development to use
// main() instead, which doesn't squelch the stderr output.

#ifndef DEVELOP_WINSTATS
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
#else
int main(int argc, char *argv[])
#endif
{
  HINSTANCE application = GetModuleHandle(NULL);
  HWND toplevel_window = create_toplevel_window(application);

  ShowWindow(toplevel_window, SW_SHOWMINIMIZED);

  // Create the server object.
  server = new WinStatsServer;
  if (!server->listen()) {
    nout << "Unable to open port.\n";
    exit(1);
  }
<commit_msg>use a dialog when the port is unavailable<commit_after>// Filename: winStats.cxx
// Created by:  drose (02Dec03)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "pandatoolbase.h"

#include "winStatsServer.h"
#include "config_pstats.h"

#include <windows.h>

static const char *toplevel_class_name = "pstats";
static WinStatsServer *server = NULL;

////////////////////////////////////////////////////////////////////
//     Function: toplevel_window_proc
//  Description: 
////////////////////////////////////////////////////////////////////
static LONG WINAPI
toplevel_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
  switch (msg) {
  case WM_TIMER:
    server->poll();
    break;

  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  default:
    break;
  }

  return DefWindowProc(hwnd, msg, wparam, lparam);
}

  
////////////////////////////////////////////////////////////////////
//     Function: create_toplevel_window
//  Description: Creates the initial, toplevel window for the
//               application.
////////////////////////////////////////////////////////////////////
static HWND
create_toplevel_window(HINSTANCE application) {
  WNDCLASS wc;

  ZeroMemory(&wc, sizeof(WNDCLASS));
  wc.lpfnWndProc = (WNDPROC)toplevel_window_proc;
  wc.hInstance = application;
  wc.lpszClassName = toplevel_class_name;

  if (!RegisterClass(&wc)) {
    nout << "Could not register window class!\n";
    exit(1);
  }

  DWORD window_style = WS_POPUP | WS_SYSMENU | WS_ICONIC;

  char window_name[128];
  sprintf(window_name, "PStats %d", pstats_port);

  HWND toplevel_window = 
    CreateWindow(toplevel_class_name, window_name, window_style,
                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                 NULL, NULL, application, 0);
  if (!toplevel_window) {
    nout << "Could not create toplevel window!\n";
    exit(1);
  }
  
  return toplevel_window;
}


// WinMain() is the correct way to start a Windows-only application,
// but it is sometimes more convenient during development to use
// main() instead, which doesn't squelch the stderr output.

#ifndef DEVELOP_WINSTATS
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
#else
int main(int argc, char *argv[])
#endif
{
  HINSTANCE application = GetModuleHandle(NULL);
  HWND toplevel_window = create_toplevel_window(application);

  ShowWindow(toplevel_window, SW_SHOWMINIMIZED);

  // Create the server object.
  server = new WinStatsServer;
  if (!server->listen()) {
    ostringstream stream;
    stream 
      << "Unable to open port " << pstats_port
      << ".  Try specifying a different\n"
      << "port number using pstats-port in your Config file.";
    string str = stream.str();
    MessageBox(toplevel_window, str.c_str(), "PStats error", 
               MB_OK | MB_ICONEXCLAMATION);
    exit(1);
  }

{'fraction_non_alphanumeric': 0.2185368877867328, 'fraction_numerical': 0.00712957222566646, 'mean_word_length': 2.106884930187771, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6870908', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1894, 'n_words': 541}