跳转至

Basic_IO

对UTF-8文本文件的读写

#include <locale>
using namespace std;

wstring* ReadFile(const wchar_t* FileName) { // remember to delete the wstring returned after use
    FILE* fl;
    _wfopen_s(&fl, FileName, L"rt+,ccs=UTF-8");
    locale loc("");
    wcout.imbue(loc);
    wstring *buf = new wstring;
    wchar_t tmp[100];
    while (!feof(fl)) {
        fgetws(tmp, 100, fl);
        *buf += tmp;
    }
    fclose(fl);
    return buf;
}

int WriteFile(const wchar_t* FileName, wstring String) {
    FILE* fl;
    if (_wfopen_s(&fl, FileName, L"wt+,ccs=UTF-8") != 0) {
        printf("Error writing file.\n");
        return 0;
    }
    locale loc("");
    wcin.imbue(loc);
    fputws(&String[0], fl);
    fclose(fl);
    return 1;
}

读字节码

char* ReadFileBytes(const char* FileName, int* NumberOfBytesRead) {
    FILE* fl;
    if (fopen_s(&fl, FileName, "rb+") != 0) {
        printf("Error reading file.\n");
        return NULL;
    }
    FILE* org = fl;
    fseek(fl, 0, SEEK_END);
    long fl_size = ftell(fl);
    fseek(fl, 0, SEEK_SET);

    char* buffer = new char[fl_size]; // remember to delete this
    fread(buffer, fl_size, 1, org);
    *NumberOfBytesRead = fl_size;
    fclose(fl);
    return buffer;
}

Split wstring

#include <sstream>
using namespace std;

vector<wstring> *split_wstring(wstring origin, const wchar_t split_char) {// remember to delete the vector returned after use
    vector<wstring> *parts = new vector<wstring>;
    wstringstream wss(origin);
    wstring tmp;
    while (getline(wss, tmp, split_char)) {
        (*parts).push_back(tmp);
    }
    return parts;
}

Base64

直接修改了一下网上的代码。

wstring

#include <codecvt>
using namespace std;

