-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlcreator.cpp
More file actions
94 lines (78 loc) · 2.92 KB
/
sqlcreator.cpp
File metadata and controls
94 lines (78 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "sqlcreator.h"
SqlCreator::SqlCreator() {}
char* replaceBackslashes(const char* input);
void SqlCreator::creatsql(TreeNode *root,const char *path)
{
char dirpath[MAX_PATH_LENGTH],filepath[MAX_PATH_LENGTH];
snprintf(dirpath,sizeof(dirpath),"%s\\sqldir.txt",path);
snprintf(filepath,sizeof(filepath),"%s\\sqlfile.txt",path);
FILE *Dir = fopen(dirpath,"w");
FILE *File = fopen(filepath,"w");
mystack ss;
ss.push(path,root);
while(ss.getsize())
{
TreeNode *nownode = ss.getnode();
ss.pop();
char *newpath;
newpath = replaceBackslashes(nownode->path);
//strncpy(newpath,nownode->path,strlen(nownode->path)-1);
if(nownode->tag == DIRR)
{
TreeNode *p = nownode->firstChild;
int dircount = 0;
int filecount = 0;
while(p != nullptr)
{
if(p->tag == DIRR)dircount++;
if(p->tag == FILEE)filecount++;
p = p->nextSibling;
}
fprintf(Dir,"INSERT INTO directories (name, path, subdirectories_count, files_count, depth)VALUES ('%s', '%s', %d, %d, %d);\n",\
nownode->name,newpath,dircount,filecount,nownode->dirdeep);
}
else
{
fprintf(File,"INSERT INTO files (name, path, size, depth, last_modified)VALUES ('%s', '%s', %ld, %d, '%04d-%02d-%02d %02d:%02d:%02d');\n",\
nownode->name,newpath,nownode->file_size,nownode->dirdeep,nownode->stLocal.wYear,nownode->stLocal.wMonth, nownode->stLocal.wDay,nownode->stLocal.wHour, nownode->stLocal.wMinute, nownode->stLocal.wSecond);
}
if(nownode->firstChild != nullptr)
ss.push(path,nownode->firstChild);
if(nownode->nextSibling != nullptr)
ss.push(path,nownode->nextSibling);
}
// fprintf(Dir,"Damn");
// fprintf(File,"Man");
fclose(Dir);
fclose(File);
}
// 函数实现
char* replaceBackslashes(const char* input) {
// 计算新字符串所需的内存大小
size_t inputLength = strlen(input);
size_t newLength = 0;
for (size_t i = 0; i < inputLength; i++) {
if (input[i] == '\\') {
newLength++;
}
newLength++;
}
// 分配新字符串的内存
char* result = (char*)malloc(newLength + 1);
if (!result) {
// 内存分配失败
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// 替换并复制字符
size_t j = 0;
for (size_t i = 0; i < inputLength; i++) {
if (input[i] == '\\') {
result[j++] = '\\';
}
result[j++] = input[i];
}
// 在新字符串的末尾添加 null 终止符
result[newLength] = '\0';
return result;
}