」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > RGFW 底層:剪貼簿複製/貼上

RGFW 底層:剪貼簿複製/貼上

發佈於2024-11-09
瀏覽:755

RGFW Under the Hood: Clipboard Copy/Paste

Introduction

Reading and writing to the clipboard using low-level APIs can be tricky. There are a bunch of steps required. This tutorial simplifies the process so you can easily read and write to the clipboard using the low-level APIs.

The tutorial is based on RGFW's source code and its usage of the low-level APIs.

Note: the cocoa code is written in Pure-C.

Overview

1) Clipboard Paste

  • X11 (init atoms, convert section, get data)
  • Win32 (open clipboard, get data, convert data, close clipboard)
  • Cocoa (set datatypes, get pasteboard, get data, convert data)

2) Clipboard Copy

  • X11 (init atoms, convert section, handle request, send data)
  • Win32 (setup global object, convert data, open clipboard, convert string, send data, close clipboard)
  • Cocoa (create datatype array, declare types, convert string, send data)

Clipboard Paste

X11

To handle the clipboard, you must create some Atoms via XInternAtom.
X Atoms are used to ask for or send specific data or properties through X11.

You'll need three atoms,

1) UTF8_STRING: Atom for a UTF-8 string.
2) CLIPBOARD: Atom for getting clipboard data.
3) XSEL_DATA: Atom to get selection data.

const Atom UTF8_STRING = XInternAtom(display, "UTF8_STRING", True);
const Atom CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
const Atom XSEL_DATA = XInternAtom(display, "XSEL_DATA", 0);

Now, to get the clipboard data you have to request that the clipboard section be converted to UTF8 using XConvertSelection.

use XSync to send the request to the server.

XConvertSelection(display, CLIPBOARD, UTF8_STRING, XSEL_DATA, window, CurrentTime);
XSync(display, 0);

The selection will be converted and sent back to the client as a XSelectionNotify event. You can get the next event, which should be the SelectionNotify event with XNextEvent.

XEvent event;
XNextEvent(display, &event);

Check if the event is a SelectionNotify event and use .selection to ensure the type is a CLIPBOARD. Also make sure .property is not 0 and can be retrieved.

if (event.type == SelectionNotify && event.xselection.selection == CLIPBOARD && event.xselection.property != 0) {

You can get the converted data via XGetWindowProperty using the selection property.

    int format;
    unsigned long N, size;
    char* data, * s = NULL;
    Atom target;

    XGetWindowProperty(event.xselection.display, event.xselection.requestor,
        event.xselection.property, 0L, (~0L), 0, AnyPropertyType, &target,
        &format, &size, &N, (unsigned char**) &data);

Make sure the data is in the right format by checking target

    if (target == UTF8_STRING || target == XA_STRING) {

The data is stored in data, once you're done with it free it with XFree.

You can also delete the property via XDeleteProperty.

        XFree(data);
    }

    XDeleteProperty(event.xselection.display, event.xselection.requestor, event.xselection.property);
}

winapi

First, open the clipboard OpenClipboard.

if (OpenClipboard(NULL) == 0)
    return 0;

Get the clipboard data as a utf16 string via GetClipboardData

If the data is NULL, you should close the clipboard using CloseClipboard

HANDLE hData = GetClipboardData(CF_UNICODETEXT);
if (hData == NULL) {
    CloseClipboard();
    return 0;
}

Next, you need to convert the utf16 data back to utf8.

Start by locking memory for the utf8 data via GlobalLock.

wchar_t* wstr = (wchar_t*) GlobalLock(hData);

Use setlocale to ensure the data format is utf8.

Get the size of the UTF-8 version with wcstombs.

setlocale(LC_ALL, "en_US.UTF-8");

size_t textLen = wcstombs(NULL, wstr, 0);

If the size is valid, convert the data using wcstombs.

if (textLen) {
    char* text = (char*) malloc((textLen * sizeof(char))   1);

    wcstombs(text, wstr, (textLen)   1);
    text[textLen] = '\0';

    free(text);
}

Make sure to free leftover global data using GlobalUnlock and close the clipboard with CloseClipboard.

GlobalUnlock(hData);
CloseClipboard();

cocoa

