Skip to content

Commit

Permalink
add some code
Browse files Browse the repository at this point in the history
  • Loading branch information
hoterran committed Nov 19, 2013
1 parent 259cb7a commit d35b286
Show file tree
Hide file tree
Showing 36 changed files with 1,702 additions and 12 deletions.
5 changes: 5 additions & 0 deletions a.c
@@ -0,0 +1,5 @@
#include <stdio.h>

int main(int argc, char* argv[]) {
printf("%d, %s", argc, argv[0]);
}
31 changes: 31 additions & 0 deletions charset.c
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void binary(char c) {
int i;
for (i = 7; i >= 0; i--) {
if(c & (1 << i))
printf("1");
else
printf("0");
}
}

int main(int argc, char* argv[]) {
char *s = argv[1];
int l = strlen(s);
int i;

for (i = 0; i < l; i++) {
binary(s[i]);
printf(" ");
}
printf("\n");
for (i = 0; i < l; i++) {
printf("%8d", s[i]);
printf(" ");
}
printf("\n");
return 0;
}
33 changes: 33 additions & 0 deletions charset_file.c
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void binary(char c) {
int i;
for (i = 7; i >= 0; i--) {
if(c & (1 << i))
printf("1");
else
printf("0");
}
}

int main(int argc, char* argv[]) {
FILE *f = fopen(argv[1], "r");
char s[1000] = {};
fread(s, 1, 10000, f);
int l = strlen(s);
int i;
printf("--------------------%d----------------------", l);
for (i = 0; i < l; i++) {
binary(s[i]);
printf(" ");
}
printf("\n");
for (i = 0; i < l; i++) {
printf("%8d", s[i]);
printf(" ");
}
printf("\n");
return 0;
}
12 changes: 6 additions & 6 deletions clear_non_ascii_or_utf8.c
Expand Up @@ -24,9 +24,9 @@ bool clear_non_ascii_or_utf8(char* str, int length)
chr <<= 1;
nBytes++;
}
if (nBytes < 2 || nBytes > 6) {
if (nBytes < 2 || nBytes > 4) {
// non 110x
str[i] = '-'; //第一个字节最少为110x xxxx
str[i] = '_'; //第一个字节最少为110x xxxx
i++;
nBytes = 0;
continue;
Expand All @@ -42,11 +42,11 @@ bool clear_non_ascii_or_utf8(char* str, int length)
// non 10xx
for(; utf8Start < i; utf8Start++) {
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '-';
str[utf8Start] = '_';
}
utf8Start = 0;
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '-';
if ((str[i] & 0x80) != 0)
str[i] = '_';
i++;
nBytes = 0;
continue;
Expand All @@ -62,7 +62,7 @@ bool clear_non_ascii_or_utf8(char* str, int length)
// non complete end
for(; utf8Start < i; utf8Start++) {
if ((str[utf8Start] & 0x80) != 0)
str[utf8Start] = '-';
str[utf8Start] = '_';
}
utf8Start = 0;
nBytes = 0;
Expand Down
160 changes: 160 additions & 0 deletions clear_non_utf8.c
@@ -0,0 +1,160 @@
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static const char UTF8_BYTE_PREFIX = 0x80;
static const char UTF8_BYTE_MASK = 0xc0;

int clear_non_ascii_or_utf8(char *str, size_t len)
{
unsigned int i = 0;
while(i < len) {
uint16_t unicode = 0;
if((str[i] & 0x80) == 0x00) // one byte
{
i++;
}
else if((str[i] & 0xe0) == 0xc0) // two bytes
{
if (i + 1 > len) {
str[i] = '_';
return 1;
}
/* check whether the byte is in format 10xxxxxx */
if (((str[i + 1]) & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
i = i + 2;
continue;
}
/* get unicode value */
unicode = (((str[i] & 0x1f) << 6) | (str[i + 1] & 0x3f));
/* validate unicode range */
if (!(unicode >= 0x80 && unicode <= 0x7ff)) {
str[i] = '_';
str[i + 1] = '_';
i = i + 2;
continue;
}
i = i + 2;
}
else if((str[i] & 0xf0) == 0xe0) // three bytes
{
if (i + 1 > len) {
str[i] = '_';
return 1;
}
if (i + 2 > len) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
return 1;
}
/* check whether the byte is in format 10xxxxxx */
if ((str[i + 1] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
i = i + 2;
continue;
}
if ((str[i + 2] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
str[i + 1] = '_';
if ((str[i + 2] & 0x80) != 0x00)
str[i + 2] = '_';
i = i + 3;
continue;
}
/* get unicode value */
unicode = (((str[i] & 0x0f) << 12) | (( str[i + 1] & 0x3f) << 6) | (str[i + 2] & 0x3f));
/* validate unicode range */
if (!(unicode >= 0x800 /* && unicode <= 0xffff */)) {
str[i] = '_';
str[i + 1] = '_';
str[i + 2] = '_';
i = i + 3;
continue;
}
i = i + 3;
}
else if((str[i] & 0xf8) == 0xf0) // four bytes
{
if (i + 1 > len) {
str[i] = '_';
return 1;
}
if (i + 2 > len) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
return 1;
}
if (i + 3 > len) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
if ((str[i + 2] & 0x80) != 0x00)
str[i + 2] = '_';
return 1;
}
/* check whether the byte is in format 10xxxxxx */
if ((str[i + 1] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
if ((str[i + 1] & 0x80) != 0x00)
str[i + 1] = '_';
i = i + 2;
continue;
}
if ((str[i + 2] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
str[i + 1] = '_';
if ((str[i + 2] & 0x80) != 0x00)
str[i + 2] = '_';
i = i + 3;
continue;
}
if ((str[i + 3] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) {
str[i] = '_';
str[i + 1] = '_';
str[i + 2] = '_';
if ((str[i + 3] & 0x80) != 0x00)
str[i + 3] = '_';
i = i + 4;
continue;
}
uint32_t unicode = 0x00000000; // for 4 bytes utf-8 encoding
unicode = ((str[i] & 0x07) << 18) | ((str[i + 1] & 0x3f) << 12) |
((str[i + 2] & 0x3f) << 6) | ((str[i + 3]& 0x3f));
if (!(unicode >= 0x00010000 && unicode <= 0x0010ffff)) { /* validate unicode range */
str[i] = '_';
str[i + 1] = '_';
str[i + 2] = '_';
str[i + 3] = '_';
i = i + 4;
continue;
}
i = i + 4;
}
else
{
str[i] = '_';
i++;
continue;
}
}
return 0;
}

int main(int argc, char *argv[])
{
FILE *f = fopen(argv[1], "r");
char s[3000];

while (NULL != fgets(s, (int)sizeof(s), f)) {
clear_non_ascii_or_utf8(s, strlen(s));
printf("%s\n", s);
}
}
64 changes: 64 additions & 0 deletions cpu_affinity_multi_set.c
@@ -0,0 +1,64 @@
#define _GNU_SOURCE

#include <stdint.h>
#include <stdio.h>
#include <sched.h>
#include <pthread.h>
#include <stdlib.h>

#define app_panic(format, args...) \
do { \
printf(format, ## args); \
abort(); \
} while(0)

static inline void print_cpu_mask(cpu_set_t cpu_mask)
{
uint8_t flag;
uint32_t i;
printf("Cpu affinity is ");
flag = 0;
for (i = 0; i < sizeof(cpu_set_t); i ++) {
if (CPU_ISSET(i, &cpu_mask)) {
if (flag == 0) {
flag = 1;
printf("%d", i);
} else {
printf(",%d", i);
}
}
}
printf(".\n");
}

static inline void get_cpu_mask(pid_t pid, cpu_set_t *mask) {
if (sched_getaffinity(pid, sizeof(cpu_set_t), mask) == -1) {
app_panic("Get cpu affinity failed.\n");
}
}

static inline void set_cpu_mask(pid_t pid, cpu_set_t *mask) {
if (sched_setaffinity(pid, sizeof(cpu_set_t), mask)
== -1) {
app_panic("Set cpu affinity failed.\n");
}
}

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

get_cpu_mask(0, &cpu_mask);
print_cpu_mask(cpu_mask);

CPU_ZERO(&cpu_mask);
CPU_SET(1, &cpu_mask);
CPU_SET(2, &cpu_mask);
set_cpu_mask(0, &cpu_mask);

get_cpu_mask(0, &cpu_mask);
print_cpu_mask(cpu_mask);

while(1);
return 0;
}
65 changes: 65 additions & 0 deletions cpu_affinity_set.c
@@ -0,0 +1,65 @@
#define _GNU_SOURCE

#include <stdint.h>
#include <stdio.h>
#include <sched.h>
#include <pthread.h>
#include <stdlib.h>

#define app_panic(format, args...) \
do { \
printf(format, ## args); \
abort(); \
} while(0)

static inline void print_cpu_mask(cpu_set_t cpu_mask)
{
uint8_t flag;
uint32_t i;
printf("Cpu affinity is ");
flag = 0;
for (i = 0; i < sizeof(cpu_set_t); i ++) {
if (CPU_ISSET(i, &cpu_mask)) {
if (flag == 0) {
flag = 1;
printf("%d", i);
} else {
printf(",%d", i);
}
}
}
printf(".\n");
}

static inline void get_cpu_mask(pid_t pid, cpu_set_t *mask) {
if (sched_getaffinity(pid, sizeof(cpu_set_t), mask) == -1) {
app_panic("Get cpu affinity failed.\n");
}
}

static inline void set_cpu_mask(pid_t pid, cpu_set_t *mask) {
if (sched_setaffinity(pid, sizeof(cpu_set_t), mask)
== -1) {
app_panic("Set cpu affinity failed.\n");
}
}

int main(int argc, char *argv[])
{
/* argv[1] is cpu number */
uint32_t active_cpu = atoi(argv[1]);
cpu_set_t cpu_mask;

get_cpu_mask(0, &cpu_mask);
print_cpu_mask(cpu_mask);

CPU_ZERO(&cpu_mask);
CPU_SET(active_cpu, &cpu_mask);
set_cpu_mask(0, &cpu_mask);

get_cpu_mask(0, &cpu_mask);
print_cpu_mask(cpu_mask);

while(1);
return 0;
}

0 comments on commit d35b286

Please sign in to comment.