cPy.cIO module

Contents

cPy.cIO module#

class cFileDisk[source]#

Bases: object

Class for direct disk file Input/Output operations.
  • This class wraps standard C file operations (fopen, fread, fwrite)

to provide a convenient interface for reading and writing files directly to disk.
OpenRead(FilePn: str) bool[source]#
CreateWrite(FilePn: str) bool[source]#
OpenAppend(FilePn: str) bool[source]#
GetSizeBytes() any[source]#
GetPos() any[source]#
SetPos(Pos: any)[source]#
Sets the file pointer to a specific absolute position.
SeekCur(Offset: any)[source]#
Moves the file pointer relative to the current position.
SeekEnd()[source]#
Moves the file pointer to the end of the file.
IsEof() bool[source]#
Checks if the file pointer is at the end of the file.
ReadBytes(To: any, MaxSize: int) int[source]#
WriteBytes(From: any, SizeBytes: int)[source]#
Close()[source]#
GetFILE() any[source]#
Returns the underlying standard FILE pointer.
class cFile[source]#

Bases: object

Class representing an in-memory file or data buffer.
  • Allows reading and writing various data types to a memory block similarly to a file stream.

  • Handles endianness swapping if configured.

Copy(Src: any, Size: int, FilePn: str = None)[source]#
Copies data from a raw pointer into the internal buffer.
Clear()[source]#
Clears the internal data buffer and resets position.
GetFilePn() cStr[source]#
SetFilePn(FilePn: str)[source]#
IsEof() bool[source]#
Size() int[source]#
GetPos() int[source]#
SeekCur(Offset: int) int[source]#
SeekEnd(Offset: int) int[source]#
Moves the data position pointer relative to the end of the data.
Parameters:

Offset (int) – The offset in bytes (usually negative or zero).

Returns:

The new absolute position.

Return type:

int

SetPos(Pos: int) int[source]#
Sets the data position pointer to an absolute value.
Parameters:

Pos (int) – The new position.

Returns:

The actual new position (clamped to valid range).

Return type:

int

SetPosMutable(Pos: int) int[source]#
Sets the data position pointer, enlarging the file with zeros if “Pos” lies after EOF.
Parameters:

Pos (int) – The new position.

Returns:

The new position.

Return type:

int

ReadBytes(To: any, MaxSize: int) int[source]#
Reads raw bytes from the buffer.
  • Warning: only bytes reading / writing are endian independent.

Parameters:
  • To – Destination buffer.

  • MaxSize (int) – Maximum bytes to read.

Returns:

The number of bytes actually read.

Return type:

int

WriteBytes(Fm: any, Size: int)[source]#
Inserts raw bytes at the current position.
ReplaceBytes(Fm: any, MaxSize: int) int[source]#
Replaces existing bytes at the current position without inserting new space.
ToPtr() any[source]#
ToCharPtr() str[source]#
ReadByte(b: any) bool[source]#
WriteByte(b: any)[source]#
ReadShort(s: int) bool[source]#
WriteShort(s: int)[source]#
ReadWord(w: int) bool[source]#
WriteWord(w: int)[source]#
ReadInt(i: int) bool[source]#
WriteInt(i: int)[source]#
ReadDword(dw: int) bool[source]#
WriteDword(dw: int)[source]#
ReadFloat(f: float) bool[source]#
WriteFloat(f: float)[source]#
ReadDouble(d: float) bool[source]#
WriteDouble(d: float)[source]#
ReadString(_0: cStr, Terminators: str = '\r\n') bool[source]#
Reads a string until a terminator is found.
WriteString(_0: str)[source]#
Writes a string without a trailing zero.
ReadVec2(u: cVec2) bool[source]#
WriteVec2(u: cVec2)[source]#
ReadVec3(u: cVec3) bool[source]#
WriteVec3(u: cVec3)[source]#
ReadRect(r: cRect) bool[source]#
WriteRect(r: cRect)[source]#
ReadVec2i(u: any) bool[source]#
WriteVec2i(u: any)[source]#
class cImageCodec[source]#

Bases: object

Abstract base class for image decoding and encoding.
  • Implement this interface to add support for new image formats.

Decode(Src: cFile, To: cImage) bool[source]#
Encode(Image: cImage, To: cFile) bool[source]#
Encodes an image into a file buffer.
Parameters:
  • Image (cImage) – The source image to encode.

  • To (cFile) – The destination file buffer.

