Skip to content

Instantly share code, notes, and snippets.

@psychoss
Last active January 19, 2016 09:23
Show Gist options
  • Save psychoss/0c9e982390f4c9706d62 to your computer and use it in GitHub Desktop.
Save psychoss/0c9e982390f4c9706d62 to your computer and use it in GitHub Desktop.
NDK
#ifndef __dbg_h
#define __dbg_h
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifndef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s (in function '%s'):%d: " M "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s (in function '%s'):%d: errno: %s) " M "\n", __FILE__, __FUNCTION__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define jump_to_error_if(A) if (A) { goto error; }
#define jump_to_error_unless(A) if (!(A)) { goto error; }
#endif
//
#ifndef __dbg_h
#define __dbg_h
#include <stdio.h>
#ifndef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s (in function '%s'):%d: " M "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif
#define jump_unless(A) if (!(A)) { goto error; }
#define error_unless(A, M, ...) if (!(A)) { fprintf(stderr, M "\n", ##__VA_ARGS__); goto error; }
#endif
#include <jni.h>
#ifdef __ANDROID__
#include <android/log.h>
#define TAG "CURL-C"
#define LOGE(...) (__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
#define LOGD(...) (__android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__))
#else
#define LOGE(...) ((void)0)
#define LOGD(...) ((void)0)
#endif
#undef __cplusplus
#include <jni.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <fcntl.h>
#include <android/log.h>
#include <poll.h>
#include <stddef.h>
#define CHUNK_SIZE 512
#define LOG_LEVEL 9
#define LOG_TAG "Logger"
#define LOGU(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_UNKNOWN, LOG_TAG, __VA_ARGS__);}
#define LOGD(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_DEFAULT, LOG_TAG, __VA_ARGS__);}
#define LOGV(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__);}
#define LOGDE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);}
#define LOGI(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__);}
#define LOGW(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);}
#define LOGE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);}
#define LOGF(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__);}
#define LOGS(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_SILENT, LOG_TAG, __VA_ARGS__);}
//, jstring host,
// jstring path
jstring Java_com_psycho_photo_MainActivity_request(JNIEnv *env, jobject thiz,
jstring host, jstring path) {
jstring r;
const char* n_host = (*env)->GetStringUTFChars(env, host, 0);
const char* n_path = (*env)->GetStringUTFChars(env, path, 0);
struct hostent *server_h;
server_h = gethostbyname(n_host);
int socket_desc;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
LOGE(3, "Create socket failed");
return r;
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(80);
memcpy(&serv_addr.sin_addr.s_addr, server_h->h_addr,server_h->h_length);
if (connect(socket_desc, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
< 0) {
LOGE(3, "Connect failed");
return r;
}
char message[1024];
char *msg_fmt, server_reply[2000];
msg_fmt = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n";
sprintf(message, msg_fmt, n_path, n_host);
if (send(socket_desc, &message, strlen(message), 0) < 0) {
LOGE(3, "Send message failed");
return r;
}
char buffer[512];
int ptr = 0;
ssize_t rc;
struct pollfd fd = { .fd = socket_desc, .events = POLLIN };
poll(&fd, 1, 0); // Doesn't wait for data to arrive.
while (fd.revents & POLLIN) {
rc = read(socket_desc, buffer + ptr, sizeof(buffer) - ptr);
if (rc <= 0)
break;
ptr += rc;
poll(&fd, 1, 0);
}
LOGW(3, "Print chunk is %s", buffer);
// int size_recv, total_size = 0;
// int timeout = 4;
// struct timeval begin, now;
// char chunk[CHUNK_SIZE];
// char *result = "*";
// double timediff;
//
// fcntl(socket_desc, F_SETFL, O_NONBLOCK);
//
// gettimeofday(&begin, NULL);
//
// while (1) {
// gettimeofday(&now, NULL);
//
// timediff = (now.tv_sec - begin.tv_sec)
// + 1e-6 * (now.tv_usec - begin.tv_usec);
//
// if (total_size > 0 && timediff > timeout) {
// LOGW(3, "Time out break");
// break;
// }
//
// else if (timediff > timeout * 2) {
// LOGW(3, "Time out break");
// break;
// }
//
// memset(chunk, 0, CHUNK_SIZE); //clear the variable
// if ((size_recv = recv(socket_desc, chunk, CHUNK_SIZE, 0)) < 0) {
//
// usleep(100000);
// } else {
// total_size += size_recv;
// LOGW(3, "Print chunk is %d", total_size);
// //strcat(result, &chunk);
// strcat(result, chunk);
// gettimeofday(&begin, NULL);
// }
// }
// LOGW(3, "Print chunk is %s", result);
r = (*env)->NewStringUTF(env, &buffer);
//(*env)->ReleaseStringUTFChars(env, host, n_host);
return r;
}
typedef struct {
int *array;
size_t used;
size_t size;
} Array;
void initArray(Array *a, size_t initialSize) {
a->array = (int *) malloc(initialSize * sizeof(int));
a->used = 0;
a->size = initialSize;
}
void insertArray(Array *a, int element) {
if (a->used == a->size) {
a->size *= 2;
a->array = (int *) realloc(a->array, a->size * sizeof(int));
}
a->array[a->used++] = element;
}
void freeArray(Array *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
#undef __cplusplus
#include <jni.h>
#include <dirent.h>
#include <stdio.h>
#include <stddef.h>
#include "file_util.h"
#ifdef __ANDROID__
#include <android/log.h>
#define TAG "Logger"
#define LOGE(...) (__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
#define LOGD(...) (__android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__))
#else
#define LOGE(...) ((void)0)
#define LOGD(...) ((void)0)
#endif
#define MIN_BUF_SIZE 512
#define SPLIT_DELIMITER "Subject:"
#define EXTENSION ".mhtml"
char *trim(char *str) {
char *end;
while (isspace(*str))
str++;
if (*str == 0)
return str;
end = str + strlen(str) - 1;
while (end > str && isspace(*end))
end--;
*(end + 1) = 0;
return str;
}
char *replace(const char *s, char ch, const char *repl) {
int count = 0;
const char *t;
for (t = s; *t; t++)
count += (*t == ch);
size_t rlen = strlen(repl);
char *res = malloc(strlen(s) + (rlen - 1) * count + 1);
char *ptr = res;
for (t = s; *t; t++) {
if (*t == ch) {
memcpy(ptr, repl, rlen);
ptr += rlen;
} else {
*ptr++ = *t;
}
}
*ptr = 0;
return res;
}
int start_with(const char *content, const char *pattern) {
if (strncmp(content, pattern, strlen(pattern)) == 0)
return 1;
return 0;
}
void Java_com_psycho_photo_MainActivity_changefilename(JNIEnv *env,
jobject thiz, jstring path) {
/*
* Convert the jstring to const char*
* */
const char *n_path = (*env)->GetStringUTFChars(env, path, 0);
DIR *d;
struct dirent *dir;
d = opendir(n_path);
// For make a absolute path
char filename[256];
char target_filename[256];
// Read from the file
char content[MIN_BUF_SIZE];
if (d) {
while ((dir = readdir(d)) != NULL) {
// the filename without parent
// like '1.txt'
char *name = dir->d_name;
// reset the variables
memset(filename, 0, 256);
memset(target_filename, 0, 256);
memset(content, 0, MIN_BUF_SIZE);
// check the file extension
if (strlen(name) > 6
&& !strcmp(name + strlen(name) - 6, EXTENSION)) {
// make a absolute path
strcat(filename, n_path);
strcat(filename, "/");
strcat(filename, name);
// check if the file is exist
if (file_exists(&filename)) {
int fd = open(filename, 0x00000000);
if (fd >= 0) {
read(fd, content, MIN_BUF_SIZE);
char *pch = NULL;
pch = strtok(content, "\r\n");
while (pch != NULL) {
if (start_with(pch, SPLIT_DELIMITER)) {
int sl = strlen(pch) - strlen(SPLIT_DELIMITER);
char tmp[sl + 1];
memcpy(tmp, &pch[strlen(SPLIT_DELIMITER)], sl);
tmp[sl] = '\0';
char *tmp_fn = trim(tmp);
tmp_fn = replace(tmp_fn, ':', "-");
tmp_fn = replace(tmp_fn, '.', "-");
// make a absolute path
strcat(target_filename, n_path);
strcat(target_filename, "/");
strcat(target_filename, tmp_fn);
strcat(target_filename, EXTENSION);
rename(filename, target_filename);
LOGD("target_filename:%s\n", target_filename);
}
pch = strtok(NULL, "\r\n");
}
//break;
}
}
}
}
closedir(d);
}
(*env)->ReleaseStringUTFChars(env, path, n_path);
}

sudo adb start-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment