关于MAGIC文件...
Saturday, 21. October 2006, 17:04:05
作用是检查文件开始的几个字节,来判定文件的类型。
以下是magic文件在apache中的mime_magic_module模块的用法和定义...(译者:金步国)
囧囧囧囧囧囧囧囧囧囧
Saturday, 21. October 2006, 17:04:05
Friday, 6. October 2006, 02:51:09
Friday, 6. October 2006, 02:00:01
Wednesday, 4. October 2006, 15:52:23
Monday, 2. October 2006, 11:44:14
// Prime number computation by Erwin Unruh
template <int i> struct D { D(void*); operator int(); };
template <int p, int i> struct is_prime {
enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1> :: prim };
};
template <int i> struct Prime_print {
Prime_print<i-1> a;
enum { prim = is_prime<i, i-1>::prim };
void f() { D<i> d = prim ? 1 : 0; a.f();}
};
template<> struct is_prime<0,0> { enum {prim=1}; };
template<> struct is_prime<0,1> { enum {prim=1}; };
template<> struct Prime_print<1> {
enum {prim=0};
void f() { D<1> d = prim ? 1 : 0; };
};
#ifndef LAST
#define LAST 18
#endif
main() {
Prime_print<LAST> a;
a.f();
}
unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 17]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 13]': unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 13]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 11]': unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 11]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 7]': unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 7]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 5]': unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 5]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 3]': unruh.cpp:12: invalid conversion from `int' to `void*' unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 3]' unruh.cpp: In member function `void Prime_print<i>::f() [with int i = 2]':
Sunday, 13. August 2006, 07:18:35
typedef struct list_s *list;
struct list_s {
list prev;
list next;
void *item;
};
/* Create an empty list, returning a pointer to the list */
list list_new(void)
{
list l = zmalloc(sizeof(struct list_s)); //分配空间
l->next = l->prev = l;
l->item = NULL;
return l;
}
/* Delete a list, freeing its nodes */
void list_delete(list l)
{
list p = l, q;
do {
q = p;
p = p->next;
free(q);
} while (p != l);
}
size_t list_length(list l)
{
list p;
size_t length = 0;
for (p = l->next; p != l; p = p->next)
++length;
return length;
}
/* Add an item to the head of a list, returning the new list head */
list list_prepend(list l, void *i)
{
list n = zmalloc(sizeof(struct list_s));
n->next = l->next;
n->prev = l;
n->item = i;
l->next = l->next->prev = n;
return n;
}
/* Add an item to the tail of a list, returning the new list tail */
list list_append(list l, void *i)
{
list n = zmalloc(sizeof(struct list_s));
n->next = l;
n->prev = l->prev;
n->item = i;
l->prev = l->prev->next = n;
return n;
}
/* Return the first item of a list, or NULL if the list is empty */
void *list_head(list l)
{
list p = l->next;
if (p == l)
return NULL;
return p->item;
}
/* Remove the first item of a list, returning the item, or NULL if the
list is empty */
void *list_behead(list l)
{
void *i;
list p = l->next;
if (p == l)
return NULL;
i = p->item;
l->next = l->next->next;
l->next->prev = l;
free(p);
return i;
}
/* Remove the last item of a list, returning the item, or NULL if the
list is empty */
void *list_betail(list l)
{
void *i;
list p = l->prev;
if (p == l)
return NULL;
i = p->item;
l->prev = l->prev->prev;
l->prev->next = l;
free(p);
return i;
}
/* Return the nth item of l, or l->item (usually NULL) if that is out
of range */
void *list_at(list l, size_t n)
{
size_t i;
list p;
assert(l != NULL);
for (p = list_first(l), i = 0; p != l && i < n; p = list_next(p), i++)
;
return p->item;
}
/* Sort list l with qsort using comparison function cmp */
void list_sort(list l, int (*cmp)(const void *p1, const void *p2))
{
list p;
void **vec;
size_t i, len = list_length(l);
assert(l != NULL && cmp != NULL); //assert传入数值不为空
vec = (void **)zmalloc(sizeof(void *) * len);
for (p = list_first(l), i = 0; i < len; p = list_next(p), ++i)
vec[i] = (void *)p->item;
qsort(vec, len, sizeof(void *), cmp);
for (p = list_first(l), i = 0; i < len; p = list_next(p), ++i)
p->item = vec[i];
free(vec);
}
七种qsort排序方法
<本文中排序都是采用的从小到大排序>
一、对int类型数组排序
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
二、对char类型数组排序(同int类型)
char word[100];
Sample:
int cmp( const void *a , const void *b )
{
return *(char *)a - *(int *)b;
}
qsort(word,100,sizeof(word[0]),cmp);
三、对double类型数组排序(特别要注意)
double in[100];
int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
四、对结构体一级排序
struct In
{
double data;
int other;
}s[100]
//按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写
int cmp( const void *a ,const void *b)
{
return (*(In *)a)->data > (*(In *)b)->data ? 1 : -1;
}
qsort(s,100,sizeof(s[0]),cmp);
五、对结构体二级排序
struct In
{
int x;
int y;
}s[100];
//按照x从小到大排序,当x相等时按照y从大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);
六、对字符串进行排序
struct In
{
int data;
char str[100];
}s[100];
//按照结构体中字符串str的字典顺序排序
int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a)->str , (*(In *)b)->str );
}
qsort(s,100,sizeof(s[0]),cmp);
七、计算几何中求凸包的cmp
int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的所有点,旋转角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0) return 1;
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) //如果在一条直线上,则把远的放在前面
return 1;
else return -1;
}
其中的qsort函数包含在<stdlib.h>的头文件里,strcmp包含在<string.h>的头文件里
typedef struct Point Point; typedef struct Marker Marker; typedef struct list_s Line; /* This is evil! */ typedef struct Undo Undo; typedef struct Region Region; typedef struct Buffer Buffer; typedef struct Window Window; typedef struct Completion Completion; typedef struct History History; typedef struct Terminal Terminal;
/* Point and Marker. */
struct Point {
Line *p; /* Line pointer.(list) */
size_t n; /* Line number. */
size_t o; /* Offset. */
};
Saturday, 5. August 2006, 15:09:28
/*Each time
the string is enlarged beyond the current size of the buffer it is
reallocated with realloc.*/
struct astr_s {
char * text; // a buffer that contains the C string
size_t len; // the size of the string
size_t maxlen; //the buffer size
};
typedef struct astr_s *astr;
extern astr astr_new(void);
//implement
astr astr_new(void)
{
astr as;
as = (astr)zmalloc(sizeof *as);
as->maxlen = ALLOCATION_CHUNK_SIZE;
as->len = 0;
as->text = (char *)zmalloc(as->maxlen + 1);
memset(as->text, 0, as->maxlen + 1);
return as;
}
#define ALLOCATION_CHUNK_SIZE16
void *zmalloc(size_t size)
{
void *ptr;
assert(size > 0); //assert(断言) size_t, string size of allocation
//calloc函数分配足够大的内存区来存储size个元素的数组,每个元素的长度为1
if ((ptr = calloc(size, (size_t)1)) == NULL) {
fprintf(stderr, "zile: cannot allocate memory\n");
zile_exit(1); //-_-b i've discover 3 zile_exit(exitcode) prototype...node temporarily
}
return ptr;
}
42 void *
43 zmalloc(int severity, size_t n)
44 {
45 void*p;
46
47 if ((p = malloc(n)) == NULL) //malloc分配存储n长度的内存
48 _errmsg("UXzmalloc1", severity,
49 "Cannot allocate a block of %d bytes.",
50 n);
51 return (p);
52 }
static void astr_resize(astr as, size_t reqsize)
{
assert(as != NULL);
if (reqsize > as->maxlen) {
//重新分配大小,STL中Vector的分配方法是成对应比例加倍,而这里是用分配的空间加上初始大小
as->maxlen = reqsize + ALLOCATION_CHUNK_SIZE;
as->text = (char *)zrealloc(as->text, as->maxlen + 1);
}
}
/*
* Resize an allocated memory area.
*/
void *zrealloc(void *ptr, size_t size)
{
void *newptr;
assert(size > 0);
if ((newptr = realloc(ptr, size)) == NULL) {
fprintf(stderr, "zile: cannot reallocate memory\n");
zile_exit(1);
}
return newptr;
}
static int astr_pos(astr as, ptrdiff_t pos)
{
assert(as != NULL);
if (pos < 0) // deal with negative....eg: -1 pos in last position of string
pos = as->len + pos;
assert(pos >=0 && pos <= (int)as->len); //assert pos in astr string
return pos;
}
char *astr_char(const astr as, ptrdiff_t pos)
{
assert(as != NULL);
pos = astr_pos(as, pos);
return as->text + pos;
}
void astr_delete(astr as)
{
assert(as != NULL);
free(as->text);
as->text = NULL;
free(as);
}
static astr astr_cpy_x(astr as, const char *s, size_t csize)
{
astr_resize(as, csize); //1 resize string text size
memcpy(as->text, s, csize); //2 memory copy s(csize) to as->text;
as->len = csize; //3 reset len of text
as->text[csize] = '\0'; //4 add NULL character to text
return as;
}
astr astr_cpy(astr as, const astr src)
{
assert(src != NULL);
return astr_cpy_x(as, src->text, src->len);
}
astr astr_cpy_cstr(astr as, const char *s)
{
assert(s != NULL);
return astr_cpy_x(as, s, strlen(s));
}
//1
static astr astr_cat_x(astr as, const char *s, size_t csize)
{
astr_resize(as, as->len + csize);
memcpy(as->text + as->len, s, csize); // different copy at this position
as->len += csize;
as->text[as->len] = '\0';
return as;
}
//2
astr astr_cat(astr as, const astr src)
{
assert(src != NULL);
return astr_cat_x(as, src->text, src->len);
}
//3
astr astr_ncat_cstr(astr as, const char *s, size_t len)
{
return astr_cat_x(as, s, len);
}
//cat a char to astr string
astr astr_cat_char(astr as, int c)
{
assert(as != NULL);
astr_resize(as, as->len + 1);
as->text[as->len] = (char)c;
as->text[++as->len] = '\0';
return as;
}
//cat astr src to astr as and delete src
astr astr_cat_delete(astr as, const astr src)
{
assert(src != NULL);
astr_cat_x(as, src->text, src->len);
astr_delete(src);
return as;
}
astr astr_substr(const astr as, ptrdiff_t pos, size_t size)
{
assert(as != NULL);
pos = astr_pos(as, pos); // location
assert(pos + size <= as->len); //assert substring not longer than astring len
return astr_ncat_cstr(astr_new(), astr_char(as, pos), size); //allocation new astr for memory space...
}
int astr_find_cstr(const astr as, const char *s)
{
char *sp;
assert(as != NULL && s != NULL);
sp = strstr(as->text, s);
return (sp == NULL) ? -1 : sp - as->text;
}
int astr_rfind(const astr as, const astr src)
{
return astr_rfind_cstr(as, src->text);
}
int astr_rfind_cstr(const astr as, const char *s)
{
char *sp;
assert(as != NULL && s != NULL);
sp = strrstr(as->text, s);
return (sp == NULL) ? -1 : sp - as->text;
}
static astr astr_replace_x(astr as, ptrdiff_t pos, size_t size, const char *s, size_t csize)
{
astr tail;
assert(as != NULL);
pos = astr_pos(as, pos); //broudary of position check
if (as->len - pos < size)
size = as->len - pos; //if size beyond, cut
//这里获得as的最后一个字符,分配一个新astr struct把tail string放入, 这里的tail char是指在pos+size截断后的字符,比如ABCDE中pos为2,size为1,则tail string DE
tail = astr_substr(as, pos + (ptrdiff_t)size, astr_len(as) - (pos + size));
astr_truncate(as, pos); // 截断string在pos位置
astr_ncat_cstr(as, s, csize); //把c style string cat到pos后
astr_cat(as, tail); //cat回tail string
astr_delete(tail); //释放astr tail
return as;
}
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
|
| ||||||
| 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 | ||
到蓝星北京去实习ING的猥琐男- -EROMAN...超级猥琐....猥琐到死...无限猥琐....ERO界的新星...
创造奇迹的少年的BLOG...其实是个渣变态XD
KS君的BLOG....最近貌似出了点问题...= =帮不上忙中..
果冻少年的BLOG...= =
本部某雅的BLOG...
原BLOG
GOOGLE PAGE主页
给真正喜欢E文的人...
PS2DEV上的开发WIKI...
很不错的软件工程站...
要下什么电子书到这BLOG吧...全啊...
science fiction sites search...
Boutell.Com, Inc. publishes articles about the workings of the World Wide Web, free of charge
技术文档
电子书
华丽的资讯站..
自由资讯
三思科学
STL 中文站
Center Of STL Study
C++ FAQ
数学史资料
network dictionary
http://www.cs.ualberta.ca/~mburo/orts/doxygen/html/files.html
GIS网站
传说中最强壮的论坛...XS佬的大本营...
这里么~~是一个很CJ很CJ的地方
传说中的SOS团隐藏基地
一个很囧的网络漫画
某国外animeBBS