Android development tools line_endings hacking
/********************************************************************
* Android development tools line_endings hacking
* 说明:
* 本文主要是对android源代码中的line_endings开发工具进行了解读,
* 目的是为了知道传说中的dos,unix文件之间转换的工作机制。
*
* 2016-5-3 深圳 南山平山村 曾剑锋
*******************************************************************/ #include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h> #define BUFSIZE (1024*8)
static void to_unix(char* buf);
static void unix_to_dos(char* buf2, const char* buf); // 使用方法
int usage()
{
fprintf(stderr, "usage: line_endings unix|dos FILES\n"
"\n"
"Convert FILES to either unix or dos line endings.\n");
return ;
} // 定义Node数据结构
typedef struct Node {
struct Node *next;
char buf[BUFSIZE*+];
} Node; int
main(int argc, char** argv)
{
// 枚举UNIX,DOS两种数据
enum { UNIX, DOS } ending;
int i; // 参数个数判断
if (argc < ) {
return usage();
} // 参数比较
if ( == strcmp("unix", argv[])) {
ending = UNIX;
}
else if ( == strcmp("dos", argv[])) {
ending = DOS;
}
else {
return usage();
} // 命令行传入的参数可能有多个,利用for循环进行轮流转换。
for (i=; i<argc; i++) {
int fd;
int len; // force implied
chmod(argv[i], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); // 打开文件
fd = open(argv[i], O_RDWR);
if (fd < ) {
fprintf(stderr, "unable to open file for read/write: %s\n", argv[i]);
return ;
} // 获取文件大小
len = lseek(fd, , SEEK_END);
lseek(fd, , SEEK_SET); // 文件长度正常才有必要进行转换
if (len > ) {
// 创建根节点
Node* root = malloc(sizeof(Node));
Node* node = root;
node->buf[] = ; // root节点的buf数据位0 while (len > ) {
// 创建节点,并出示节点
node->next = malloc(sizeof(Node));
node = node->next;
node->next = NULL; // 这里还是没搞太懂为什么要+2,后面有'\0',那只需要+1就行了,为什么
// 还要+2,没搞懂。
char buf[BUFSIZE+];
ssize_t amt;
ssize_t amt2 = len < BUFSIZE ? len : BUFSIZE;
amt = read(fd, buf, amt2);
if (amt != amt2) {
fprintf(stderr, "unable to read file: %s\n", argv[i]);
return ;
}
buf[amt2] = '\0'; // 字符串结尾
// 先转成unix文档
to_unix(buf);
if (ending == UNIX) {
strcpy(node->buf, buf);
} else {
// 这里BUFSIZE*2的主要原因应该是怕所有的都是换行符,这样转换出来
// 就是*2了,但是没搞懂为什么要+3,个人感觉最多有个+1就行了。
char buf2[(BUFSIZE*)+];
unix_to_dos(buf2, buf);
strcpy(node->buf, buf2);
}
len -= amt2;
} // 将文件长度修改为0,并重新从文件头开始
ftruncate(fd, );
lseek(fd, , SEEK_SET);
// 循环将链表中的内容写入文件,并释放链表中的内容
while (root) {
ssize_t amt2 = strlen(root->buf);
if (amt2 > ) {
ssize_t amt = write(fd, root->buf, amt2);
if (amt != amt2) {
fprintf(stderr, "unable to write file: %s\n", argv[i]);
return ;
}
}
node = root;
root = root->next;
free(node);
}
}
close(fd);
}
return ;
} // 这里相当于是字符的的不断的拷贝
void
to_unix(char* buf)
{
char* p = buf;
char* q = buf;
while (*p) {
if (p[] == '\r' && p[] == '\n') {
// dos
*q = '\n';
p += ;
q += ;
}
else if (p[] == '\r') {
// old mac
*q = '\n';
p += ;
q += ;
}
else {
*q = *p;
p += ;
q += ;
}
}
*q = '\0';
} // 这里和to_unix的动作正好相反
void
unix_to_dos(char* buf2, const char* buf)
{
const char* p = buf;
char* q = buf2;
while (*p) {
if (*p == '\n') {
q[] = '\r';
q[] = '\n';
q += ;
p += ;
} else {
*q = *p;
p += ;
q += ;
}
}
*q = '\0';
}
Android development tools line_endings hacking的更多相关文章
- ADT Android Development Tools
ADT(Android Development Tools)在Eclipse编译IDE环境中,需安装ADT(Android Developer Tools)Plug-in,这是Android在Ecli ...
- ADT-bundle(Android Development Tools)环境配置
Android开发环境有两套比较主流的:ADT-bundle和Android Studio,前者是Eclipse插件的形式进行开发,后者是Android的官方IDE. ADT环境的配置与调试:(1)安 ...
- Android Development Tools 发生checkAndLoadTargetData错误
之前使用时没有出现任何问题的,我把D:\IDE\ADT\adt-bundle-windows-x86_64-20140321\eclipse目录下面的 eclipse.exe重名名为adt.exe并设 ...
- 在eclipse里卸载已安装的插件[例如Android Development Tools ADT]
在eclipse里卸载已安装的插件 有四种方法: 1.到plugins和features目录中找到你要卸载的插件的文件夹, ...
- android 安装 出现Android Native Development Tools不能安装
Software being installed: Android Native Development Tools 20.0.0.v201206242043-391819 (com.android. ...
- Websites for more Android development information
There is a vibrant, helpful Android developer community on the Web. Here are a numberof useful websi ...
- android sdk tools 一览
ANDROID SDK ADKROID SDK的工具划分为两部分,一部分是SDK tools,与平台无关,另一部分是Platform tools支持最新的安卓平台 SDK tools有 SDK m ...
- Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:
Administrator@DESKTOP-EHCTIOR MINGW64 /d/react-native-eyepetizer (master) $ react-native run-android ...
- [Android]Eclipse 安装 ADT[Android Development Tooling] 失败的两种解决办法
原因 最近想在新装的 Win7 里搭建一下 Android 的开发环境,虽然现在有 Android Studio 了,不过还是习惯 Eclipse 一点.众所周知的原因,Eclipse 直接安装 AD ...
随机推荐
- IntelliJ IDEA 文件夹重命名--解决重命名后js文件引用找不到路径报404错误
情景: 说明:ExtJS是我后来的改的名字--原来叫extjs,可是当我把在页面的引用地址改为 src="ExtJS/.."后页面就报404错误,我把它改回之前的extjs就可以( ...
- Unity3D脚本中文系列教程(十)
http://dong2008hong.blog.163.com/blog/static/4696882720140312627682/?suggestedreading&wumii Unit ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- java基础知识回顾之java集合类-Properties集合
/** java.lang.Object |--java.util.Dictionary<K,V> |--java.util.Hashtable<Object,Obje ...
- POJ 3278Catch That Cow
http://poj.org/problem?id=3278 大意是说牛在原地不动,他在某点去抓牛,他有两种方式可以走,第一种走一步,往前往后都可,第二种是走现在所在点的两倍的数目.只要能够刚好到达牛 ...
- winserver2008,运行可执行文件,提示 激活上下文生成失败。 找不到从属程序集 Microsoft.VC90.DebugCRT,processorArchitecture="x86"
首先我运行的可执行文件是在win7下使用vs2008生成的release版本的文件,为什么在运行的时候提示需要DebugCRT? 另外我在winserver2008上是安装了vc2008的运行环境的. ...
- HTML5入门7---"session的会话缓存"和"localStorage的cookie"缓存数据
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python自省指南
深入python中对自省的定义: python的众多强大功能之一,自省,正如你所知道的,python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其他模块和函数,获取它们的信息,并对它们进行 ...
- [cocoapods] 如何卸载工程里的cocoapods
1. 打开自己的工程所在文件夹 删除这4个文件 2. 打开工程,把红线的东西都删除掉 3. 只留下原来的4个,其余都删除掉
- Servlet中如何实现页面转发
在Servlet中实现页面转发主要是利用RequestDispatcher接口实现的.此接口可以把一个请求转发到另一个JSP页面上. forward():把请求转发到服务器上的另一个资源. ...