<commit_before>package com.pty4j.windows;

import com.pty4j.PtyException;
import com.pty4j.WinSize;
import com.pty4j.util.PtyUtil;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import jtermios.windows.WinAPI;

import java.io.File;
import java.io.IOException;
import java.nio.Buffer;

/**
 * @author traff
 */
public class WinPty {
  private final winpty_t myWinpty;

  private NamedPipe myNamedPipe;
  private boolean myClosed = false;


  public WinPty(String cmdline, String cwd, String env) throws PtyException {
    myWinpty = INSTANCE.winpty_open(80, 25);

    if (myWinpty == null) {
      throw new PtyException("winpty is null");
    }

    int c;

    char[] cmdlineArray = cmdline != null ? toCharArray(cmdline) : null;
    char[] cwdArray = cwd != null ? toCharArray(cwd) : null;
    char[] envArray = env != null ? toCharArray(env) : null;

    if ((c = INSTANCE.winpty_start_process(myWinpty, null, cmdlineArray, cwdArray, envArray)) != 0) {
      throw new PtyException("Error running process:" + c);
    }

    myNamedPipe = new NamedPipe(myWinpty.dataPipe);
  }

  private static char[] toCharArray(String string) {
    char[] array = new char[string.length() + 1];
    System.arraycopy(string.toCharArray(), 0, array, 0, string.length());
    array[string.length()] = 0;
    return array;
  }

  public void setWinSize(WinSize winSize) {
    if (myClosed) {
      return;
    }
    INSTANCE.winpty_set_size(myWinpty, winSize.ws_col, winSize.ws_row);
  }

  public void close() {
    if (myClosed) {
      return;
    }

    INSTANCE.winpty_close(myWinpty);

    myNamedPipe.markClosed();

    myClosed = true;
  }

  public int exitValue() {
    if (myClosed) {
      return -2;
    }
    return INSTANCE.winpty_get_exit_code(myWinpty);
  }

  public int read(byte[] buf, int len) throws IOException {
    if (myClosed) {
      return 0;
    }

    return myNamedPipe.read(buf, len);
  }

  public void write(byte[] buf, int len) throws IOException {
    if (myClosed) {
      return;
    }

    myNamedPipe.write(buf, len);
  }

  public static class winpty_t extends Structure {
    public WinNT.HANDLE controlPipe;
    public WinNT.HANDLE dataPipe;
  }

  public static final Kern32 KERNEL32 = (Kern32)Native.loadLibrary("kernel32", Kern32.class);

  interface Kern32 extends Library {
    boolean PeekNamedPipe(WinNT.HANDLE hFile,
                          Buffer lpBuffer,
                          int nBufferSize,
                          IntByReference lpBytesRead,
                          IntByReference lpTotalBytesAvail,
                          IntByReference lpBytesLeftThisMessage);

    boolean ReadFile(WinNT.HANDLE handle, Buffer buffer, int i, IntByReference reference, WinBase.OVERLAPPED overlapped);
  }

  public static WinPtyLib INSTANCE = (WinPtyLib)Native.loadLibrary(getLibraryPath(), WinPtyLib.class);

  private static String getLibraryPath() {
    try {
      String folder = PtyUtil.getJarFolder();
      File path = new File(folder, "win");

      if (Platform.is64Bit()) {<commit_msg>open field in pc structure.<commit_after>package com.pty4j.windows;

import com.pty4j.PtyException;
import com.pty4j.WinSize;
import com.pty4j.util.PtyUtil;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import jtermios.windows.WinAPI;

import java.io.File;
import java.io.IOException;
import java.nio.Buffer;

/**
 * @author traff
 */
public class WinPty {
  private final winpty_t myWinpty;

  private NamedPipe myNamedPipe;
  private boolean myClosed = false;


  public WinPty(String cmdline, String cwd, String env) throws PtyException {
    myWinpty = INSTANCE.winpty_open(80, 25);

    if (myWinpty == null) {
      throw new PtyException("winpty is null");
    }

    int c;

    char[] cmdlineArray = cmdline != null ? toCharArray(cmdline) : null;
    char[] cwdArray = cwd != null ? toCharArray(cwd) : null;
    char[] envArray = env != null ? toCharArray(env) : null;

    if ((c = INSTANCE.winpty_start_process(myWinpty, null, cmdlineArray, cwdArray, envArray)) != 0) {
      throw new PtyException("Error running process:" + c);
    }

    myNamedPipe = new NamedPipe(myWinpty.dataPipe);
  }

  private static char[] toCharArray(String string) {
    char[] array = new char[string.length() + 1];
    System.arraycopy(string.toCharArray(), 0, array, 0, string.length());
    array[string.length()] = 0;
    return array;
  }

  public void setWinSize(WinSize winSize) {
    if (myClosed) {
      return;
    }
    INSTANCE.winpty_set_size(myWinpty, winSize.ws_col, winSize.ws_row);
  }

  public void close() {
    if (myClosed) {
      return;
    }

    INSTANCE.winpty_close(myWinpty);

    myNamedPipe.markClosed();

    myClosed = true;
  }

  public int exitValue() {
    if (myClosed) {
      return -2;
    }
    return INSTANCE.winpty_get_exit_code(myWinpty);
  }

  public int read(byte[] buf, int len) throws IOException {
    if (myClosed) {
      return 0;
    }

    return myNamedPipe.read(buf, len);
  }

  public void write(byte[] buf, int len) throws IOException {
    if (myClosed) {
      return;
    }

    myNamedPipe.write(buf, len);
  }

  public static class winpty_t extends Structure {
    public WinNT.HANDLE controlPipe;
    public WinNT.HANDLE dataPipe;
    public boolean open;
  }

  public static final Kern32 KERNEL32 = (Kern32)Native.loadLibrary("kernel32", Kern32.class);

  interface Kern32 extends Library {
    boolean PeekNamedPipe(WinNT.HANDLE hFile,
                          Buffer lpBuffer,
                          int nBufferSize,
                          IntByReference lpBytesRead,
                          IntByReference lpTotalBytesAvail,
                          IntByReference lpBytesLeftThisMessage);

    boolean ReadFile(WinNT.HANDLE handle, Buffer buffer, int i, IntByReference reference, WinBase.OVERLAPPED overlapped);
  }

  public static WinPtyLib INSTANCE = (WinPtyLib)Native.loadLibrary(getLibraryPath(), WinPtyLib.class);

  private static String getLibraryPath() {
    try {
      String folder = PtyUtil.getJarFolder();
      File path = new File(folder, "win");

      if (Platform.is64Bit()) {
{'fraction_non_alphanumeric': 0.10719236100415833, 'fraction_numerical': 0.0101647928538426, 'mean_word_length': 3.036047234307023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7370872', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2156, 'n_words': 501}