const wchar_t base[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const wchar_t padding_char = L'=';

static char find_pos(wchar_t ch)
{
    wchar_t *ptr = (wchar_t*)wcsrchr(base, ch);//the last position (the only) in base[]   
    return (ptr - base);
}

wstring base64_encode(const char* sourcedata) //加密的sourcedata仍为字节数组,不分编码
{
    wstring base64;
    int i = 0, j = 0;
    unsigned char trans_index = 0;
    const int datalength = strlen((const char*)sourcedata);
    for (; i < datalength; i += 3) {
        trans_index = ((sourcedata[i] >> 2) & 0x3f);
        base64+= base[(int)trans_index];
        trans_index = ((sourcedata[i] << 4) & 0x30);
        if (i + 1 < datalength) {
            trans_index |= ((sourcedata[i + 1] >> 4) & 0x0f);
            base64 += base[(int)trans_index];
        }
        else {
            base64 += base[(int)trans_index];
            base64 += padding_char;
            base64 += padding_char;
            break;
        }
        trans_index = ((sourcedata[i + 1] << 2) & 0x3c);
        if (i + 2 < datalength) {
            trans_index |= ((sourcedata[i + 2] >> 6) & 0x03);
            base64 += base[(int)trans_index];
            trans_index = sourcedata[i + 2] & 0x3f;
            base64 += base[(int)trans_index];
        }
        else {
            base64 += base[(int)trans_index];
            base64 += padding_char;
            break;
        }
    }
    return base64;
}

wstring base64_decode(wchar_t *data, int data_len)
{
    int ret_len = (data_len / 4) * 3;
    int equal_count = 0;
    char *ret = NULL;
    char *f = NULL;
    int tmp = 0;
    int temp = 0;
    int prepare = 0;
    int i = 0;
    if (*(data + data_len - 1) == padding_char)
    {
        equal_count += 1;
    }
    if (*(data + data_len - 2) == padding_char)
    {
        equal_count += 1;
    }
    if (*(data + data_len - 3) == padding_char)
    {//seems impossible   
        equal_count += 1;
    }
    switch (equal_count)
    {
    case 0:
        ret_len += 4;//3 + 1 [1 for NULL]   
        break;
    case 1:
        ret_len += 4;//Ceil((6*3)/8)+1   
        break;
    case 2:
        ret_len += 3;//Ceil((6*2)/8)+1   
        break;
    case 3:
        ret_len += 2;//Ceil((6*1)/8)+1   
        break;
    }
    ret = (char *)malloc(ret_len);
    if (ret == NULL)
    {
        printf("No enough memory.\n");
        exit(0);
    }
    memset(ret, 0, ret_len);
    f = ret;
    while (tmp < (data_len - equal_count))
    {
        temp = 0;
        prepare = 0;
        while (temp < 4)
        {
            if (tmp >= (data_len - equal_count))
            {
                break;
            }
            prepare = (prepare << 6) | (find_pos(data[tmp]));
            temp++;
            tmp++;
        }
        prepare = prepare << ((4 - temp) * 6);
        for (i = 0; i<3; i++)
        {
            if (i == temp)
            {
                break;
            }
            *f = (char)((prepare >> ((2 - i) * 8)) & 0xFF);
            f++;
        }
    }
    *f = '\0';

    typedef codecvt_utf8<wchar_t> convert_type;
    wstring_convert<convert_type, wchar_t> converter;
    return converter.from_bytes(ret);//ret不转换则为char*
}

ascii

#include "stdafx.h"  
#include <stdio.h>
#include <memory>
#include <cstring>

const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char padding_char = '=';

char *base64_decode(const char* data, int data_len);

int main(int argc, char* argv[])
{

    char dec1[] = "_r-+_Cl5;vgq_pdme7#7eC0=";
    char *dec = base64_decode(dec1, strlen(dec1));

    printf("%s", dec);

    free(dec);
    return 0;
}

int base64_encode(char * sourcedata, char * base64)
{
    int i=0, j=0;
    unsigned char trans_index=0;
    const int datalength = strlen((const char*)sourcedata);
    for (; i < datalength; i += 3){
        trans_index = ((sourcedata[i] >> 2) & 0x3f);
        base64[j++] = base[(int)trans_index];
        trans_index = ((sourcedata[i] << 4) & 0x30);
        if (i + 1 < datalength){
            trans_index |= ((sourcedata[i + 1] >> 4) & 0x0f);
            base64[j++] = base[(int)trans_index];
        }else{
            base64[j++] = base[(int)trans_index];
            base64[j++] = padding_char;
            base64[j++] = padding_char;
            break; 
        }
        trans_index = ((sourcedata[i + 1] << 2) & 0x3c);
        if (i + 2 < datalength){
            trans_index |= ((sourcedata[i + 2] >> 6) & 0x03);
            base64[j++] = base[(int)trans_index];
            trans_index = sourcedata[i + 2] & 0x3f;
            base64[j++] = base[(int)trans_index];
        }
        else{
            base64[j++] = base[(int)trans_index];
            base64[j++] = padding_char;
            break;
        }
    }
    base64[j] = '\0'; 
    return 0;
}


static char find_pos(char ch)
{
    char *ptr = (char*)strrchr(base, ch);//the last position (the only) in base[]   
    return (ptr - base);
}

/* Base64 解码 */
char *base64_decode(const char *data, int data_len)
{
    int ret_len = (data_len / 4) * 3;
    int equal_count = 0;
    char *ret = NULL;
    char *f = NULL;
    int tmp = 0;
    int temp = 0;
    int prepare = 0;
    int i = 0;
    if (*(data + data_len - 1) == padding_char)
    {
        equal_count += 1;
    }
    if (*(data + data_len - 2) == padding_char)
    {
        equal_count += 1;
    }
    if (*(data + data_len - 3) == padding_char)
    {//seems impossible   
        equal_count += 1;
    }
    switch (equal_count)
    {
    case 0:
        ret_len += 4;//3 + 1 [1 for NULL]   
        break;
    case 1:
        ret_len += 4;//Ceil((6*3)/8)+1   
        break;
    case 2:
        ret_len += 3;//Ceil((6*2)/8)+1   
        break;
    case 3:
        ret_len += 2;//Ceil((6*1)/8)+1   
        break;
    }
    ret = (char *)malloc(ret_len);
    if (ret == NULL)
    {
        printf("No enough memory.\n");
        exit(0);
    }
    memset(ret, 0, ret_len);
    f = ret;
    while (tmp < (data_len - equal_count))
    {
        temp = 0;
        prepare = 0;
        while (temp < 4)
        {
            if (tmp >= (data_len - equal_count))
            {
                break;
            }
            prepare = (prepare << 6) | (find_pos(data[tmp]));
            temp++;
            tmp++;
        }
        prepare = prepare << ((4 - temp) * 6);
        for (i = 0; i<3; i++)
        {
            if (i == temp)
            {
                break;
            }
            *f = (char)((prepare >> ((2 - i) * 8)) & 0xFF);
            f++;
        }
    }
    *f = '\0';
    return ret;
}