来自  资质荣誉 2019-09-22 07:26 的文章
当前位置: 澳门太阳娱乐手机登录 > 资质荣誉 > 正文

自己实现,NDK开发文件列表

strlen strcat strcpy strcmp 自身实现

NDK开采一开端接触的就是合法给的demo:HelloJni

strlen

include <stdio.h>#include <string.h>#include <assert.h>size_t my_strlen(const char* str){  assert(str != NULL);  const char *tmp = str;  size_t count = 0;  while(*tmp++ != ''){    count++;  }  return count;}size_t my_strlen1(const char* str){  assert(str != NULL);  if(*str == 0){    return 0;  }else{    return my_strlen1 + 1;  }}int main(){  char as[] = "hello C";  printf("%ldn",strlen;  printf("%ldn",my_strlen;  printf("%ldn",my_strlen1;}

但是里面包车型大巴代码非凡轻便啦,然后自身想写二个C语言来遍历手提式有线电电话机的公文夹,然后android体现出来,作用正是那样了。

strcat

#include <stdio.h>#include <string.h>#include <assert.h>#include <malloc.h>char* my_strcat(char* strd, const char* strs){  assert(strd != NULL && strs != NULL);  char *tmp = strd;  while(*tmp++ != 0){}  tmp--;  while(*strs != 0){    * = *strs++;  }  *tmp = '';  return strd;}int main(){  char s1[20] = "hello";  char s2[] = " C";  printf("strcat before s1 = %sn", s1);  char *str = my_strcat;  printf("strcat after s1 = %sn", s1);  printf("strcat after str = %sn", str);}

首先要去化解的难点正是怎么遍历;然后境遇的主题材料就是怎么把遍历出来的公文列表重回给android代码,直接贴代码吧

strcpy

#include <stdio.h>#include <string.h>#include <assert.h>#include <malloc.h>char* my_strcpy(char* strd, const char* strs){  assert(NULL != strd && NULL != strs);  char* tmp = strd;  while(*strs != ''){    *tmp++ = *strs++;  }  *tmp = '';  return strd;}int main(){  char s1[20] = "hello";  char s2[] = " wod";  printf("strcpy before s1 = [%s]n", s1);  char *str = my_strcpy;  printf("strcpy after s1 = [%s]n", s1);  printf("strcat after str = [%s]n", str);}

int total = 0;

char filePaths[100][256] = {""};

void searchdir(const char *path, const char *tempPath) {

DIR *dp;

struct dirent *dmsg;

if ((dp = opendir(path)) != NULL) {

chdir(path);

while ((dmsg = readdir(dp)) != NULL) {

if (!strcmp(dmsg->d_name, ".") || !strcmp(dmsg->d_name, "..")) {

continue;

}

if (dmsg->d_type == DT_DIR) {

char *filePath = malloc(strlen(tempPath) + strlen(dmsg->d_name) + 20);

strcpy(filePath, tempPath);

strcat(filePath, "/");

strcat(filePath, dmsg->d_name);

searchdir(dmsg->d_name, filePath);

free(filePath);

} else {

int size = strlen(dmsg->d_name);

if (strcmp(dmsg->d_name + (size - 4), ".mp3") == 0) {

char *filePath = malloc(strlen(tempPath) + strlen(dmsg->d_name) + 20);

strcpy(filePath, tempPath);

strcat(filePath, "/");

strcat(filePath, dmsg->d_name);

strcpy(filePaths[total], filePath);

free(filePath);

total++;

}

}

}

chdir("..");

closedir(dp);

}

}

strcmp

#include <stdio.h>#include <string.h>#include <assert.h>#include <malloc.h>int my_strcmp(const char *s1, const char *s2){  assert(NULL != s1 && NULL != s2);  int res = 0;  while(*s1 != '' || *s2 != ''){    if(*s1 > *s2){      res = 1;      break;    }else if(*s1 < *s2){      res = -1;      break;    }else{      s1++;      s2++;    }  }  return res;}int main(){  char *s1 = "a1123";  char *s2 = "a1123";  int res = my_strcmp;  if{    printf("s1 == s2n");  }else if(res > 0){    printf("s1 > s2n");  }else{    printf("s1 < s2n");  }}

如上是遍历手提式有线电话机的措施,个中,if (strcmp(dmsg->d_name + (size - 4), ".VCD") == 0)这些论断是,只选取mp4文书。二维数组filePaths,是存放在全体遍历结果

实施完那一个方法后,就是要把结果传回到的长河了

jobjectArray  Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env, jobject thiz) {

total = 0;

clock_t tick_start, tick_end;

double t;

char *dirpath = "/storage/emulated/0/";

tick_start = clock();

searchdir(dirpath, dirpath);

tick_end = clock();

double dtime = (double) (tick_end - tick_start) / CLOCKS_PER_SEC;

jclass objClass = (*env)->FindClass(env, "java/lang/String");

jobjectArray texts = (*env)->NewObjectArray(env, (jsize) ARRAY_LENGTH, objClass, 0);

jstring jstr;

int i = 0;

for (; i < ARRAY_LENGTH; i++) {

jstr = (*env)->NewStringUTF(env, filePaths[i]);

(*env)->SetObjectArrayElement(env, texts, i, jstr); //必需放入jstring

}

return texts;

}

内部变量dtime便是开销的时日,经过自家的实验,C遍历时间比java要快相当多,小编扫描3k个文件夹,C费用3s左右,java花了20多s.

变量dirpath是本身写死的围观伊始路线,应该视同一律,也足以由java那边传参数过来。

澳门太阳娱乐在线网址,C那边即使是写完了!

java这边用String[] aa = stringFromJNI();接收下就足以了.

下一篇文章,笔者会分享下,java得到DVD文件的撰稿人,专辑等新闻境遇的乱码难题,两篇作品使用的是贰个类型

地址:

本文由澳门太阳娱乐手机登录发布于 资质荣誉,转载请注明出处:自己实现,NDK开发文件列表

关键词: