概述
也许有人要问为什么要自己用c写?
原因是:
1 我是菜鸟
2 我最最初选择了AsyncSocket,弄了很久,基本上稳定了,但有时候出现了数据不完整,但我又没办法在这个时候识别到并重试;所以不完美,最后放弃
3 我又用NSMutableURLRequest+NSURLConnection,这个玩意儿能帮你处理不定长度的返回内容,但有2个问题:a、修改Connection为closed无效,Accept、Accept-Language头删除不了;b、某些页面浏览器能正常打开,但NSURLConnection却只能获得http头,获取不了内容,这听起来不可能,但我试过了同步sendSynchronousRequest、异步委托“connectionWithRequest:request delegate:self”、异步队列“NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler”,统统不行。我实在不知道错在哪里。
最后我只能上了我的绝招,逼不得已,用了c来实现。
上代码,造福百姓:
#include <stdio.h>
//#include <malloc.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <stdarg.h>
#include <strings.h>
#ifndef HELPER
//#define DEBUG 1//定义了就会显示很多信息
#define HELPER 1
typedef struct {
//char shecma[8];
char host[128];
char url[1024];
int port;
//char queryString[1024];
} URI;
//flag=1会替换#和?,返回的是新申请的内存空间(实际上是replace函数申请的)
char *encodeURI(char *data, int flag);
long microTime();
//找出url的主机和url地址(域名后的部分)及端口
URI parseUrl(char * url);
//截取字符串
char* substr(char* str, int start, int len);
//转小写
void strToLower(char * str);
//转大写
void strToUpper(char * str);
//分割字符串
char** split(char *strin, char *seprator, int *l, int max);
//查找字符位置
unsigned long strpos(char *str, char *find);
//替换,返回的需要释放,至多替换150个
char* replace(char *strin, char *find, char *replace);
char* replaceAndCount(char *strin, char *find, char *replace, int *count);
//替换,至多替换150个,与replace的区别在于,此函数会把你传进去的原字符串free掉
char* doReplace(char *strin, char *find, char *replace);
//传入字符获取字符代表的今天的毫秒
long date(const char *ft);
//现在的毫秒
long Time();
//去除头尾的空白
char *trim(char *str);
//读文件
char* readFile(char* filename, int limit);
//写文件
void fastWriteFile(char* filename, char* data, int append);
void writeFile(char* filename, char* data, unsigned long len, int append);
char* getUniqueString();
int myrand();
int getRand(int min, int max);
//能保证dest以0结尾,注意,不能处理内存越界,dest可用内存至少为len+1
void sstrncpy(char* dest, char* src, unsigned long len);
char* make_message(const char *fmt, ...);
void dateStr(const char *ft, char* rtn);
void dateStrByTs(const char *ft, char* rtn, long ts);
void dateStrByDiffSecondsOfToday(const char *ft, char* rtn, long ts);
int isLower(char c);
int isUpper(char c);
int toLower(char c);
int toUpper(char c);
unsigned long parse16(char* str);
#endif
#include "helper.h"
void n2fs(long i, char *buf) {
long G = 1024 * 1024 * 1024;
long M = 1024 * 1024;
long K = 1024;
//char *buf=malloc(128);//[128];
memset(buf, 0, 128);
int j = 0;
/*if(i>T)
{
j=floor(i/T);
sprintf(buf,"%d T",j);
i=i%T;
}*/
if (i > G) {
j = floor(i / G);
if (strlen(buf) > 0)
sprintf(buf, "%s,%dG", buf, j);
else
sprintf(buf, "%dG", j);
i = i % G;
}
if (i > M) {
j = floor(i / M);
if (strlen(buf) > 0)
sprintf(buf, "%s,%dM", buf, j);
else
sprintf(buf, "%dM", j);
i = i % M;
}
if (i > K) {
j = floor(i / K);
if (strlen(buf) > 0)
sprintf(buf, "%s,%dK", buf, j);
else
sprintf(buf, "%dK", j);
i = i % K;
}
if (i > 0) {
if (strlen(buf) > 0)
sprintf(buf, "%s,%ldByte", buf, i);
else
sprintf(buf, "%ldByte", i);
}
}
long date(const char *ft) {
int i = 0;
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
double j = 0;
while (i < strlen(ft)) {
switch (ft[i]) {
case 'Y':
j = j + (local->tm_year + 1900) * 10000000000.;
break;
case 'm':
j = j + (local->tm_mon + 1) * 100000000.;
break;
case 'd':
j = j + local->tm_mday * 1000000.;
break;
case 'H':
j = j + local->tm_hour * 10000;
break;
case 'i':
j = j + local->tm_min * 100;
break;
case 's':
j = j + local->tm_sec;
break;
}
i++;
}
return j;
}
/**
* 获得今天制定格式的
*/
void dateStr(const char *ft, char* rtn) {
return dateStrByTs(ft, rtn, 0);
}
void dateStrByDiffSecondsOfToday(const char *ft, char* rtn, long ts) {
time_t t;
t = time(NULL);
long in = ((long) t) + ts;
//printf("T=%ld, ts=%ld, in=%ldn", (long) t, ts, in);
return dateStrByTs(ft, rtn, in);
}
void dateStrByTs(const char *ft, char* rtn, long ts) {
int i = 0;
struct tm *local;
time_t t;
if (ts == 0)
t = time(NULL);
else
t = (time_t) ts;
local = localtime(&t);
int j = 0;
rtn[0] = 0;
while (i < strlen(ft)) {
switch (ft[i]) {
case 'Y':
j = (local->tm_year + 1900);
sprintf(rtn, "%s%d", rtn, j);
break;
case 'y':
j = (local->tm_year + 1900);
j -= (int) (j / 100);
sprintf(rtn, "%s%d", rtn, j);
break;
case 'm':
j = (local->tm_mon + 1);
sprintf(rtn, j < 10 ? "%s0%d" : "%s%d", rtn, j);
break;
case 'n':
j = (local->tm_mon + 1);
sprintf(rtn, "%s%d", rtn, j);
break;
case 'd':
j = local->tm_mday;
sprintf(rtn, j < 10 ? "%s0%d" : "%s%d", rtn, j);
break;
case 'D':
j = local->tm_mday;
sprintf(rtn, "%s%d", rtn, j);
break;
case 'j':
j = local->tm_mday;
sprintf(rtn, "%s%d", rtn, j);
break;
case 'N':
j = local->tm_wday;
sprintf(rtn, "%s%d", rtn, j);
break;
case 'H':
j = local->tm_hour;
sprintf(rtn, j < 10 ? "%s0%d" : "%s%d", rtn, j);
break;
case 'G':
j = local->tm_hour;
sprintf(rtn, "%s%d", rtn, j);
break;
case 'i':
j = local->tm_min;
sprintf(rtn, j < 10 ? "%s0%d" : "%s%d", rtn, j);
break;
case 'I':
j = local->tm_min;
sprintf(rtn, "%s%d", rtn, j);
break;
case 's':
j = local->tm_sec;
sprintf(rtn, j < 10 ? "%s0%d" : "%s%d", rtn, j);
break;
case 'S':
j = local->tm_sec;
sprintf(rtn, "%s%d", rtn, j);
break;
default:
sprintf(rtn, "%s%c", rtn, ft[i]);
break;
}
i++;
}
}
char** split(char *strin, char *seprator, int *l, int max) {
char **rtn;
rtn = (char **) malloc(sizeof(char **));
char *str = strdup(strin);
char *str0 = str;
char *p;
p = strstr(str, seprator);
if (p == NULL) {
*l = 1;
*rtn = str;
return rtn;
}
unsigned long len, spl, inlen, plen;
spl = strlen(seprator);
max--;
*l = 0;
while (p != NULL) {
if (*l > 0)
rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
inlen = strlen(str);
plen = strlen(p);
if (!plen)
break;
len = inlen - plen;
rtn[*l] = malloc(len + 1);
strncpy(rtn[*l], str, len);
rtn[*l][len] = 0;
str += (len + spl);
p = strstr(str, seprator);
(*l)++;
if (*l == max || p == NULL) {
/*if (*l == max) {
len = strlen(rtn[*l - 1]) + strlen(str) + 1;
rtn[*l - 1] = realloc(rtn[*l - 1], len);
strncat(rtn[*l - 1], str, len - 1);
} else {*/
len = strlen(str);
rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
rtn[*l] = malloc(len + 1);
strncpy(rtn[*l], str, len);
rtn[*l][len] = 0;
//}
(*l)++;
break;
}
}
free(str0);
return rtn;
}
unsigned long strpos(char *strin, char *seprator) {
char *right = strstr(strin, seprator);
if (right == NULL)
return -1;
return strlen(strin) - strlen(right);
}
char* doReplace(char *strin, char *find, char *replace) {
//int len = strlen(strin);
int count = 0;
char *rtn = replaceAndCount(strin, find, replace, &count);
//strcpy(strin, rtn);
free(strin);
return rtn;
}
char* replace(char *strin, char *find, char *replace) {
int count = 0;
return replaceAndCount(strin, find, replace, &count);
}
char* replaceAndCount(char *strin, char *find, char *replace, int *count) {
char* rtn;
unsigned long lenf = strlen(find);
unsigned long lenr = strlen(replace);
unsigned long rtn_len;
if (lenf > lenr)
rtn_len = strlen(strin) + 1;
else
rtn_len = strlen(strin) + (lenr - lenf) * 1000 + 1;
rtn = (char*) malloc(rtn_len);
rtn[0] = 0;
char *str = strdup(strin);
char *sp = strdup(find);
char *p;
p = strstr(str, sp);
if (p == NULL) {
free(sp);
free(rtn);
return str;
}
unsigned long spl = strlen(sp);
unsigned long sl = strlen(str);
unsigned long i = 0, len;
char *tmp = (char*) malloc(sl + 1);
while (p != NULL) {
if (i++ > 0)
strcat(rtn, replace);
len = sl - strlen(p);
strncat(rtn, str, len);
strcpy(tmp, p + spl);
strcpy(str, tmp);
sl = strlen(str);
p = strstr(str, sp);
(*count)++;
if (*count >= 999)
break;
}
if (strlen(str)) {
if (i > 0)
strcat(rtn, replace);
strcat(rtn, str);
(*count)++;
}
free(tmp);
free(str);
free(sp);
return rtn;
}
long microTime() {
struct timeval start;
//struct timezone tz; //后面有说明
gettimeofday(&start, NULL); //gettimeofday(&start,&tz);结果一样
// printf("start.tv_sec:%dn",start.tv_sec);
// printf("start.tv_usec:%dn",start.tv_usec);
return start.tv_sec * 1000 + start.tv_usec / 1000;
}
long Time() {
time_t t;
t = time(NULL);
return (long) t;
}
char *trim(char *str) {
int i = 0;
unsigned long l = 0;
char *s; //=malloc(strlen(str)+1);
s = strdup(str);
while (*(s + i) == 13 || *(s + i) == 10 || *(s + i) == 32) {
i++;
}
memset(s, 0, strlen(s));
strcpy(s, str + i);
l = strlen(s) - 1;
while (*(s + l) == 13 || *(s + l) == 10 || *(s + l) == 32) {
l--;
}
memset(s, 0, strlen(s));
strncpy(s, str + i, l + 1);
return s;
}
char* readFile(char* filename, int limit) {
char *buffer = malloc(limit + 1);
buffer[0] = 0;
FILE *fp;
if ((fp = fopen(filename, "r")) == 0) {
free(buffer);
buffer = NULL;
return NULL;
}
rewind(fp);
int pos = 0;
int len = 10;
unsigned long l;
char* buf = malloc(len);
while (!feof(fp)) {
l = fread(buf, 1, len, fp);
pos += l;
if (l > 0)
strncat(buffer, buf, l);
if (pos + len > limit) {
printf("filesize %s bigger than %dn", filename, limit);
break;
}
}
free(buf);
//printf("Read %s, data length: %dn", filename, pos);
fclose(fp);
buffer[pos - 1] = '