Cocoa uses NSPasteboardTypeString to ask for string data. You'll have to define this yourself if you're not using Objective-C.

NSPasteboardType const NSPasteboardTypeString = "public.utf8-plain-text";

Although the is a c-string and Cocoa uses NSStrings, you can convert the c-string to an NSString via stringWithUTF8String.

NSString* dataType = objc_msgSend_class_char(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), (char*)NSPasteboardTypeString);

Now we'll use generalPasteboard to get the default pasteboard object.

NSPasteboard* pasteboard = objc_msgSend_id((id)objc_getClass("NSPasteboard"), sel_registerName("generalPasteboard")); 

Then you can get the pasteboard's string data with the dataType using stringForType.

However, it will give you an NSString, which can be converted with UTF8String.

NSString* clip = ((id(*)(id, SEL, const char*))objc_msgSend)(pasteboard, sel_registerName("stringForType:"), dataType);
const char* str = ((const char* (*)(id, SEL)) objc_msgSend) (clip, sel_registerName("UTF8String"));

Clipboard Copy

X11

To copy to the clipboard you'll need a few more Atoms.

1) SAVE_TARGETS: To request a section to convert to (for copying).
2) TARGETS: To handle one requested target
3) MULTIPLE: When there are multiple request targets
4) ATOM_PAIR: To get the supported data types.
5) CLIPBOARD_MANAGER: To access data from the clipboard manager.

const Atom SAVE_TARGETS = XInternAtom((Display*) display, "SAVE_TARGETS", False);
const Atom TARGETS = XInternAtom((Display*) display, "TARGETS", False);
const Atom MULTIPLE = XInternAtom((Display*) display, "MULTIPLE", False);
const Atom ATOM_PAIR = XInternAtom((Display*) display, "ATOM_PAIR", False);
const Atom CLIPBOARD_MANAGER = XInternAtom((Display*) display, "CLIPBOARD_MANAGER", False);

We can request a clipboard section. First, set the owner of the section to be a client window via XSetSelectionOwner. Next request a converted section using XConvertSelection.

XSetSelectionOwner((Display*) display, CLIPBOARD, (Window) window, CurrentTime);

XConvertSelection((Display*) display, CLIPBOARD_MANAGER, SAVE_TARGETS, None, (Window) window, CurrentTime);

The rest of the code would exist in an event loop. You can create an external event loop from your main event loop if you wish or add this to your main event loop.

We'll be handling SelectionRequest in order to update the clipboard selection to the string data.

if (event.type == SelectionRequest) {
    const XSelectionRequestEvent* request = &event.xselectionrequest;

At the end of the SelectionNotify event, a response will be sent back to the requester. The structure should be created here and modified depending on the request data.

    XEvent reply = { SelectionNotify };
    reply.xselection.property = 0;

The first target we will handle is TARGETS when the requestor wants to know which targets are supported.

    if (request->target == TARGETS) {

I will create an array of supported targets

        const Atom targets[] = { TARGETS,
                                MULTIPLE,
                                UTF8_STRING,
                                XA_STRING };

This array can be passed using XChangeProperty.

I'll also change the selection property so the requestor knows what property we changed.

        XChangeProperty(display,
            request->requestor,
            request->property,
            4,
            32,
            PropModeReplace,
            (unsigned char*) targets,
            sizeof(targets) / sizeof(targets[0]));

        reply.xselection.property = request->property;
    }

Next, I will handle MULTIPLE targets.

    if (request->target == MULTIPLE) {

We'll start by getting the supported targets via XGetWindowProperty

        Atom* targets = NULL;

        Atom actualType = 0;
        int actualFormat = 0;
        unsigned long count = 0, bytesAfter = 0;

        XGetWindowProperty(display, request->requestor, request->property, 0, LONG_MAX, False, ATOM_PAIR, &actualType, &actualFormat, &count, &bytesAfter, (unsigned char **) &targets);

Now we'll loop through the supported targets. If the supported targets match one of our supported targets, we can pass the data with XChangeProperty.

If the target is not used, the second argument should be set to None, marking it as unused.

        unsigned long i;
        for (i = 0; i requestor,
                    targets[i   1],
                    targets[i],
                    8,
                    PropModeReplace,
                    (unsigned char*) text,
                    sizeof(text));
                XFlush(display);
            } else {
                targets[i   1] = None;
            }
        }

