我正在尝试使用CMake实现跨平台构建系统.现在该软件有一些依赖性.我自己编译并将它们安装在我的系统上.
安装的一些示例文件:
-- Installing: /usr/local/share/SomeLib/SomeDir/somefile
-- Installing: /usr/local/share/SomeLib/SomeDir/someotherfile
-- Installing: /usr/local/lib/SomeLib/somesharedlibrary
-- Installing: /usr/local/lib/SomeLib/cmake/FindSomeLib.cmake
-- Installing: /usr/local/lib/SomeLib/cmake/HelperFile.cmake
Run Code Online (Sandbox Code Playgroud)
现在,CMake有一个find_package()打开Find*.cmake文件并在系统上的库之后搜索并定义一些变量等SomeLib_FOUND.
我的CMakeLists.txt包含这样的内容:
set(CMAKE_MODULE_PATH "/usr/local/lib/SomeLib/cmake/;${CMAKE_MODULE_PATH}")
find_package(SomeLib REQUIRED)
Run Code Online (Sandbox Code Playgroud)
第一个命令定义了CMake在搜索之后搜索的位置,Find*.cmake并且我添加SomeLib了FindSomeLib.cmake可以找到的目录,因此find_package()按预期工作.
但这find_package()有点奇怪,因为存在的原因之一是远离非跨平台硬编码路径.
这通常是怎么做的?我应该将cmake/目录复制SomeLib到我的项目中并设置CMAKE_MODULE_PATH相对吗?
memmove()函数将n个字节从内存区域src复制到内存区域dest.存储区可能重叠:复制的过程就好像src中的字节首先被复制到一个不与src或dest重叠的临时数组中,然后将字节从临时数组复制到dest.
我们可以只执行以下操作,而不是分配临时数组并复制值两次:
void *my_memmove(void *dest, const void *src, size_t n) {
signed char operation;
size_t end;
size_t current;
if(dest != src) {
if(dest < src) {
operation = 1;
current = 0;
end = n;
} else {
operation = -1;
current = n - 1;
end = -1;
}
for( ; current != end; current += operation) {
*(((unsigned char*)dest) + current) = *(((unsigned char*)src) + current);
}
}
return dest;
}
Run Code Online (Sandbox Code Playgroud)
在这个实现中,我们只是处理我们开始复制的位置.
我的实施有缺点吗?
注意:我实际上不会使用我的实现.我只是好奇.
我正在写一个模板引擎.它甚至支持多种"格式".目前它可以解析.php文件和.tpl(特定于此引擎).
我会给你一个两个例子,只是为了给你一个想法.
的template.php:
Name: <?php echo $this->h($name) ?>
Posts:
<?php foreach($posts as $post): ?>
- <?php echo $this->h($post->name) ?> (<?php echo count($post->comments) ?> comments)
<?php echo $this->render('post/shortpost', array('post' => $post)) ?>
<?php endforeach ?>
Run Code Online (Sandbox Code Playgroud)
这基本上只是一个标准的PHP.
template.tpl
Name: {>$name}
Posts:
{foreach($posts as $post):}
- {>$post->name} ({=count($post->comments)} comments)
{=:render('post/shortpost', array('post' => $post))}
{endforeach}
Run Code Online (Sandbox Code Playgroud)
这种模板化的"语言"简单地被翻译成上面的PHP.
目前使用这些模板进行解析eval().
临
反对
我最近阅读了关于php中的流包装器.你甚至可以创建自己的.另一种解决方案eval是为每个模板"格式"创建自定义流包装器,并使用include来解析模板.
这有以下(潜在)缺陷:
临
考虑以下程序:
#include <stdio.h>
void some_func(char*, int*, char*);
void stack_alignment(void) {
char a = '-';
int i = 1337;
char b = '+';
some_func(&a, &i, &b); // to prevent the compiler from removing the local variables
printf("%c|%i|%c", a, i, b);
}
Run Code Online (Sandbox Code Playgroud)
它生成以下程序集(由我自己添加的注释,我是程序集的完整新手):
$ vim stack-alignment.c
$ gcc -c -S -O3 stack-alignment.c
$ cat stack-alignment.s
.file "stack-alignment.c"
.section .rdata,"dr"
LC0:
.ascii "%c|%i|%c\0"
.text
.p2align 2,,3
.globl _stack_alignment
.def _stack_alignment; .scl 2; .type 32; .endef
_stack_alignment:
LFB7:
.cfi_startproc
subl $44, %esp
.cfi_def_cfa_offset 48 …Run Code Online (Sandbox Code Playgroud) 我的代码目前看起来像这样(这些步骤分为多个函数):
/* open file */
FILE *file = fopen(filename, "r+");
if(!file) {
/* read the file */
/* modify the data */
/* truncate file (how does this work?)*/
/* write new data into file */
/* close file */
fclose(file);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以在"w"模式下打开文件,但在这种情况下我不想这样做.我知道有一个函数ftruncate在unistd.h/ sys/types.h,但我不希望使用这些功能,我的代码应该是高度可移植(在Windows上也是如此).
是否有可能在不关闭/重新打开文件的情况下清除文件?
我正在为a编写一个包装器std::stringstream,我希望operator<<通过我的类将所有调用转发给std::stringstream.现在这很好用(感谢这个问题:STL流的包装类:前向运算符<<调用),但它仍有一个问题.
假设我有以下代码:
class StreamWrapper {
private:
std::stringstream buffer;
public:
template<typename T>
void write(T &t);
template<typename T>
friend StreamWrapper& operator<<(StreamWrapper& o, T const& t);
// other stuff ...
};
template<typename T>
StreamWrapper& operator<<(StreamWrapper& o, T const& t) {
o.write(t);
return o;
}
template<typename T>
void StreamWrapper::write(T& t) {
// other stuff ...
buffer << t;
// other stuff ...
}
Run Code Online (Sandbox Code Playgroud)
如果我现在这样做:
StreamWrapper wrapper;
wrapper << "text" << 15 << "stuff";
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,如果我想使用流修饰符std::endl,这是一个根据 …
假设我们有以下代码(用于某种搜索或类似):
$stmt = $pdo->prepare("SELECT * FROM users WHERE username LIKE ?");
$stmt->execute(array('%' . $username . '%'));
Run Code Online (Sandbox Code Playgroud)
提供的用户名已正确转义,但字符%(= 0 或更多任意字符)和_(= 恰好 1 个任意字符)被 MySQL 解释为通配符。
我知道用户可以输入%或 the_进行搜索,如果我希望搜索功能正常工作,我应该转义它。(在类似的情况下set_pt并获得setopt结果)。
但我的问题是:有人可以利用这个吗?如果是,那么有人会如何利用这一点以及如何防止它?下面的功能够用吗?
function escape_like_string($str) {
return str_replace(Array('%', '_'), Array('\%', '\_'), $str);
}
Run Code Online (Sandbox Code Playgroud)
我能想到的一种可能性是输入大量的%,因此服务器需要分配大量内存。这行得通吗?
我有以下课程:
template <typename T>
class A
{
public:
void method(const char *buffer);
// the template T is used inside this method for a local variable
};
Run Code Online (Sandbox Code Playgroud)
现在我需要一个具有不同模板的类的实例数组,例如:
std::vector<A*> array;
array.push_back(new A<uint32_t>);
array.push_back(new A<int32_t>);
Run Code Online (Sandbox Code Playgroud)
但是std::vector<A*> array;不会工作,因为我显然需要指定一个模板,但我不能这样,因为我在这个数组中存储了不同的类型.是否有某种通用类型或其他解决方案?
我有以下代码:
#include <windows.h>
#include <iostream>
static DWORD __stdcall startThread(void *);
class BaseClass {
private:
void threadLoop() {
// stuff ...
std::cout << someStuff() << std::endl;
// stuff ...
}
protected:
HANDLE handle;
virtual int someStuff() {
return 1;
}
public:
friend DWORD __stdcall startThread(void *);
BaseClass() {
handle = 0;
};
void start() {
handle = CreateThread(NULL, 0, startThread, this, 0, NULL);
}
~BaseClass() {
if(handle != 0) {
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
}
}
// stuff
};
static DWORD __stdcall …Run Code Online (Sandbox Code Playgroud)