跳到正文

剪贴板

剪贴板 h9bgcuhw

原文
剪贴板信息

剪贴板信息

收录
更新
Python
import easygui as p
from sys import exit
import re, os
TIT = 'Vscode缺省源转换工具V0.1 by liangbob'
def is_cpp_file(filename):
    """检查文件名是否为C++源文件"""
    # 获取文件扩展名
    extension = filename.split('.')[-1].lower()

    # C++源文件通常的扩展名
    cpp_extensions = ['cpp', 'cc', 'cxx']

    # 检查文件扩展名是否在C++扩展名列表中
    return extension in cpp_extensions
def is_valid_filename(filename):
    # 文件名不能超过255个字符
    if len(filename) > 255:
        return False

    # 文件名不能包含以下字符:< > : " / \ | ? *
    if any(char in '< > : " / \\ | ? *' for char in filename):
        return False

    # 文件名不能包含控制字符(例如 CR(回车符)和 LF(换行符))
    if '\r' in filename or '\n' in filename:
        return False

    return True
fna = p.enterbox(msg = '缺省源文件名(例:cfzakxtq.cpp)', title = TIT)
if not fna:
    exit(0)
if not (is_cpp_file(fna) and is_valid_filename(fna)):
    p.msgbox("文件名格式不正确!", title = TIT, ok_button='确认')
    exit(0)
exi = os.path.exists(fna)
if not exi:
    p.msgbox("文件不存在!", title = TIT, ok_button='确认')
    exit(0)
try:
    f = open(fna)
except:
    p.msgbox("打开文件时出错!", title = TIT, ok_button='确认')
    exit(0)

MOD = '''
{
	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"%s":{
		"prefix": "%s",
		"body": [
%s
		]
	}
}
'''
t = f.read().splitlines()
z = ""
q = t[:-1]
for i in q:
    z += '			"'+i+'",\n'
z += '			"' + t[-1] + '"'
cfz = p.enterbox(msg = '请输入触发词', title = TIT)
if not cfz:
    exit(0)
nmr = "QSY" + str(__import__('random').randint(1, 10))

opt = MOD % (nmr, cfz, z)
with open('output.txt', 'w') as g:
    g.write(opt)
p.msgbox(msg = '缺省源转换完毕!转换的文件在 output.txt 文件中!', ok_button = '确认')