1, 刪除宏定義:java
java 文件DeleteMacro.java的做用是:刪除文件中帶關鍵字的宏定義,並以空行代替.android
1)javac DeleteMacro.javajson
2)java DeleteMacro A.cpp A.new.cpp MTK //刪除文件A.cpp中帶關鍵字MTK的宏定義,並以空行代替bash
以後,輸出到A.new.cppapp
//package com.letv.jsontest;ide
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;this
//import com.letv.jsontest.DeleteMacro.LineRecord;.net
public class DeleteMacro {
/* 1, 讀入文件每一行
2, 解析記錄 #ifdef #else #endif ,併爲#ifdef 編號
3, 刪除對應的行.
4, 輸出新的文件.*/
/**
* @param args
*/
private static String MYINPUTFILENAME = "/home/liyl/OMXCodec.cpp";
private static String MYOUTPUTFILENAME = "/home/liyl/OMXCodec_ORG.cpp";
private static String KEY = "MTK";
private static String IFDEF="#if";
private static String ELSEDEF="#else";
private static String ENDIF="#endif";
protected final static class LineRecord {
String key;
int line;
public int getLine() {
return line;
}
public void setLine(int line) {
this.line = line;
}
int number;
boolean deleteFlag;
public boolean isDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(boolean deleteFlag) {
this.deleteFlag = deleteFlag;
}
public void setNumber(int number) {
this.number = number;
}日誌
public LineRecord(String key, int line, int number) {
super();
this.key = key;
this.line = line;
this.number = number;
this.deleteFlag = false;
}
@Override
public String toString() {
return "LineRecord [key=" + key + ", line=" + line + ", number="
+ number + ", deleteFlag=" + deleteFlag + "]";
}
}
/////////////////////////////////////query////////////////////////////////////////////////////////
private static LinkedHashMap<String,LineRecord> ifMap = new LinkedHashMap<String, LineRecord>();
private static LinkedHashMap<String,LineRecord> elseMap = new LinkedHashMap<String, LineRecord>();
private static LinkedHashMap<String,LineRecord> endMap = new LinkedHashMap<String, LineRecord>();
private static ArrayList<LineRecord> ifList = new ArrayList<LineRecord>();
private static ArrayList<LineRecord> endList = new ArrayList<LineRecord>();
private static ArrayList<LineRecord> elseList = new ArrayList<LineRecord>();
private static int myNumber = -1;
private static void myprint(LineRecord lineRecord) {
if(lineRecord != null){
System.out.println(lineRecord.toString());
}
}
private static void markMacroTags(String lineStr,int lineNum, String key) {code
LineRecord currentRecord = null;
int number = -1;
String keyStr = IFDEF;
if(key != null && !key.isEmpty()){
keyStr = IFDEF + " " + key;
}
if (lineStr.startsWith(IFDEF)) {
currentRecord = new LineRecord(lineStr,lineNum,number);
//myprint(currentRecord);
ifList.add( currentRecord);
}else if(lineStr.startsWith(ELSEDEF)) {
currentRecord = new LineRecord(lineStr,lineNum,number);
//myprint(currentRecord);
elseList.add( currentRecord);
}else if(lineStr.startsWith(ENDIF)) {
currentRecord = new LineRecord(lineStr,lineNum,number);
//myprint(currentRecord);
endList.add( currentRecord);
}else {
}
}
private static boolean endifMatchif() {
if(endList.isEmpty() || ifList.isEmpty()) {
return false;
}
LineRecord endLast = endList.remove(endList.size() - 1);
LineRecord ifLast = ifList.remove(ifList.size() - 1);
myNumber++;
ifLast.setNumber(myNumber);
endLast.setNumber(myNumber);
ifMap.put("" + myNumber, ifLast);
endMap.put("" + myNumber, endLast);
System.out.println("myNumber = " + myNumber);
return true;
}
private static boolean isMatch(int elseTag,int endTag ,int ifTag) {
System.out.println(" ifTag="+ifTag +" elseTag="+elseTag +" endTag="+ endTag );
return elseTag < endTag ? elseTag > ifTag ? true: false:false;
}
private static boolean elseMatchAndPutMap(LineRecord record) {
if(record == null || ifMap.isEmpty() || endMap.isEmpty()) {
return false;
}
int elseLine = record.line;
for(int i = 0;i <= myNumber; i++) {
int endLine = endMap.get(""+i).line;
int ifLine = ifMap.get(""+i).line;
if(isMatch(elseLine,endLine,ifLine)){
record.setNumber(i);
elseMap.put(""+i, record);
return true;
}
}
return false;
}
private static boolean matchElseList() {
if(elseList.isEmpty()) {
return false;
}
for(int i =0; i < elseList.size();i++) {
myprint(elseList.get(i));
elseMatchAndPutMap(elseList.get(i));
}
return true;
}
private static void printAll() {
for(int i = 0;i <= myNumber;i++) {
System.out.println("=============={");
myprint(ifMap.get(""+i));
myprint(elseMap.get(""+i));
myprint(endMap.get(""+i));
System.out.println("==============}");
System.out.println("\r\n");
}
}
private static void removeAll() {
myNumber = -1;
ifList.clear();
endList.clear();
elseList.clear();
// anchorPointList.clear();
lineOutput = null;
ifMap.clear();
endMap.clear();
elseMap.clear();
}
/////////////////////////////////////delete////////////////////////////////////////////////////////
// private static ArrayList<Integer> anchorPointList = new ArrayList<Integer>();
private static boolean[] lineOutput = new boolean[15000];
private static void initLineOutput (){
for(int i = 0;i<lineOutput.length;i++){
lineOutput[i] = false;
}
}
private static void printLineOutput (boolean printDelete){
for(int i = 0;i<lineOutput.length;i++){
if(lineOutput[i] == printDelete)
System.out.println("LINE is "+ i +" "+(lineOutput[i]?"delete":"save"));
}
}
private static boolean filteMacroTag(String key) {
if(key == null || ifMap.isEmpty() || endMap.isEmpty()) {
return false;
}
for(int i = 0;i <= myNumber; i++) {
String macroTag = ifMap.get(""+i).key;
if(macroTag.contains(key)){
ifMap.get(""+i).setDeleteFlag(true);
endMap.get(""+i).setDeleteFlag(true);
if(!elseMap.isEmpty() && elseMap.get(""+i) != null)
elseMap.get(""+i).setDeleteFlag(true);
}
}
return true;
}
private static boolean resetfilteMacroTag(String key) {
if(key == null || ifMap.isEmpty() || endMap.isEmpty()) {
return false;
}
for(int i = 0;i <= myNumber; i++) {
String macroTag = ifMap.get(""+i).key;
if(macroTag.contains(key)){
ifMap.get(""+i).setDeleteFlag(false);
endMap.get(""+i).setDeleteFlag(false);
if(!elseMap.isEmpty() && elseMap.get(""+i) != null)
elseMap.get(""+i).setDeleteFlag(false);
}
}
return true;
}
private static boolean computerAndMarkOutputLine(String key) {
if (key == null || ifMap.isEmpty() || endMap.isEmpty()) {
return false;
}
for (int i = 0; i <= myNumber; i++) {
String macroTag = ifMap.get("" + i).key;
if (macroTag.contains(key)) {
if (ifMap.get("" + i).key.contains("ifndef")) {
if (!elseMap.isEmpty() && elseMap.get("" + i) != null) {
ifMap.get("" + i).setDeleteFlag(true);
elseMap.get("" + i).setDeleteFlag(true);
endMap.get("" + i).setDeleteFlag(true);
int tmp = ifMap.get("" + i).getLine();
ifMap.get("" + i).setLine(elseMap.get("" + i).getLine());
elseMap.get("" + i).setLine(tmp);
}else {
//only has if and end;delete ifndef Macro
lineOutput[ifMap.get("" + i).getLine()] = true;
lineOutput[endMap.get("" + i).getLine()] = true;
}
} else {
ifMap.get("" + i).setDeleteFlag(true);
endMap.get("" + i).setDeleteFlag(true);
if (!elseMap.isEmpty() && elseMap.get("" + i) != null) {
elseMap.get("" + i).setDeleteFlag(true);
int tmp = endMap.get("" + i).getLine();
endMap.get("" + i).setLine(elseMap.get("" + i).getLine());
elseMap.get("" + i).setLine(tmp);
}
}
}
}
for (int i = 0; i <= myNumber; i++) {
if (ifMap.get("" + i).isDeleteFlag()) {
for(int j = ifMap.get("" + i).getLine();j <= endMap.get("" + i).getLine();j++ ) {
lineOutput[j] = true;
}
if(elseMap.get(""+i) != null){
lineOutput[elseMap.get(""+i).getLine()] = true;
}
}
}
return true;
}
private static void printAllDeleteMacro() {
for (int i = 0; i <= myNumber; i++) {
if (ifMap.get("" + i).isDeleteFlag()) {
System.out.println("=============={");
myprint(ifMap.get("" + i));
myprint(elseMap.get("" + i));
myprint(endMap.get("" + i));
System.out.println("==============}");
System.out.println("\r\n");
}
}
}
private static void deleteMacroContent(String macroTAG) {
String key = macroTAG;
filteMacroTag(key);
printAllDeleteMacro();
resetfilteMacroTag(key);
computerAndMarkOutputLine(key);
printLineOutput(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// read file content from file
int argc = args.length;
switch(argc){
case 0:
break;
case 3:
KEY = args[2];
case 2:
MYOUTPUTFILENAME = args[1];
case 1:
MYINPUTFILENAME = args[0];
break;
}
System.out.println("MYINPUTFILENAME ="+MYINPUTFILENAME+" MYOUTPUTFILENAME "+MYOUTPUTFILENAME+" KEY "+ KEY);
StringBuffer sb = new StringBuffer("");
int linenum = 0;
int outlinenum = 0;
try {
FileReader reader;
BufferedReader br;
reader = new FileReader(MYINPUTFILENAME);
br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
linenum++;
markMacroTags( str,linenum,null);
endifMatchif();
}
matchElseList();
//printAll();
deleteMacroContent(KEY);
reader.close();
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileReader readerInput;
FileWriter writerOutput;
BufferedReader brIn;
BufferedWriter brOut;
readerInput = new FileReader(MYINPUTFILENAME);
writerOutput = new FileWriter(MYOUTPUTFILENAME);
brIn = new BufferedReader(readerInput);
brOut = new BufferedWriter(writerOutput);
String strout = null;
while ((strout = brIn.readLine()) != null) {
outlinenum++;
if(lineOutput[outlinenum]) {
brOut.newLine();
continue;
}
brOut.write(strout+"\n");
}
removeAll();
brIn.close();
brOut.close();
readerInput.close();
writerOutput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
cat deletemacro.sh
#########################################################################
# File Name: deletemacro.sh
# Author: liyl
# mail: liyunlong_88@126.com
# Created Time: Wed 04 Jan 2017 03:50:52 PM CST
#########################################################################
#!/bin/bash
LOCAL_PATH=pwd
SUFFIX=".h"
tree -f -P *$SUFFIX $1 > tmp
sed -r -i /$SUFFIX$/!d tmp
awk -F " /" '{print "/"$2}' ./tmp > tmp2
mkdir -p org/
while read line; do echo $line;mkdir -p `dirname ./org$line` ; touch `pwd`/org$line; java DeleteMacro $line `pwd`/org$line MTK; done < tmp2
sh deletemacro.sh /home/liyl/source/media_codec1/fat_frog/jni/
例如:過濾/home/liyl/source/media_codec1/fat_frog/jni/ 目錄下全部頭文件中帶MTK的宏定義,並輸出到
./org
能夠用meld 比較 刪除Macro以後的文件和原始文件
2, 對帶關鍵字key1,key2的android 標準日誌中的每一行與下一行的時間差.
例如: 計算A.log 中的701和702 之間的時間差,單位是毫秒
1)javac TimeDiff.java
2)java TimeDiff A.log "701" "702"
/*************************************************************************
> File Name: TimeDiff.java
> Author: liyunlong
> Mail: liyunlong_88@126.com
> Created Time: Tue 29 Mar 2016 05:55:30 PM CST
************************************************************************/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DiffTime {
/**
* * @param args
* */
private static String MYFILENAME = "/home/liyl/aa.log";
private static String key1 = "701";
private static String key2 = "702";
// private static String regstr =
// "[0-9]{4}(\-|\/|\.)[0-9]{1,2}\1[0-9]{1,2}";
private static String extractDateFromLine(String line, String key) {
if (line.contains(key)) {
/*
* Pattern p = Pattern.compile(""); Matcher m = p.matcher(line);
*/
return line.split(" - ")[0];
}
return null;
}
private static long timeDiff(String time1, String time2) {
SimpleDateFormat format = new SimpleDateFormat(
"MM-dd HH:mm:ss.SSS");
long ret = 0l;
try {
if (time1 != null && time2 != null) {
// System.out.println(time1 +" - "+ time2);
/*
* String time_s1 = time1.split(",")[0]; String time_ms1 =
* time1.split(",")[1]; String time_s2 = time2.split(",")[0];
* String time_ms2 = time2.split(",")[1]; long time_before =
* format.parse(time_s1).getTime() + Integer.parseInt(time_ms1)
* ; long time_after = format.parse(time_s2).getTime() +
* Integer.parseInt(time_ms2) ;
*/
long time_before = format.parse(time1).getTime();
long time_after = format.parse(time2).getTime();
ret = time_after - time_before;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// read file content from file
int argc = args.length;
switch(argc){
case 0:
break;
case 3:
key2 = args[2];
case 2:
key1 = args[1];
case 1:
MYFILENAME = args[0];
break;
}
System.out.println("argc = "+argc+" MYFILENAME ="+MYFILENAME+" key1 "+key1+" key2 "+ key2);
StringBuffer sb = new StringBuffer("");
int linenum = 0;
int line_key1 = 0;
int line_key2 = 0;
String pre_key = "";
String cur_key = "";
try {
FileReader reader;
reader = new FileReader(MYFILENAME);
BufferedReader br = new BufferedReader(reader);
String str = null;
String time_701 = null;
String time_702 = null;
String tmp701 = null;
String tmp702 = null;
while ((str = br.readLine()) != null) {
linenum++;
tmp701 = extractDateFromLine(str, key1);
cur_key = null;
if (tmp701 != null) {
time_701 = tmp701.replaceFirst(",", ",");
line_key1++;
cur_key = key1;
}
tmp702 = extractDateFromLine(str, key2);
if (tmp702 != null) {
time_702 = tmp702.replaceFirst(",", ",");
line_key2++;
cur_key = key2;
}
String errMsg = pre_key.equals(cur_key)? "******************ERROR: not should be here*****************":"";
sb = sb.append("----------------------line ("+linenum+") = "+cur_key+ "---------------------MSG:"+errMsg+"\r\n");
pre_key = cur_key;
if (cur_key.equals(key2)) {
long diff = timeDiff(time_701, time_702);
sb = sb.append(" line_key1 = ").append(line_key1)
.append(" line_key2 = ").append(line_key2)
.append(" time_").append(key2).append("(")
.append(time_702).append(")").append(" - ")
.append(" time_").append(key1).append("(")
.append(time_701).append(")").append(" = ")
.append(diff).append(" ms\r\n");
}
}
System.out.println(sb.toString());
br.close();
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}