You can pass the final array of supported targets to the requestor using XChangeProperty. This tells the requestor which targets to expect for the original list it sent.

The message will be sent out asap when XFlush is called.

You can free your copy of the target array with XFree.

        XChangeProperty((Display*) display,
            request->requestor,
            request->property,
            ATOM_PAIR,
            32,
            PropModeReplace,
            (unsigned char*) targets,
            count);

        XFlush(display);
        XFree(targets);

        reply.xselection.property = request->property;
    }

For the final step of the event, send the selection back to the requestor via XSendEvent.

Then flush the queue with XFlush.

    reply.xselection.display = request->display;
    reply.xselection.requestor = request->requestor;
    reply.xselection.selection = request->selection;
    reply.xselection.target = request->target;
    reply.xselection.time = request->time;

    XSendEvent((Display*) display, request->requestor, False, 0, &reply);
    XFlush(display);
}

winapi

First allocate global memory for your data and your utf-8 buffer with GlobalAlloc

HANDLE object = GlobalAlloc(GMEM_MOVEABLE, (1   textLen) * sizeof(WCHAR));
WCHAR*  buffer = (WCHAR*) GlobalLock(object);

Next, you can use MultiByteToWideChar to convert your string to a wide string.

MultiByteToWideChar(CP_UTF8, 0, text, -1, buffer, textLen);

Now unlock the global object and open the clipboard

GlobalUnlock(object);
OpenClipboard(NULL);

To update the clipboard data, you start by clearing what's currently on the clipboard via EmptyClipboard you can use SetClipboardData to set the data to the utf8 object.

Finally, close the clipboard with CloseClipboard.

EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, object);

CloseClipboard();

cocoa

Start by creating an array of the type of data you want to put on the clipboard and convert it to an NSArray using initWithObjects.

NSPasteboardType ntypes[] = { dataType };

NSArray* array = ((id (*)(id, SEL, void*, NSUInteger))objc_msgSend)
                    (NSAlloc(objc_getClass("NSArray")), sel_registerName("initWithObjects:count:"), ntypes, 1);

Use declareTypes to declare the array as the supported data types.

You can also free the NSArray with NSRelease.

((NSInteger(*)(id, SEL, id, void*))objc_msgSend) (pasteboard, sel_registerName("declareTypes:owner:"), array, NULL);
NSRelease(array);

You can convert the string to want to copy to an NSString via stringWithUTF8String and set the clipboard string to be that NSString using setString.

NSString* nsstr = objc_msgSend_class_char(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), text);

((bool (*)(id, SEL, id, NSPasteboardType))objc_msgSend) (pasteboard, sel_registerName("setString:forType:"), nsstr, dataType);  

Full examples

X11

// compile with:
// gcc x11.c -lX11

#include 
#include 
#include 
#include 
#include 

#include 

