<commit_before>// eslint-disable-next-line import/no-extraneous-dependencies
import ref from 'ref-napi'

import {
  DllNames,
  DStruct as DS,
  StructFactory,
  ucsBufferSplit,
  DModel as M,
} from '../../index.js'
import { Winspool as DLL } from '../../index.promise.js'


export {
  M, DS, ref,
}

export const dllName = DllNames.winspool
export type Win32Fns = DLL.Win32Fns


export function retriveStruct_PRINTER_INFO<L extends M.PRINTER_INFO_LEVEL>(
  pPrinter: Buffer,
  Level: L,
): M.PRINTER_INFO_X[L] {

  switch (Level) {
    case 1:
      return retriveStruct_PRINTER_INFO_1(pPrinter) as M.PRINTER_INFO_X[L]
    case 4:
      return retriveStruct_PRINTER_INFO_4(pPrinter) as M.PRINTER_INFO_X[L]
    default:
      throw new Error(`Level not implemented:${Level}`)
  }

}

export function retriveStruct_PRINTER_INFO_1(pPrinter: Buffer): M.PRINTER_INFO_1 {
  const structDef = DS.PRINTER_INFO_1
  const pbuf = Buffer.alloc(pPrinter.byteLength)
  pPrinter.copy(pbuf, 0, 32)

  const Flags = pPrinter.readUInt32LE()
  const txtArr = ucsBufferSplit(pbuf)

  const struct = StructFactory<M.PRINTER_INFO_X[1]>(structDef)
  struct.Flags = Flags

  const keys = Object.keys(structDef)
  const keysRev = keys.reverse()
  const txtLen = txtArr.length
  keysRev.forEach((key, idx) => {
    if (idx >= txtLen) {
      return
    }
    if (key === 'Attributes') { return }
    if (typeof txtArr[idx] === 'undefined') {
      return
    }
    const str = txtArr[idx] ?? ''
    // @ts-ignore
    if (typeof struct[key] === 'string') {
      // @ts-ignore
      struct[key] = str
    }
  })

  return struct

  // const pDescriptionAddr = pPrinter.readUInt64LE(8)
  // const pNameAddr = pPrinter.readUInt64LE(16)
  // const pCommentAddr = pPrinter.readUInt64LE(24)
  // // @ts-ignore
  // const ptr = ref.alloc('void **')
  // ptr.writeBigInt64LE(BigInt(pDescriptionAddr))
  // const pName = ptr.deref()
  // void pName

}

export function retriveStruct_PRINTER_INFO_4(pPrinter: Buffer): M.PRINTER_INFO_4 {
  const structDef = DS.PRINTER_INFO_4
  const pbuf = Buffer.alloc(pPrinter.byteLength)
  pPrinter.copy(pbuf, 0, 24)

  const attr = pPrinter.readUInt32LE(16)
  // pPrinterName: WCHAR_String
  // pServerName: WCHAR_String
  // Attributes: DWORD

  const txtArr = ucsBufferSplit(pbuf)

  const struct = StructFactory<M.PRINTER_INFO_X[4]>(structDef)
  struct.Attributes = attr

  const keys = Object.keys(structDef)
  // const keysRev = keys.reverse()
  const txtLen = txtArr.length
  keys.forEach((key, idx) => {
    if (idx >= txtLen) {
      return
    }
    if (typeof txtArr[idx] === 'undefined') {
      return
    }
    const str = txtArr[idx] ?? ''
    // @ts-ignore
    if (typeof struct[key] === 'string') {
      // @ts-ignore
      struct[key] = str
    }
  })

  return struct

  // const pDescriptionAddr = pPrinter.readUInt64LE(8)
  // const pNameAddr = pPrinter.readUInt64LE(16)
  // const pCommentAddr = pPrinter.readUInt64LE(24)
  // // @ts-ignore
  // const ptr = ref.alloc('void **')
  // ptr.writeBigInt64LE(BigInt(pDescriptionAddr))
  // const pName = ptr.deref()
  // void pName

}
<commit_msg>refactor(win32-api): retriveStruct_PRINTER_INFO_4() by ptrToString()<commit_after>// eslint-disable-next-line import/no-extraneous-dependencies
import ref from 'ref-napi'

import {
  DllNames,
  DStruct as DS,
  StructFactory,
  ucsBufferSplit,
  DModel as M,
  ptrToString,
} from '../../index.js'
import { Winspool as DLL } from '../../index.promise.js'


export {
  M, DS, ref,
}

export const dllName = DllNames.winspool
export type Win32Fns = DLL.Win32Fns


export function retriveStruct_PRINTER_INFO<L extends M.PRINTER_INFO_LEVEL>(
  pPrinter: Buffer,
  Level: L,
): M.PRINTER_INFO_X[L] {

  switch (Level) {
    case 1:
      return retriveStruct_PRINTER_INFO_1(pPrinter) as M.PRINTER_INFO_X[L]
    case 4:
      return retriveStruct_PRINTER_INFO_4(pPrinter) as M.PRINTER_INFO_X[L]
    default:
      throw new Error(`Level not implemented:${Level}`)
  }

}

export function retriveStruct_PRINTER_INFO_1(pPrinter: Buffer): M.PRINTER_INFO_1 {
  const structDef = DS.PRINTER_INFO_1
  const pbuf = Buffer.alloc(pPrinter.byteLength)
  pPrinter.copy(pbuf, 0, 32)

  const Flags = pPrinter.readUInt32LE()
  const txtArr = ucsBufferSplit(pbuf)

  const struct = StructFactory<M.PRINTER_INFO_X[1]>(structDef)
  struct.Flags = Flags

  const keys = Object.keys(structDef)
  const keysRev = keys.reverse()
  const txtLen = txtArr.length
  keysRev.forEach((key, idx) => {
    if (idx >= txtLen) {
      return
    }
    if (key === 'Attributes') { return }
    if (typeof txtArr[idx] === 'undefined') {
      return
    }
    const str = txtArr[idx] ?? ''
    // @ts-ignore
    if (typeof struct[key] === 'string') {
      // @ts-ignore
      struct[key] = str
    }
  })

  return struct

  // const pDescriptionAddr = pPrinter.readUInt64LE(8)
  // const pNameAddr = pPrinter.readUInt64LE(16)
  // const pCommentAddr = pPrinter.readUInt64LE(24)
  // // @ts-ignore
  // const ptr = ref.alloc('void **')
  // ptr.writeBigInt64LE(BigInt(pDescriptionAddr))
  // const pName = ptr.deref()
  // void pName

}

export function retriveStruct_PRINTER_INFO_4(pPrinter: Buffer): M.PRINTER_INFO_4 {
  const structDef = DS.PRINTER_INFO_4
  const blen = pPrinter.byteLength

  // const pbuf = Buffer.alloc(blen)
  // pPrinter.copy(pbuf, 0, 24)
  // const txtArr = ucsBufferSplit(pbuf)

  const struct = StructFactory<M.PRINTER_INFO_X[4]>(structDef)
  // pPrinterName: WCHAR_String
  // pServerName: WCHAR_String
  // Attributes: DWORD
  const addrPName = pPrinter.readUInt64LE(0)
  struct.pPrinterName = ptrToString(addrPName, blen)

  const addrSName = pPrinter.readUInt64LE(8)
  struct.pServerName = ptrToString(addrSName, blen)

  struct.Attributes = pPrinter.readUInt32LE(16)

  return struct
}


{'fraction_non_alphanumeric': 0.1279089812101362, 'fraction_numerical': 0.017927943458024478, 'mean_word_length': 3.388804841149773, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '6638393', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2102, 'n_words': 428}