作者归档:fourfireadmin

char*,char[], string 之间互相转换

char[] 转 char*

char ch[]="abcdef";
char *s = ch;
cout << s << endl; // 这不是打印地址,而是打印s的内容

char*转char[]

#include <string>
#include <iostream>

using namespace std;

int main() {

    char *s = (char *) "aabbcc";
    char ch[100];
    size_t size = sizeof(char) * strlen(s) + 1;
    strncpy_s(ch, s, size);

    cout << ch << endl;

    return 0;
}

string转char[]

#include <string>
#include <iostream>

using namespace std;

int main() {

    string s = "abc";
    // to char[]
    const size_t chSize = 128;
    char ch[chSize];
    int i = 0;
    strncpy_s(ch, chSize, s.c_str(), s.size());
    cout << ch << endl;

    // to const char*
    const char *aa = s.c_str();
    cout << aa << endl;

    return 0;
}

char[]转string

#include <string>
#include <iostream>

using namespace std;

int main() {


    string s;
    char ch[10] = "abcdef";
    s = ch; // 简单粗爆,

    string b(ch);   // 构造函数

    string c;
    c.append(ch); // append

    cout << s << endl;
    cout << b << endl;
    cout << c << endl;
}

string转char*

#include <string>
#include <iostream>

using namespace std;

int main() {


    // 方法一
    {
        string s = "aa";
        char *ch;
        ch = (char *) s.c_str();
        cout << ch << endl;
    }

    
    // 方法二
    {
        string s = "bb";
        char *ch;
        s += '\0';
        ch = (char *) s.data();
        cout << ch << endl;
    }
}

char*转string

#include <string>
#include <iostream>

using namespace std;

int main() {

    // 方法一
    {
        string s;
        char *ch = (char *) "abcdef";
        s = ch;

        cout << s << endl;
    }

    // 方法二
    {
        string s;
        char *ch = (char *) "abcdef";
        s = ch;
        s.assign(ch);

        cout << s << endl;
    }
}

CMake笔记之GLOB和GLOB_RECURSE的使用方法

1.GLOB

帮助开发收集要编译的源文件,就是模糊匹配到文件后,把所有文件放到一个变量里面

cmake_minimum_required(VERSION 3.10)
project(MyProject)

# 设置变量包含所有源文件
file(GLOB MY_PROJECT_SOURCES "src/*.cpp")

# 将源文件编译成可执行文件
add_executable(${PROJECT_NAME} ${SOURCE_SRC})

这样所有的src下面的cpp文件都放进了 MY_PROJECT_SOURCES 这个变量中,后面可以直接使用这个编译进行可执行文件的编译

2.GLOB_RECURSE

GLOB会递归的寻找文件夹下面的文件,GLOB_RECURSE就不会就这点区别

pve修改登录ssl证书

修改目录

目录: 
/etc/pve/nodes/<hostname>/

需要修改的文件:
/etc/pve/nodes/<hostname>/pve-ssl.key
/etc/pve/nodes/<hostname>/pve-ssl.pem

我的证书是在Let’s encrypt申请的证书,直接替换即可

先备份:
mv /etc/pve/nodes/<hostname>/pve-ssl.key /etc/pve/nodes/<hostname>/pve-ssl.key.backup
mv /etc/pve/nodes/<hostname>/pve-ssl.pem /etc/pve/nodes/<hostname>/pve-ssl.pem.backup

重命名:
mv fullchain.pem pve-ssl.key
mv privkey.pem pve-ssl.pem

最后重启

systemctl restart pveproxy

Linux删除包含特殊字符的目录或文件

首先使用 ls -i 查看包含特殊字符的文件/目录名

root@comet:~# ls -i -l
total 28
26222802 drwxr-xr-x 2 root root 4096 Feb  6  2024  fonts
26214522 drwxr-xr-x 2 root root 4096 Jun 27 10:12  nes
26214418 drwxr-xr-x 2 root root 4096 May 16 15:18  picoshare
26214527 -rw-r--r-- 1 root root 7033 Nov 13 01:14 ''$'\033'':Qq' # 这就是我需要删除的
26214412 drwxr-xr-x 3 root root 4096 Jun 28 04:06  sslocal
26214504 drwxrwxrwx 5 root root 4096 May 22 01:30  transmission

然后通过find后进行删除

root@comet:~# find . -inum 26214527 -exec rm -r {} \; # 后面的 ; 不能省略
root@comet:~# ls -l
total 20
drwxr-xr-x 2 root root 4096 Feb  6  2024 fonts
drwxr-xr-x 2 root root 4096 Jun 27 10:12 nes
drwxr-xr-x 2 root root 4096 May 16 15:18 picoshare
drwxr-xr-x 3 root root 4096 Jun 28 04:06 sslocal
drwxrwxrwx 5 root root 4096 May 22 01:30 transmission

nginx配置反向代理路径匹配

四种路径匹配

访问路径固定为:http://x.x.x.x/user/index.html

location /user/ {
    proxy_pass http://127.0.0.1:8080/ => http://127.0.0.1:8080/index.html
}

location /user/ {
    proxy_pass http://127.0.0.1:8080 => http://127.0.0.1:8080/user/index.html
}

location /user/ {
    proxy_pass http://127.0.0.1:8080/admin/ => http://127.0.0.1:8080/admin/index.html
}

location /user/ {
    proxy_pass http://127.0.0.1:8080/admin => http://127.0.0.1:8080/adminindex.html
}

location模块的匹配模式

全路径匹配

进行uri的全路径精确匹配,要匹配的一模一样的uri
比如 location = /index ,那么只有路径为/index的才会匹配到

前缀模糊匹配 ^~

进行uri的前缀精确匹配
比如 location ^~ /user/ ,那么只要是路径以/user/开头的都会匹配到

无匹配符号的精确匹配

没有符号,按照路径开头精确匹配,但是匹配到后不会立即返回,
还会继续匹配其他普通匹配,如果匹配到,则会舍弃之前匹配的路径

比如 location /user/ , 当访问/user/开头时会匹配到
比如 location /user/admin,当访问/user/admin时会匹配到

模糊匹配 ~

进行uri的模糊匹配,区分大小写,匹配到后不再进行其他匹配
比如 location ~ /user/ ,当路径包含/user/时会匹配,比如/admin/user/或者/user/admin/
当然也可以用正则表达式来表示匹配路径:
比如 location ~ ^/user(.*)admin$,能够匹配以/user开头,admin结尾的路径,.*表示的是任意字符

模糊匹配 ~*

进行uri的模糊匹配,不区分大小写,匹配到后不再进行其他匹配,优先级与~相同,按照先后顺序优先
比如 location ~ /USER/,当路径包含/USER/时会匹配,如果是/user/则不会,因为区分大小写了

优先级:
由高到低:= 大于 ^~ 大于 其他,其他匹配根据先后顺序,配置在前的越优先

docker配置pull代理

1.创建 dockerd 相关的 systemd 目录,这个目录下的配置将覆盖 dockerd 的默认配置

sudo mkdir -p /etc/systemd/system/docker.service.d

2.新建配置文件 /etc/systemd/system/docker.service.d/http-proxy.conf,这个文件中将包含环境变量

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"

3.如果你自己建了私有的镜像仓库,需要 dockerd 绕过代理服务器直连,那么配置 NO_PROXY 变量:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"
Environment="NO_PROXY=your-registry.com,10.10.10.10,*.example.com"

4.重新加载配置文件,重启 dockerd

systemctl daemon-reload
systemctl restart docker

5.检查确认环境变量已经正确配置:

systemctl show --property=Environment docker