int main(void) {
    Display* display = XOpenDisplay(NULL);

    Window window = XCreateSimpleWindow(display, RootWindow(display, DefaultScreen(display)), 10, 10, 200, 200, 1,
                                 BlackPixel(display, DefaultScreen(display)), WhitePixel(display, DefaultScreen(display)));

    XSelectInput(display, window, ExposureMask | KeyPressMask); 

    const Atom UTF8_STRING = XInternAtom(display, "UTF8_STRING", True);
    const Atom CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
    const Atom XSEL_DATA = XInternAtom(display, "XSEL_DATA", 0);

    const Atom SAVE_TARGETS = XInternAtom((Display*) display, "SAVE_TARGETS", False);
    const Atom TARGETS = XInternAtom((Display*) display, "TARGETS", False);
    const Atom MULTIPLE = XInternAtom((Display*) display, "MULTIPLE", False);
    const Atom ATOM_PAIR = XInternAtom((Display*) display, "ATOM_PAIR", False);
    const Atom CLIPBOARD_MANAGER = XInternAtom((Display*) display, "CLIPBOARD_MANAGER", False);

    // input
    XConvertSelection(display, CLIPBOARD, UTF8_STRING, XSEL_DATA, window, CurrentTime);
    XSync(display, 0);

    XEvent event;
    XNextEvent(display, &event);

    if (event.type == SelectionNotify && event.xselection.selection == CLIPBOARD && event.xselection.property != 0) {

        int format;
        unsigned long N, size;
        char* data, * s = NULL;
        Atom target;

        XGetWindowProperty(event.xselection.display, event.xselection.requestor,
            event.xselection.property, 0L, (~0L), 0, AnyPropertyType, &target,
            &format, &size, &N, (unsigned char**) &data);

        if (target == UTF8_STRING || target == XA_STRING) {
            printf("paste: %s\n", data);
            XFree(data);
        }

        XDeleteProperty(event.xselection.display, event.xselection.requestor, event.xselection.property);
    }

    // output
    char text[] = "new string\0";

    XSetSelectionOwner((Display*) display, CLIPBOARD, (Window) window, CurrentTime);

    XConvertSelection((Display*) display, CLIPBOARD_MANAGER, SAVE_TARGETS, None, (Window) window, CurrentTime);

    Bool running = True;
    while (running) {
        XNextEvent(display, &event);
        if (event.type == SelectionRequest) {
            const XSelectionRequestEvent* request = &event.xselectionrequest;

            XEvent reply = { SelectionNotify };
            reply.xselection.property = 0;

            if (request->target == TARGETS) {
                const Atom targets[] = { TARGETS,
                                        MULTIPLE,
                                        UTF8_STRING,
                                        XA_STRING };

                XChangeProperty(display,
                    request->requestor,
                    request->property,
                    4,
                    32,
                    PropModeReplace,
                    (unsigned char*) targets,
                    sizeof(targets) / sizeof(targets[0]));

                reply.xselection.property = request->property;
            }

            if (request->target == MULTIPLE) {  
                Atom* targets = NULL;

                Atom actualType = 0;
                int actualFormat = 0;
                unsigned long count = 0, bytesAfter = 0;

                XGetWindowProperty(display, request->requestor, request->property, 0, LONG_MAX, False, ATOM_PAIR, &actualType, &actualFormat, &count, &bytesAfter, (unsigned char **) &targets);

                unsigned long i;
                for (i = 0; i requestor,
                            targets[i   1],
                            targets[i],
                            8,
                            PropModeReplace,
                            (unsigned char*) text,
                            sizeof(text));
                        XFlush(display);
                        running = False;
                    } else {
                        targets[i   1] = None;
                    }
                }

                XChangeProperty((Display*) display,
                    request->requestor,
                    request->property,
                    ATOM_PAIR,
                    32,
                    PropModeReplace,
                    (unsigned char*) targets,
                    count);

                XFlush(display);
                XFree(targets);

                reply.xselection.property = request->property;
            }

            reply.xselection.display = request->display;
            reply.xselection.requestor = request->requestor;
            reply.xselection.selection = request->selection;
            reply.xselection.target = request->target;
            reply.xselection.time = request->time;

            XSendEvent((Display*) display, request->requestor, False, 0, &reply);
            XFlush(display);
        }
    }

    XCloseDisplay(display);
 }

Winapi

// compile with:
// gcc win32.c

#include 
#include 

#include 

int main() {
    // output
    if (OpenClipboard(NULL) == 0)
        return 0;

    HANDLE hData = GetClipboardData(CF_UNICODETEXT);
    if (hData == NULL) {
        CloseClipboard();
        return 0;
    }

    wchar_t* wstr = (wchar_t*) GlobalLock(hData);

    setlocale(LC_ALL, "en_US.UTF-8");

    size_t textLen = wcstombs(NULL, wstr, 0);

    if (textLen) {
        char* text = (char*) malloc((textLen * sizeof(char))   1);

        wcstombs(text, wstr, (textLen)   1);
        text[textLen] = '\0';

        printf("paste: %s\n", text);
        free(text);
    }

    GlobalUnlock(hData);
    CloseClipboard();


    // input

    char text[] = "new text\0";

    HANDLE object = GlobalAlloc(GMEM_MOVEABLE, (sizeof(text) / sizeof(char))  * sizeof(WCHAR));

    WCHAR* buffer = (WCHAR*) GlobalLock(object);
    if (!buffer) {
        GlobalFree(object);
        return 0;
    }

    MultiByteToWideChar(CP_UTF8, 0, text, -1, buffer, (sizeof(text) / sizeof(char)));

    GlobalUnlock(object);
    if (OpenClipboard(NULL) == 0) {
        GlobalFree(object);
        return 0;
    }

    EmptyClipboard();
    SetClipboardData(CF_UNICODETEXT, object);
    CloseClipboard();
}