Returns:

true if encoding was successful, false otherwise.

Return type:

bool

CheckMagic(Magic: int, ext: str) int[source]#
Checks if the file magic number matches this codec.
Parameters:
  • Magic (int) – The magic number (usually the first 4 bytes of the file).

  • ext (str) – The file extension.

Returns:

1 if magic is correct, 0 if incorrect, -1 if this format has no magic number.

Return type:

int

class cIO[source]#

Bases: object

Main Input/Output utility class.
  • Provides static methods for file manipulation, path handling, ZIP archives,

system dialogs, and image loading/saving.
static AddSourceZip(ZipFilePn: str, DataFolderWithinZip: str = None)[source]#
Mounts a ZIP file as a data source.
Parameters:
  • ZipFilePn (str) – Path to the zip file.

  • DataFolderWithinZip (str) – Optional subfolder within the zip to treat as root.

static SearchZipSources(DataFolderWithinZip: str = None)[source]#
Searches for ZIP files to add as sources.
Parameters:

DataFolderWithinZip (str) – Optional subfolder check.

static ZipInflateRaw(SrcCompressed: any, CompressedSize: int, ToMemory: cFile) bool[source]#
Inflates a raw deflated chunk into memory.
Parameters:
  • SrcCompressed – Pointer to compressed data.

  • CompressedSize (int) – Size of compressed data.

  • ToMemory (cFile) – Destination file buffer.

Returns:

true if successful.

Return type:

bool

static ZipInflateRaw_Debug()[source]#
static LoadFile(FilePn: str, To: cFile, ShowWarning: bool = True) bool[source]#
Loads a file into memory.
Parameters:
  • FilePn (str) – Path to the file.

  • To (cFile) – Destination cFile object.

  • ShowWarning (bool) – If true, shows a warning if loading fails.

Returns:

true if successful.

Return type:

bool

static SaveFile(FilePn: str, Src: cFile, Append: bool = False, ShowWarning: bool = True) bool[source]#
Saves a cFile object to disk.
Parameters:
  • FilePn (str) – Path to the file. If nullptr, uses Src.GetFilePn().

  • Src (cFile) – Source cFile object.

  • Append (bool) – If true, appends to the file.

  • ShowWarning (bool) – If true, shows a warning on failure.

Returns:

true if successful.

Return type:

bool

static SetFileDialogInitialFolder(PrefKey: str, Folder: str)[source]#
Sets the initial folder for a specific preference key (can be absolute or relative).
static GetFileDialogInitialFolder(PrefKey: str) cStr[source]#
Returns the initial folder for the given key. Returns “GetDefaultSourceFolder” if key is new.
static GetFileDialogPrefExists(PrefKey: str) bool[source]#
Returns false if there is no custom initial folder stored for this PrefKey.
static SelectFolderDialog(Title: str, SelectedFolder: cStr, InitialFolder: str = None) bool[source]#
Opens a dialog to select a folder.
Parameters:
  • Title (str) – Dialog title.

  • SelectedFolder (cStr) – Output for selected folder path.

  • InitialFolder (str) – Optional start folder (absolute or relative).

Returns:

true if a folder was selected.

Return type:

bool

static AddCodec(FileExtension: str, Codec: cImageCodec) bool[source]#
Registers a new image codec for a specific extension.
static LoadImage(FilePn: str, To: cImage) bool[source]#
Loads an image from file.
static SaveImage(FilePn: str, Src: cImage) bool[source]#
Saves an image to file.
static FindImageCodec(FileExtension: str) cImageCodec[source]#
Finds a codec responsible for the given extension.
static GetActualFileExtensionByMagic(Magic: int, ext: cStr) bool[source]#
static LastOpenedResource() cStr[source]#
Returns the path of the last opened resource.
static LoadImageDialog(ImageFilePn: cStr) bool[source]#
static SaveImageDialog(ImageFilePn: cStr) bool[source]#
static SetImageDialogInitialPath(InitialPath: str)[source]#
static SetImageDialogInitialFile(InitialFile: str)[source]#
Sets initial file for image dialog. Overrides initial path if not nullptr.
static GetImageDialogInitialPath() cStr[source]#
static GetImageDialogInitialFile() cStr[source]#
static SaveScreenShot(I: cImage, OptionalSuffix: str = None)[source]#
Saves a screenshot of the given image, optionally adding a suffix.
static MakeAndSaveScreenShot()[source]#
Captures the current screen and saves it.
static LoadXmlDialog(XmlFilePn: cStr) bool[source]#
static SaveXmlDialog(XmlFilePn: cStr) bool[source]#
static SetXmlDialogInitialPath(InitialPath: str)[source]#
static SetXmlDialogInitialFile(InitialFile: str)[source]#
static GetXmlDialogInitialPath() cStr[source]#
If not nullptr, it will override initial path
static GetXmlDialogInitialFile() cStr[source]#
Returns last loaded/saved XML file or the one set via SetXmlDialogInitialFile.
static SetModelAnimCameraBspDialogsInitialPath(InitialPath: str)[source]#
Model, Animation, Camera, and Bsp dialogs have common initial path, because they are in the same folder
static GetModelAnimCameraBspDialogsInitialPath() cStr[source]#
static SetModelDialogInitialFile(InitialFile: str)[source]#
decoding general formats for preview takes a lot of time
static SetAnimDialogInitialFile(InitialFile: str)[source]#
static SetBspDialogInitialFile(InitialFile: str)[source]#
static SetCameraDialogInitialFile(InitialFile: str)[source]#
static PathIsAbsolute(Path: str) bool[source]#
Returns “true” if path is absolute local or network (contains drive letter or starts from two (back)slashes).
static MakeRelativePath(Path: cStr) bool[source]#
Converts the path to relative. Returns true if shortening happened successfully.
static EnsureAbsolutePath(Path: str) cStr[source]#
Builds an absolute path relative to the default source folder if “Path” is relative.
Also ensures correct (back)slashes.
static GetDefaultSourceFolder() cStr[source]#
Returns folder of the first (default) source.
usually program folder or its parent if running in Debug/Release.
static SetDefaultSourceWriteFolder(WriteFolder: str)[source]#
Defines alternative write path for source folder.
static ReplaceReadPath(Path: cStr) bool[source]#
Updates path to point to the write folder if applicable.
Returns “true” if:
1. “WriteFolder” is defined;
2. “Path” lies within default source folder;
3. The argument “Path” has been altered and contains absolute path including “WriteFolder”.
static ReplaceReadPathForExistingFile(Path: cStr) bool[source]#
static CheckReadPath(Path: str) bool[source]#
Checks if path is within the default source folder structure.
Returns “true” if:
1. “WriteFolder” is defined;
2. “Path” lies within default source folder.
static RestartThisApplication()[source]#
Exits this application and launches this executable again.
static GetThisFilePathName() cStr[source]#
Returns absolute path to this executable file including its name.
static GetDocumentsFolder() cStr[source]#
Returns documents folder. Linux: /home/USER/Documents/, macOS: /Users/USER/Documents/, Windows: C:/Users/USER/Documents/
static GetSystemDocumentsFolder() cStr[source]#
static GetDownloadsFolder() cStr[source]#
Returns downloads folder.
static GetDesktopFolder() cStr[source]#
Returns desktop folder (Windows only).
static SetFilePermissions(FilePn: str)[source]#
Corrects file attributes to ensure read/write rights.
static SetFolderPermissions(Folder: str)[source]#
static CreatePath(PathOrFilePn: str, inUserDocuments: bool = True) bool[source]#
Checks existence of all intermediate directories and creates them if necessary.
  • “PathOrFilePn” can be absolute / relative path or file name without path.

Examples:

- `cIO::CreatePath("1/2/3");`
- Windows: `cIO::CreatePath("c:/A/B\C\File.txt")`
- MacOS: `cIO::CreatePath("/A/B\C")`
Parameters:
  • PathOrFilePn (str) – The path to create.

  • inUserDocuments (bool) – If true, tries to create in user documents folder.

Returns:

“true” if all intermediate directories exist or have been created.

Return type:

bool

static Exec(FilePn: str, Args: str = None, Wait: bool = False, Hide: bool = False) bool[source]#
Executes an external file or program.
  • Usage examples:

1. Document pathname:
  • cIO::Exec(cIO::EnsureAbsolutePath("~/Scripts/Scripting.pdf"))

