#include <jni.h> #include <string> #include <stdio.h> #include <stdlib.h> #include "logs.h" char *fa() { //char *pa = "123456"; // pa 指针在栈区,“123456”在常量区,该函数调用完后指针变量 pa 就被释放了 char *p = NULL; //指针变量 p 在栈中分配 4 字节 p = (char *) malloc(100); //本函数在这里开辟了一块堆区的内存空间,并把地址赋值给 p strcpy(p, "wudunxiong 1234566"); //把常量区的字符串拷贝到堆区 return p; //返回给主调函数 fb(),相对 fa 来说 fb 是主调函数,相对 main 来说, //fa(),fb()都是被调用函数 } char *fb() { char *pstr = NULL; pstr = fa(); return pstr; //指针变量 pstr 在这就结束 } void selectSort(int *p, int n) { // LOGE("%d\n", p[0]); int tmp; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (p[i] > p[j]) { tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } } } char *getMem(char **p, int n) { *p = (char *) (malloc(n)); if (*p == NULL) return NULL; return *p; } void displayArray(int(*p)[4], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { //LOGE("%d ", *(*(p + i) + j)); LOGE("%d ", *(*(p + i) + j)); } putchar(10); } } int mm = 500; char *jstringToChar(jstring pJstring); void func(int *p) { *p = 400; } void func2(int **pp) { // *pp = &mm; *pp = &mm; } typedef struct node { char *name; char *passwd; struct node *next; } Node; Node *createList() { Node *head = static_cast<Node *>(malloc(sizeof(Node))); head->next = NULL; return head; } void insertList(Node *head, Node *item) { item->next = head->next; head->next = item; } void travereList(Node *head) { head = head->next; while (head) { LOGE("name = %s passwd = %s\n", head->name, head->passwd); head = head->next; } } void trimStrSpace(char *str) { char *t = str; while (*str) { if (*str != ' ') { *t++ = *str; } str++; } *t = '\0'; } //void readConfigFile(char *fileName, Node *head) { // FILE *fp = fopen(fileName, "r"); // if (fp == NULL) // return; // // char buf[1024]; // int len = 0; // char *p; // while (fgets(buf, 1024, fp) != NULL) { // trimStrSpace(buf); // if (*buf == '#' || *buf == '\n' || *buf == '[') continue; // len = strlen(buf); // LOGE("len:%d\n", len); // if (buf[len - 1] == '\n') // buf[len - 1] = '\0'; // p = strtok(buf, "="); // LOGE("p:%s\n", p); // if (p == NULL) continue; // LOGE("cur:%d\n", sizeof(Node)); // Node *cur = static_cast<Node *>(malloc(sizeof(Node))); // LOGE("p.size:%d\n", strlen(p) + 1); // cur->name = static_cast<char *>(malloc(strlen(p) + 1)); // strcpy(cur->name, p); // p = strtok(NULL, "="); // LOGE("p.size:%d\n", strlen(p) + 1); // cur->passwd = static_cast<char *>(malloc(strlen(p) + 1)); // strcpy(cur->passwd, p); // insertList(head, cur); // } // // //} void trimStrLeftSpace(char *str) { char *pTmp = str; while (*pTmp == ' ') pTmp++; while (*pTmp) *str++ = *pTmp++; *str = '\0'; } #include <pthread.h> pthread_key_t p_key; void func1() { int *tmp = static_cast<int *>(pthread_getspecific(p_key)); LOGE("%d is running in %s\n", *tmp, __func__); } void *thread_func(void *args) { pthread_setspecific(p_key, args); int *tmp = static_cast<int *>(pthread_getspecific(p_key)); LOGE("%d is running in %s\n", *tmp, __func__); *tmp = (*tmp) * 100; func1(); return (void *) 0; } int main_pthread() { pthread_t pa, pb; int a = 1; int b = 2; pthread_key_create(&p_key, NULL); pthread_create(&pa, NULL, thread_func, &a); pthread_create(&pb, NULL, thread_func, &b); pthread_join(pa, NULL); pthread_join(pb, NULL); return 0; } struct node1 { int num; struct node1 *next; }; struct node *creat(); void print(node1 *pNode1); node1 *creat(node1 *pNode1); void main_node() { struct node1 *head; head = NULL;//创建一个空表 head = creat(head);//创建单链表 print(head);//打印单链表 } void print(struct node1 *head) { struct node1 *temp; temp = head; LOGE("\n\n\n链表存入的值为:\n"); while (temp != NULL) { LOGE("%6d\n", temp->num);/*输出链表节点的值*/ temp = temp->next; } LOGE("链表打印结束!!"); } node1 *creat(node1 *head) { struct node1 *p1, *p2; int i = 1; p1 = p2 = static_cast<node1 *>(malloc(sizeof(struct node1))); // LOGE("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %p\n", p1); p1->num = 1; p1->next = NULL; // LOGE("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %p\n,p2_ADDR= %p\n", p1,p2); LOGE("请输入值,值小于等于0结束,值存放地址为:head= %p\n", head); LOGE("p1->num:%d\n,p2->num:%d\n", p1->num, p2->num); while (i < 5) { if (head == NULL) { head = p1; } else { p2->next = p1; // LOGE("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %p\n,p2_ADDR= %p\n", p1,p2); p2 = p1; } // LOGE("p1->num:%d\n,p2->num:%d\n",p1->num,p2->num); p1 = static_cast<node1 *>(malloc(sizeof(struct node1))); i += 1; // printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %p\n", i, p2); // LOGE("请输入值,值小于等于0结束,值存放地址为:i:%d,head_ADDR= %p\n", i, head); // LOGE("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %p\n", i, p2); p1->num = i + 1; } LOGE("head->num:%d\n,p2->num:%d\n", head->num, p2->num); free(p1); p1 = NULL; p2->next = NULL; LOGE("链表输入结束(END)\n"); return head; } #define LEN sizeof(NODE) typedef struct _NODE { int val; struct _NODE *next; } NODE, *PNODE; void print(PNODE head) { while (head) { LOGE("%3d", head->val); head = head->next; } LOGE("\n"); } void insertHead(PNODE *pHead, int val) {//头插法 PNODE n = static_cast<PNODE>(malloc(LEN)); n->val = val; n->next = *pHead; *pHead = n; } void insertTail(PNODE *pHead, int val) {//尾插发 PNODE t = *pHead; PNODE n = static_cast<PNODE>(malloc(LEN)); n->val = val; n->next = NULL; if (*pHead == NULL) { n->next = *pHead; *pHead = n; } else { while (t->next) { t = t->next; } t->next = n; } } void deleteHead(PNODE *pHead) {//删除头 if (*pHead == NULL) { return; } else { PNODE t = *pHead; *pHead = (*pHead)->next; free(t); } } void deleteTail(PNODE *pHead) {//删除尾 PNODE t = *pHead; if (t == NULL) { return; } else if (t->next == NULL) { free(t); *pHead = NULL; } else { while (t->next->next != NULL) { t = t->next; } free(t->next); t->next = NULL; } } PNODE findByVal(PNODE head, int val) { while (head != NULL && head->val != val) { head = head->next; } return head; } PNODE findByIndex(PNODE head, int index) {//根据索引找节点 if (index == 1) { return head; } else { int c = 1; while (head != NULL && index != c) { head = head->next; c++; } } return head; } void insertByIndex(PNODE *pHead, int index, int val) { if (index == 1) { insertHead(pHead, val); } else { //PNODE t = findByIndex(*pHead, index - 1); PNODE t = findByIndex(*pHead, index - 1);//front if (t == NULL) { return; } else { // PNODE n = t->next; // t->next = static_cast<_NODE *>(malloc(LEN)); // t->next->next = n; // t->next->val = val; PNODE n = t->next;//target t->next = static_cast<_NODE *>(malloc(LEN)); t->next->next = n; t->next->val = val; } } } void deleteByIndex(PNODE *pHead, int index) { if (index == 1) { deleteHead(pHead); } else { PNODE t = findByIndex(*pHead, index - 1);//front if (t == NULL || t->next == NULL) { return; } else { // PNODE n = t->next->next;//back; // free(t->next);//target // t->next = n; PNODE n = t->next->next; free(t->next); t->next = n; } } } void deleteByVal(PNODE *pHead, int val) { if (*pHead == NULL) { return; } else { if ((*pHead)->val == val) { deleteHead(pHead); } else { PNODE t = *pHead; while (t->next != NULL && t->next->val != val) { t = t->next; } if (t->next) {//target PNODE n = t->next->next;//next free(t->next); t->next = n; } } } } void clear(PNODE *pHead)//清除链表 { //while ((*pHead) != NULL) { while ((*pHead) != NULL) deleteHead(pHead);//从头删除 } #define gap8_fl1(x) (31 - __builtin_clz((x))) static int array[32]; void main_risc() { int test = 0xFFFFF00; while (test > 0) { test = test >> 1; LOGE("test:%x gap8:%d \n", test, gap8_fl1(test)); } } void main_NODE() { PNODE head = NULL; insertTail(&head, 1); // deleteHead(&head); insertTail(&head, 2); insertTail(&head, 3); insertTail(&head, 4); insertTail(&head, 5); insertTail(&head, 6); // print(head); insertByIndex(&head, 6, 9); // print(head); deleteByVal(&head, 2); //print(head); clear(&head); // print(head); insertByIndex(&head, 1, 12); print(head); } void *my_memcpy(void *dst, const void *src, size_t n) { char *tmp = (char *) dst;//dst=head,tmp->递增游标 //char *s_src = (char *) src; char *s_src = (char *) src; // while (n--) { // *tmp++ = *s_src++; // } while (n--) { *tmp++ = *s_src++; } return dst; } void main_memcpy() { int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; my_memcpy(a + 3, a, 5 * sizeof(int)); int i = 0; for (int i = 0; i < 10; i++) { LOGE("%d,", a[i]); //0,1,2,0,1,2,0,1,8,9, } } void main_env() { LOGE("PATH : %s\n", getenv("PATH")); LOGE("HOME : %s\n", getenv("HOME")); LOGE("ROOT : %s\n", getenv("ROOT")); } #define BLOCK_SIZE 1024*1024*256 void main_calloc() { int i = 0; char *buf[10]; while ( i < 10) { buf[i] = static_cast<char *>(calloc(1, BLOCK_SIZE)); i++; } } int main_s(char *path) { //main_env(); //main_memcpy(); // main_risc(); // main_NODE(); // main_node(); // main_pthread(); // Node *node = createList(); // readConfigFile(path, node); // travereList(node); return 0; } char *jstringToChar(JNIEnv *env, jstring jstr) { char *rtn = NULL; // jclass clsstring = env->FindClass("java/lang/String"); // jstring strencode = env->NewStringUTF("utf-8"); // jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jclass clsstring = env->FindClass("java/lang/String"); jstring strencode = env->NewStringUTF("utf-8"); jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); // jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode); // jsize alen = env->GetArrayLength(barr); // jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE); jbyteArray barr = static_cast<jbyteArray>(env->CallObjectMethod(jstr, mid, strencode)); jsize alen = env->GetArrayLength(barr); jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE); // if (alen > 0) { // rtn = (char *) malloc(alen + 1); // memcpy(rtn, ba, alen); // rtn[alen] = 0; // } if (alen > 0) { rtn = static_cast<char *>(malloc(alen + 1)); memcpy(rtn, ba, alen); rtn[alen] = 0; } // env->ReleaseByteArrayElements(barr, ba, 0); env->ReleaseByteArrayElements(barr, ba, 0); return rtn; } extern "C" JNIEXPORT jstring JNICALL Java_com_massky_chars_1s_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */, jstring path) { std::string hello = "Hello from C++"; // const jchar *ss = env->GetStringChars(path, 0); char *ss = jstringToChar(env, path); main_s(ss); env->ReleaseStringChars(path, reinterpret_cast<const jchar *>(ss)); return env->NewStringUTF(hello.c_str()); }
jni调用各种c api汇总
最新推荐文章于 2024-03-14 17:01:51 发布