Cocoa

// compile with:
// gcc cocoa.c -framework Foundation -framework AppKit  


#include 
#include 
#include 
#include 

#ifdef __arm64__
/* ARM just uses objc_msgSend */
#define abi_objc_msgSend_stret objc_msgSend
#define abi_objc_msgSend_fpret objc_msgSend
#else /* __i386__ */
/* x86 just uses abi_objc_msgSend_fpret and (NSColor *)objc_msgSend_id respectively */
#define abi_objc_msgSend_stret objc_msgSend_stret
#define abi_objc_msgSend_fpret objc_msgSend_fpret
#endif

typedef void NSPasteboard;
typedef void NSString;
typedef void NSArray;
typedef void NSApplication;

typedef const char* NSPasteboardType;

typedef unsigned long NSUInteger;
typedef long NSInteger;

#define NSAlloc(nsclass) objc_msgSend_id((id)nsclass, sel_registerName("alloc"))

#define objc_msgSend_bool           ((BOOL (*)(id, SEL))objc_msgSend)
#define objc_msgSend_void           ((void (*)(id, SEL))objc_msgSend)
#define objc_msgSend_void_id        ((void (*)(id, SEL, id))objc_msgSend)
#define objc_msgSend_uint           ((NSUInteger (*)(id, SEL))objc_msgSend)
#define objc_msgSend_void_bool      ((void (*)(id, SEL, BOOL))objc_msgSend)
#define objc_msgSend_void_int       ((void (*)(id, SEL, int))objc_msgSend)
#define objc_msgSend_bool_void      ((BOOL (*)(id, SEL))objc_msgSend)
#define objc_msgSend_void_SEL       ((void (*)(id, SEL, SEL))objc_msgSend)
#define objc_msgSend_id             ((id (*)(id, SEL))objc_msgSend)
#define objc_msgSend_id_id              ((id (*)(id, SEL, id))objc_msgSend)
#define objc_msgSend_id_bool            ((BOOL (*)(id, SEL, id))objc_msgSend)

#define objc_msgSend_class_char ((id (*)(Class, SEL, char*))objc_msgSend)

void NSRelease(id obj) {
    objc_msgSend_void(obj, sel_registerName("release"));
}

