GW2GetFileType
ID ประเภทไฟล์จะถูกแมปกับประเภทไฟล์ที่ Glasswall Engine รองรับ API นี้ให้ชื่ออย่างเป็นทางการของประเภทไฟล์ในรูปแบบสตริงเมื่อระบุหมายเลข ID ประเภทไฟล์
- C++
- C#
- Java
- Python
- JavaScript
#include "glasswall.core2.api.h"
int GW2GetFileType(
Session session,
size_t fileId,
size_t *bufferLength,
char **outputBuffer);
พารามิเตอร์
session ID ของ session ตามที่ส่งกลับโดย GW2OpenSession
fileId จำนวนเต็มที่แสดงถึง ID ของประเภทไฟล์
bufferLength พารามิเตอร์เอาต์พุตซึ่งจะถูกกำหนดค่าเป็นขนาดหน่วยไบต์ของหน่วยความจำที่ outputBuffer ชี้อยู่
outputBuffer พารามิเตอร์เอาต์พุตแบบตัวชี้สตริงซึ่งจะถูกกำหนดค่าด้วยชื่อทางการของประเภทไฟล์ที่เชื่อมโยงกับ file ID ที่ระบุ เช่น "BMP Image" ผู้ใช้ไม่จำเป็นต้องคืนหน่วยความจำที่ตัวชี้นี้ใช้งาน
ส่งกลับ
ส่งคืนจำนวนเต็มที่ระบุว่าการเรียกใช้ฟังก์ชันสำเร็จหรือไม่ ค่าติดลบหมายถึงความล้มเหลว ดูตาราง Return Types สำหรับคำอธิบายของรหัสส่งคืน หากสำเร็จ outputBuffer จะถูกกำหนดค่าด้วยชื่อทางการที่เชื่อมโยงกับประเภทไฟล์
ตัวอย่าง
#include "glasswall.core2.api.h"
#include "filetype.h" // ft_t enum which includes ft_bmp
char *outbuf = NULL;
size_t buflen = 0;
if (GW2OpenSession() < 0)
/* Error opening session */
else
{
int status = GW2GetFileType(session, ft_bmp, &buflen, &outbuf);
/* In this example outbuf will point to the string "BMP Image". */
if (GW2CloseSession() < 0)
/* Error closing session */
}
สรุป
public int GW2GetFileType(
int session,
uint fileID,
ref UIntPtr bufferLength,
out IntPtr outputBuffer)
ส่งกลับ
ส่งคืนค่า enum GW2_RetStatus แบบจำนวนเต็ม ค่าติดลบหมายถึงความล้มเหลว หากสำเร็จ บัฟเฟอร์เอาต์พุตจะถูกกำหนดค่าด้วยชื่อทางการของประเภทไฟล์
ตัวอย่าง
using glasswall_core2;
...
Glasswall glasswall = new Glasswall(); // Instance of the Glasswall wrapper
int session = glasswall.OpenSession();
int returnStatus = glasswall.GW2GetFileType(session, 22, ref bufferLength, out buffer);
if (bufferLength >= 0)
{
byte[] msgArray = glasswall.CreateArrayFromBuffer(buffer, bufferLength);
// Formal name for file type ID 22 now stored in a byte array
}
if (glasswall.CloseSession(session))
{
// Error Handling
}
สรุป
import com.glasswall.core2javabridge.*;
public String GW2GetFileTypeString(int session, int fileId) throws GlasswallException
หมายเหตุ
ก่อนหน้านี้ฟังก์ชันการทำงานนี้ต้องใช้การเรียกฟังก์ชันแยกกันสองครั้งเพื่อดึงข้อมูลประเภทไฟล์ ขณะนี้ได้ปรับให้เรียบง่ายขึ้นโดยส่งคืนการตั้งค่าเป็น String ฟังก์ชันดั้งเดิมทั้งสองรายการถูกเลิกใช้งานแล้ว
ส่งกลับ
ฟังก์ชัน GW2GetFileTypeString ส่งคืนค่าที่แสดงในรูปแบบ String ของ filetype ID
ระบบจะโยนข้อยกเว้น GlasswallException หาก session ไม่ถูกต้อง หรือหากไม่สามารถดึงประเภทไฟล์ได้
สรุป - ฟังก์ชันที่เลิกใช้แล้ว
import com.glasswall.core2javabridge.*;
(Deprecated)
public int GW2GetFileType(int session, int fileId) throws GlasswallException
public byte[] GetFileTypeBuffer(int session) throws GlasswallException
คำอธิบาย - ฟังก์ชันที่เลิกใช้แล้ว
ฟังก์ชัน GW2GetFileType จะส่งออกค่าที่แสดงแทน fileId สำหรับเซสชันที่ระบุโดย session ไปยัง FileType Buffer ภายใน ดึงข้อมูลนี้ได้โดยใช้ฟังก์ชัน GetFileTypeBuffer.
ค่าที่ส่งกลับ - ฟังก์ชันที่เลิกใช้แล้ว
ฟังก์ชัน GW2GetFileType จะส่งคืนค่า enumeration GW2_RetStatus ที่แปลงเป็น int ค่าจะเป็นค่าติดลบหากเกิดข้อผิดพลาด 0 หมายถึงสำเร็จ โปรดดูรายละเอียดใน API Overview/Return types.
ฟังก์ชัน GetFileTypeBuffer จะส่งคืน byte[] ที่เข้ารหัสแบบ UTF-8 ซึ่งมีค่าที่แสดงแทน ID ของประเภทไฟล์ ค่านี้จะเป็น null หากยังไม่ได้เรียก GW2GetFileType.
ระบบจะโยนข้อยกเว้น GlasswallException หาก session ไม่ถูกต้อง หรือหากไม่สามารถดึงประเภทไฟล์ได้
สรุป
ดึงข้อมูลเกี่ยวกับประเภทไฟล์ตามตัวระบุของประเภทนั้น
def get_file_type_info(self, file_type: Union[str, int]):
""" Retrieve information about a file type based on its identifier.
Args:
file_type (Union[str, int]): The file type identifier. This can be either a string representing a file
extension (e.g. 'bmp') or an integer corresponding to a file type (e.g. 29).
Returns:
- file_type_info (Union[int, str]): Depending on the input 'file_type':
- If `file_type` is a string (e.g. 'bmp'):
- If the file type is recognised, returns an integer corresponding to that file type.
- If the file type is not recognised, returns 0.
- If `file_type` is an integer (e.g. 29):
- If the integer corresponds to a recognised file type, returns a more detailed string description
of the file type (e.g. 'BMP Image').
- If the integer does not match any recognised file type, returns an empty string.
"""
ส่งกลับ
ส่งคืนจำนวนเต็มหรือสตริงตามค่าอินพุต 'file_type' ค่านี้อาจเป็นจำนวนเต็มของประเภทไฟล์ที่รู้จัก คำอธิบายแบบสตริงโดยละเอียดของประเภทไฟล์ที่รู้จัก หรือหากไม่รู้จักค่าอินพุต 'file_type' จะเป็น 0 หรือสตริงว่าง
สรุป
ฟังก์ชันนี้จะวางพอยน์เตอร์ไปยังคำอธิบายของ fileID ที่ระบุไว้ในตำแหน่งที่ระบุ
/**
*
* @param {number} session The ID of the session.
* @param {number} fileID The ID of the file type.
* @param {number} bufferLength The length of the buffer.
* @param {string} outputBuffer The location of the output buffer.
*/
GW2GetFileType(
session,
fileID,
bufferLength,
outputBuffer)
ส่งกลับ
ส่งคืนค่า enum GW2_RetStatus แบบจำนวนเต็ม ตัวเลขติดลบหมายถึงล้มเหลว หากสำเร็จ บัฟเฟอร์เอาต์พุตจะถูกเติมด้วยชื่ออย่างเป็นทางการของประเภทไฟล์
ตัวอย่าง
const ref = require('ref-napi');
...
function buffer_to_string(buffer, buffer_size) {
if (!buffer.isNull() && ref.deref(buffer_size) > 0) {
return Buffer.from(ref.reinterpret(ref.deref(buffer), ref.deref(buffer_size), 0)).toString();
}
else {
return "";
}
}
...
output_file_buffer = ref.alloc(ref.refType(ref.types.CString));
output_buffer_size = ref.alloc(ref.types.size_t, 0);
return_status = gw.GW2GetFileType(session_id, 22, output_buffer_size, output_file_buffer);
let error_description = buffer_to_string(output_file_buffer, output_buffer_size);
...