2. Program with absolute path and optional arguments:
  • Windows: cIO::Exec("C:...maya.exe", "D:...Logo.mb")

  • Linux: cIO::Exec("/usr/bin/env python", "-V")

3. URL with protocol:
  • cIO::Exec("http://3d-coat.com/")

Parameters:
  • FilePn (str) – Path to file, program, or URL.

  • Args (str) – Optional arguments.

  • Wait (bool) – If true, waits for process to finish.

  • Hide (bool) – If true, hides the window.

Returns:

true if execution started successfully.

Return type:

bool

static CheckProcessInMemory(ProcName: str) bool[source]#
Checks if a process with the given name is running.
Parameters:

ProcName (str) – Process name (case-insensitive partial match).

Returns:

true if process exists.

Return type:

bool

static Explore(Folder: str) bool[source]#
Opens a folder in the system file explorer.
Examples: cIO::Explore("Shaders")
Parameters:

Folder (str) – Absolute or relative path.

Returns:

true on success.

Return type:

bool

static CompareFileChangeTimes(FilePn0: str, FilePn1: str) bool[source]#
Compares file modification times.
Parameters:
  • FilePn0 (str) – First file path.

  • FilePn1 (str) – Second file path.

Returns:

“true” when “FilePn0” is older than “FilePn1”.

Return type:

bool

static NeedUpdateFile(FileToUpdate: str, SourceFile: str) bool[source]#
Returns true if FileToUpdate does not exist or is older than SourceFile.
static CombineImages(File1: str, File2: str, FileOut: str) bool[source]#
Combines two images into an output image with the same format.
Returns:

true if successful.

Return type:

bool

static CheckDifferentDisks(PathName0: str, PathName1: str) bool[source]#
Returns “true” when files/folders “PathName0” and “PathName1” reside on different disks.
Note: paths could be non-existent.
static CopyToClipboard(Src: cImage)[source]#
static CopyFromClipboard(To: cImage) bool[source]#
static RemoveFolder(Folder: str, Recursive: bool = True) bool[source]#
Removes a directory.
Parameters:
  • Folder (str) – Directory path.

  • Recursive (bool) – If true, deletes content recursively.

static RemoveFile(FilePn: str) bool[source]#
Deletes a file.
static CheckIfPathWritable(path: str) bool[source]#
Checks if the path is writable.
static GetDiskFreeSpace() float[source]#
Returns number of free megabytes on the disk containing this program.
static FileExists(filename: str) bool[source]#
Returns true if file exists.
Parameters:

filename (str) – The filename. Relative and absolute paths are supported. Documents folder placement taken into account. Checked in both install and documents folders.

Returns:

true if file exists.

Return type:

bool

static FileCopy(Src: str, Dst: str, overwrite: bool = True) bool[source]#
Copy file from Src to Dst.
Parameters:
  • Src (str) – The source file.

  • Dst (str) – The destination file. If file is in install folder, it will be copied to the documents folder.

  • overwrite (bool) – If true, overwrites existing file.

Returns:

true if file copied successfully.

Return type:

bool

static GetDiskSerialNumber() cStr[source]#
Returns serial number of bootable hard disk.
Examples: MacOS “6RY57LG3”, Windows “1490395c”, Linux “49cb4cde”.
Returns:

Serial number or empty string on failure.

Return type:

cPy.cTypes.cStr

static GetMACAddress() cStr[source]#
Returns primary MAC address.
Format: “xx:xx:xx:xx:xx:xx”.
Returns:

MAC address or empty string on failure.

Return type:

cPy.cTypes.cStr

static ShowString(OptionalInit: cStr = None, dx: int = 0, dy: int = 0, center_x: bool = True, center_y: bool = True)[source]#
static GetString(S: cStr) bool[source]#
Returns “true” when the string is visible.
static InputString(S: cStr) bool[source]#
static StopStringInput()[source]#
static GetInputPixelsHeight() int[source]#
static ReturnPressed() bool[source]#
static GetUserFolder() cStr[source]#
Deprecated. Use “cIO::GetDocumentsFolder”.
static GetCacheFolder() cStr[source]#
Returns cache folder path. Could be empty.
static GetEnvironmentVariable(VarName: str) cStr[source]#
Returns value of environment variable or empty string if the variable is not defined.