int main() {
    /* input */
    NSPasteboardType const NSPasteboardTypeString = "public.utf8-plain-text";

    NSString* dataType = objc_msgSend_class_char(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), (char*)NSPasteboardTypeString);

    NSPasteboard* pasteboard = objc_msgSend_id((id)objc_getClass("NSPasteboard"), sel_registerName("generalPasteboard")); 

    NSString* clip = ((id(*)(id, SEL, const char*))objc_msgSend)(pasteboard, sel_registerName("stringForType:"), dataType);

    const char* str = ((const char* (*)(id, SEL)) objc_msgSend) (clip, sel_registerName("UTF8String"));

    printf("paste: %s\n", str);

    char text[] = "new string\0";

    NSPasteboardType ntypes[] = { dataType };

    NSArray* array = ((id (*)(id, SEL, void*, NSUInteger))objc_msgSend)
                        (NSAlloc(objc_getClass("NSArray")), sel_registerName("initWithObjects:count:"), ntypes, 1);

    ((NSInteger(*)(id, SEL, id, void*))objc_msgSend) (pasteboard, sel_registerName("declareTypes:owner:"), array, NULL);
    NSRelease(array);

    NSString* nsstr = objc_msgSend_class_char(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), text);

    ((bool (*)(id, SEL, id, NSPasteboardType))objc_msgSend) (pasteboard, sel_registerName("setString:forType:"), nsstr, dataType);  
}
版本聲明 本文轉載於:https://dev.to/colleagueriley/rgfw-under-the-hood-clipboard-copypaste-3gcl如有侵犯,請洽[email protected]刪除
最新教學 更多>
  • 在 JavaScript 中實作斐波那契數列:常見方法和變體
    在 JavaScript 中實作斐波那契數列:常見方法和變體
    作為開發人員,您可能遇到過編寫函數來計算斐波那契數列中的值的任務。這個經典問題經常出現在程式設計面試中,通常要求遞歸實現。然而,面試官有時可能會要求具體的方法。在本文中,我們將探討 JavaScript 中最常見的斐波那契數列實作。 什麼是斐波那契數列? 首先,讓我們回顧一下。斐波...
    程式設計 發佈於2024-11-09
  • 如何使用 .htaccess 更改共享伺服器上的 PHP 版本?
    如何使用 .htaccess 更改共享伺服器上的 PHP 版本?
    在共享伺服器上透過.htaccess 更改PHP 版本如果您正在操作共享伺服器並且需要更改PHP 版本,可以透過.htaccess文件來做到這一點。這允許您為您的網站運行特定的 PHP 版本,同時伺服器維護其預設版本。 要切換 PHP 版本,請按照下列步驟操作:找到 . htaccess 檔案: 該...
    程式設計 發佈於2024-11-09
  • 如何在Ajax資料載入過程中顯示進度條?
    如何在Ajax資料載入過程中顯示進度條?
    如何在Ajax 資料載入期間顯示進度條處理使用者觸發的事件(例如從下拉方塊中選擇值)時,通常會使用非同步擷取資料阿賈克斯。在獲取數據時,向用戶提供正在發生某事的視覺指示是有益的。本文探討了一種在 Ajax 請求期間顯示進度條的方法。 使用 Ajax 實作進度條要建立一個準確追蹤 Ajax 呼叫進度的...
    程式設計 發佈於2024-11-09
  • TCJavaScript 更新、TypeScript Beta、Node.js 等等
    TCJavaScript 更新、TypeScript Beta、Node.js 等等
    歡迎來到新一期的「JavaScript 本週」! 今天,我們從 TC39、Deno 2 正式版本、TypeScript 5.7 Beta 等方面獲得了一些針對 JavaScript 語言的巨大新更新,所以讓我們開始吧! TC39 更新:JavaScript 有何變化? 最近在東京...
    程式設計 發佈於2024-11-09
  • 為什麼 Bootstrap 用戶應該在下一個專案中考慮使用 Tailwind CSS?
    為什麼 Bootstrap 用戶應該在下一個專案中考慮使用 Tailwind CSS?
    Tailwind CSS 入门 Bootstrap 用户指南 大家好! ?如果您是 Bootstrap 的长期用户,并且对过渡到 Tailwind CSS 感到好奇,那么本指南适合您。 Tailwind 是一个实用程序优先的 CSS 框架,与 Bootstrap 基于组件的结构相比...
    程式設計 發佈於2024-11-09
  • 組合與繼承
    組合與繼承
    介绍 继承和组合是面向对象编程(OOP)中的两个基本概念,但它们的用法不同并且具有不同的目的。这篇文章的目的是回顾这些目的,以及选择它们时要记住的一些事情。 继承的概念 当我们考虑在设计中应用继承时,我们必须了解: 定义:在继承中,一个类(称为派生类或子类)可以从另...
    程式設計 發佈於2024-11-09
  • 如何在 JavaScript 中將浮點數轉換為整數?
    如何在 JavaScript 中將浮點數轉換為整數?
    如何在 JavaScript 中將浮點數轉換為整數要將浮點數轉換為整數,您可以使用 JavaScript內建數學物件。 Math 物件提供了多種處理數學運算的方法,包括舍入和截斷。 方法:1。截斷:截斷去除數字的小數部分。要截斷浮點數,請使用 Math.floor()。此方法向下舍入到小於或等於原始...
    程式設計 發佈於2024-11-09
  • 標準字串實作中的 c_str() 和 data() 是否有顯著差異?
    標準字串實作中的 c_str() 和 data() 是否有顯著差異?
    標準字串實作中的c_str()與data()STL中c_str()和data()函數的區別人們普遍認為類似的實作是基於空終止的。據推測,c_str() 總是提供以 null 結尾的字串,而 data() 則不然。 然而,在實踐中,實作經常透過讓 data() 在內部呼叫 c_str() 來消除這種區...
    程式設計 發佈於2024-11-09
  • C/C++ 中的類型轉換如何運作以及程式設計師應該注意哪些陷阱?
    C/C++ 中的類型轉換如何運作以及程式設計師應該注意哪些陷阱?
    了解C/C 中的類型轉換型別轉換是C 和C 程式設計的一個重要方面,涉及將資料從一種類型轉換為另一種類型。它在記憶體管理、資料操作和不同類型之間的互通性方面發揮著重要作用。然而,了解類型轉換的工作原理及其限制對於防止潛在錯誤至關重要。 明確型別轉換使用 (type) 語法執行的明確型別轉換可讓程式設...
    程式設計 發佈於2024-11-09
  • 我們如何在 Golang 中為不同的資料類型建立通用函數?
    我們如何在 Golang 中為不同的資料類型建立通用函數?
    Golang 中的通用方法參數在 Go 中,一個常見的需求是有一個對不同類型的資料進行操作的函數。以計算特定類型切片中元素數量的函數為例。如何設計這個函數來處理任何類型的數據,而不僅僅是它最初設計的特定類型? 一種方法是使用接口,它本質上是定義類型必須的一組方法的契約實現以符合接口。透過使用介面作為...
    程式設計 發佈於2024-11-09
  • 使用 Node.js 流進行高效能資料處理
    使用 Node.js 流進行高效能資料處理
    在本文中,我们将深入研究 Node.js Streams 并了解它们如何帮助高效处理大量数据。流提供了一种处理大型数据集的优雅方式,例如读取大型文件、通过网络传输数据或处理实时信息。与一次性读取或写入整个数据的传统 I/O 操作不同,流将数据分解为可管理的块并逐块处理它们,从而实现高效的内存使用。 ...
    程式設計 發佈於2024-11-09
  • 如何在 Python 中產生字串的所有可能排列,包括處理重複項?
    如何在 Python 中產生字串的所有可能排列,包括處理重複項?
    Python 中的字串排列查找給定字串的所有可能排列可能是一項具有挑戰性的任務。然而,Python使用itertools模組提供了一個簡單的解決方案。 解決方案:itertools.permutations()itertools.permutations()方法是專門為生成排列而設計的。它接受一個可...
    程式設計 發佈於2024-11-09
  • 如何不使用CMD直接從C++呼叫Java函數?
    如何不使用CMD直接從C++呼叫Java函數?
    從C 應用程式呼叫Java 函數從C 應用程式呼叫Java 函數是一項挑戰,特別是在尋求繞過使用的直接解決方案時要在這兩種語言之間建立通信,請考慮「從C 建立JVM」中詳細介紹的方法。它概述了創建 JVM 並隨後呼叫 Java 方法的過程。 在 JVM 已經存在的情況下(例如,當 Java 程式呼叫...
    程式設計 發佈於2024-11-09
  • 為什麼我無法更改 IE8 中禁用的 HTML 控制項的文字顏色?
    為什麼我無法更改 IE8 中禁用的 HTML 控制項的文字顏色?
    IE8 中禁用的HTML 控制項的CSS 顏色變更問題在HTML 中,disabled 屬性停用輸入控制項,但它也會影響控制項這些控制項的外觀。大多數瀏覽器都支援使用 CSS 套用於停用控制項的自訂樣式。然而,Internet Explorer 8 (IE8) 在更改停用控制項的顏色方面提出了獨特的...
    程式設計 發佈於2024-11-09
  • DRUGHUB 的軟體需求規格 (SRS)
    DRUGHUB 的軟體需求規格 (SRS)
    1.介紹 1.1 目的 本文檔概述了 DRUGHUB 網站的軟體要求,該網站旨在促進沙烏地阿拉伯和埃及的藥品、醫療設備和必需品的採購、物流和供應鏈管理。該網站將透過簡化營運並確保無縫存取關鍵資源來為醫療保健組織提供服務。 1.2 範圍 DRUGHU...
    程式設計 發佈於2024-11-09

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3