Wednesday, October 28, 2020

git command

 git stash

git stash 將修改的部分丟進暫存

git stash list 列出暫存的資料

git stash pop 取出暫存

git stash drop 刪除暫存

git stash clear 刪除所有暫存

git commit

新增到 Local repository

git commit path1/file1 path2/ m "Message"

git commit --amend ( 修改變最後一次 commit log)

git checkout

切換/建立/還原 file

git checkout branch_B (切換 至 branch_B)

git checkout -b New_branch (從本地當下, 建立 new_branch, 並切換至 New_branch)

git checkout path/file (還原一個修改的檔案)

git checkout path1/file1 path2/file2.. (還原多個修改過的檔案)

git checkout HEAD (所有檔案回復到 local 最後 commit 的版本)

git checkout commitID (所有檔案回復到 commitID 版本)

git branch

git branch (列出 local 端的所有 branch)

git branch -r (列出 remote 端的所有 branch)

git branch -d branch_name ( 刪除 local branch)

git reset

還原至某版本的 commit 狀態

git reset --soft HEAD^ (取消前一次 commit 紀錄 , 但保留修改檔案)

git reset --hard HEAD^ (取消前一次 commit 紀錄 , 修改的檔案會消失)

git reset --soft commitID (回復記錄到 commitID 版本 , 但保留修改的檔案)

git reset --hard commitID (回復記錄到 commitID 版本 , 所有檔案也回復到該版本)

git reset --hard origin/branch_name ( 回復 branch 內所有修改內容)

git tag

建立標記, 適合用 於 Released FW 版本記錄

git tag -l (列出所有 tags)

git checkout <tag_name>

git tag -a v1.4 m "alpha1" (建立 tag , a: tag 名稱 ; m: tag 說明)

git push origin --tags (把 Local repository 建立好的所有 tags,上傳到 Remote repository ))

git ls

git ls-files --stage

100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0 .gitignore

100755 0ac339497485f7cc80d988561807906b2fd56172 0 my_executable_script.sh

產生最新 4 筆 patch 檔

git format-patch -4


找某一年修改

git blame index.page.js | grep  "2021"

Thursday, October 22, 2020

Android Selinux

 Android SELinux avc dennied权限问题解决方法

解决avc-denied之设置SELinux策略

Example

audit2allow Tool

core/init/Android.mk

core/init/selinux.cpp


recovery -> dd command

mboot

  ac androidboot.selinux permissive

  saveenv

  recovery

  reset

dd if=/dev/block/mmcblk0p31 of=/mnt/usb/sda1/userdata.img

Monday, October 12, 2020

架構遇到問題

 code 寫死就會造成 延展性不佳

        case KeyEvent.DOM_VK_CC:

        case KeyEvent.DOM_VK_SUBTITLE:

            var subObj = this.curData.value[this.selected];

            if (this.subtitle_cc_show == false && this.selected == 0 && (typeof(subObj) == 'object' && (Array.isArray(subObj.value) || subObj.hasOwnProperty('showMode')))) {

                this.curData.focusIndex = this.selected;

                this._list_submenu_init(subObj);

                this.subtitle_cc_show = true;

                return false;

Sunday, October 11, 2020

function pointer的應用[三]: 使用 typdef 來定義函數指標以增加程式的可讀性

原來

=========================================================================

#include <stdio.h>

int add(int a, int b)

{

 return a + b;

}

int minus(int a, int b)

{

 return a - b;

}

int multiply(int a, int b)

{

 return a * b;

}

int divide(int a, int b)

{

 return a / b;

}

int operation(int a, int b, int (*operation)(int, int))

{

 return operation(a, b);

}

int main(int argc, char* argv[])

{

 int val;

 // add

 // val = add(4, 2);

 val = operation(4, 2, add);

 printf("val = %d\n", val);

 // minus

 // val = minus(4, 2);

 val = operation(4, 2, minus);

 printf("val = %d\n", val);

 // multiply

 // val = multiply(4, 2);

 val = operation(4, 2, multiply);

 printf("val = %d\n", val);

 // divide

 // val = divide(4, 2);

 val = operation(4, 2, divide);

 printf("val = %d\n", val);

return 0;

}

=========================================================================

使用 typedef

#include <stdio.h>

typedef int (*calculation)(int, int);

int add(int a, int b)

{

 return a + b;

}

int minus(int a, int b)

{

 return a - b;

}

int multiply(int a, int b)

{

 return a * b;

}

int divide(int a, int b)

{

 return a / b;

}

// int operation(int a, int b, int (*operation)(int, int))

// {

//  return operation(a, b);

// }

int operation(int a, int b, calculation calc)

{

 return calc(a, b);

}

int main(int argc, char* argv[])

{

 int val;

 // add

 // val = add(4, 2);

 val = operation(4, 2, add);

 printf("val = %d\n", val);

 // minus

 // val = minus(4, 2);

 val = operation(4, 2, minus);

 printf("val = %d\n", val);

 // multiply

 // val = multiply(4, 2);

 val = operation(4, 2, multiply);

 printf("val = %d\n", val);

 // divide

 // val = divide(4, 2);

 val = operation(4, 2, divide);

 printf("val = %d\n", val);

return 0;

}

這裡我們可以清楚的看到,相較於使用函數指標的方式相告,我們用typedef過後的函數標宣告,不僅整個程式碼看起來更簡潔,整體其實也更為直觀

// use function pointer to declare

int operation(int a, int b, int (*operation)(int, int))

{

 return operation(a, b);

}

// use calculation to declare

int operation(int a, int b, calculation calc)

{

 return calc(a, b);

}

函式指標的使用,以及使用函式指標的好處

函式指標是指向函式的指標變數。

程式在編譯時,每一個函式都有一個入口地址,該入口地址就是函式指標所指向的地址。

1. 函式指標的宣告

bool (*pf)(int, int);

//pf指向一個函式,該函式的引數是兩個int,返回值是 bool 型別

2. 函式指標的初始化

//例如有這樣一個函式:bool cmp(int a, int b);

pf = cmp//注意 cmp 型態返回值應與宣告的的函式指標的型別匹配

3. 呼叫函式指標

bool b = pf(3,5);

等價於直接呼叫:bool b = cmp(3,5);

函式指標有兩個方面的應用:

1. 把指標函式當作形參傳遞給某些具有一定通用功能的模組。並封裝成介面來提高程式碼的靈活性和後期維護的便捷性

example:

void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*));

1. 待排序陣列首地址
2. 陣列中待排序元素數量
3. 各元素的佔用空間大小
4.  指向函式的指標,用於確定排序的規則 (可提供不同的排序法)

歸納為:便於分層設計、利於系統抽象、降低耦合度以及使介面與實現分開。

這裡通過比較一個比較 low 的氣泡排序來說明一下 qsort 內部函式指標的實現機制:

#include<iostream>
using namespace std;
void sort(int arr[], int size, bool(*cmp)(int,int));
bool up(int a, int b);
bool down(int a, int b);
int main()
{
    int arr[10];
    for (int i = 0;i < 10;++i)
        cin >> arr[i];

    sort(arr, 10,down);

    for (int i = 0;i < 10;++i)
        cout << arr[i] << " ";
    return 0;
}
void sort(int arr[],int size,bool(*cmp)(int,int))//簡單氣泡排序
{
    int temp;
    for (int i = 0;i < size-1; ++i)
    {
        for (int j = i;j < size-1;++j)
        {
            if (cmp(arr[i], arr[j + 1]))
            {
                temp = arr[i];
                arr[i] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
bool up(int a, int b)
{
    if (a > b)
        return 1;
    else
        return 0;
}
bool down(int a, int b)
{
    if (a > b)
        return 0;
    else
        return 1;
}
2. 有些地方必須使用函式函式指標才能完成給定的任務,如linux系統中的非同步訊號中斷處理,當發生某一觸發訊號時,需要呼叫相應的處理函式,此時需要使用函式指標來實現。

void (*signal(int signum,void(* handler)(int)))(int); 

引數一為訊號條件,第二個引數為一個函式指標,它所指向的函式需要一個整型引數,無返回值。
該函式的返回值也是一個函式指標,返回的指標所指向的函式有一個整型引數(一般不用)

if-else條件改成以函式指標陣列(array of function pointer)的方式改寫

#include "stdio.h"

#include "stdlib.h"

int plus(int a, int b) { return a + b; }
int minus(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divided(int a, int b) { return a / b; }
int main()
{
 int a, c;
 char b;
 printf("key a Function \nEX: 1 + 1\n");
 scanf("%i %c %i", &a, &b, &c);

 if (b == '+')
  printf("%d %c %d = %d\n", a, b, c, plus(a, c));
 else if (b == '-')
  printf("%d %c %d = %d\n", a, b, c, minus(a, c));
 else if (b == '*')
  printf("%d %c %d = %d\n", a, b, c, multiply(a, c));
 else if (b == '/')
  printf("%d %c %d = %d\n", a, b, c, divided(a, c));
}

 =========================================================================

#include"stdio.h"

int plus(int a, int b){ return a+b; }
int minus(int a, int b){ return a-b; }
int multiply(int a, int b){ return a*b; }
int divided(int a, int b){ return a/b; }

int main(){
    int a, c;
    char b;
    scanf("%d %c %d", &a, &b, &c);
    
    int (*cmd[])(int a, int c) = {multiply, plus, NULL, minus, NULL, divided};

    printf("%d\n",cmd[b-42](a,c));  // '*' ASCII = 42(0x2a)  ,'+' ASCII = 43 (0x2b), '-' ASCII = 45, '/' ASCII = 47    

    return 0;
}

Thursday, October 8, 2020

repo index

error: Your local changes to the following files would be overwritten by checkout:
    Android.mk
    extendedcommands.c
    flashutils/Android.mk
    flashutils/flashutils.c
    flashutils/flashutils.h
    mounts.c
    mounts.h
    nandroid.c
    roots.c
Please, commit your changes or stash them before you can switch branches.
Aborting
error: Your local changes to the following files would be overwritten by checkout:
    encore.mk
    init.encore.rc
Please, commit your changes or stash them before you can switch branches.
Aborting
<few more errors like this.........>
error: bootable/recovery/: CyanogenMod/android_bootable_recovery checkout 50822991460cbee65757e9de12b29e39238d6386 
error: device/bn/encore/: CyanogenMod/android_device_bn_encore checkout f6586ab41f0e3f5acfa16b43f9b17008e9bb0524 


解法:丢弃修改

repo forall -c git reset --hard HEAD


How switch to special tag?

repo forall -c "git checkout <special tag>"


repo sync -c -n -j4 && repo sync -c -l -j 16 

前面是抓code, 後面是長code


repo 打 tag

repo forall -c 'git tag spectralink_sprint_8'

repo forall -c 'git push origin --tags'

切换到另一个分支

repo forall -c git checkout your_branch

删除分支

repo forall -c git branch -D 分支名称


[git]repo、branch和tag取名建議和注意事項 - 怎麼取名比較不會有問題

Tuesday, October 6, 2020

Android Logcat

 Android studio 过滤log中指定字符(不显示包含指定字符的log)

^(?!.*(你要过滤掉的字符)).*$

*log中包含got 或Activity 的log将不显示在控制台。

^(?!.*(got|Activity)).*$


只显示指定字符的log

^(.*(10:30|10:33)).*$

Logcat


採用grep正則表示式過濾

adb logcat | grep -iE "Audio3DSettings|setParameters|setAudio3DState"


在同時輸出到螢幕和檔案 tee

adb logcat | grep -E '^[VDE]/(TAG1|TAG2)' | tee my.log


可以看出 tag 是一行開頭的第三個字元開始

adb logcat | grep "^..MyApp"


僅顯示 Error 級別 tag 為 MyApp 的輸出

adb logcat | grep "^E.MyApp"


要匹配 tag 為 MyApp 和 MyActivity 的輸出

adb logcat | grep "^..MyApp\|^..MyActivity"

adb logcat | grep -E "^..MyApp|^..MyActivity"  #使用 egrep 無須轉義符


過濾 tag 為 MyApp 和 MyActivity 的輸出

adb logcat | grep -v "^..MyApp\|^..MyActivity" 

adb logcat | grep -vE "^..MyApp|^..MyActivity"  #使用 egrep 無須轉義符

logcat | grep -vE "BluetoothAdapter|chatty| BluetoothManagerService| *bluetooth* | *Bluetooth*|bluetoot|bt_*"

顯示同一個程序的所有輸出

#!/bin/bash

packageName=$1 

pid=`adb shell ps | grep $packageName | awk ‘{print $2}’` 

adb logcat | grep –color=auto $pid


從當前開始顯示

adb logcat -c && adb logcat

logcat | grep -e Controller -e StateDvrPlayback

grep 指令使用 or 及 and 查兩個條件以上

Saturday, October 3, 2020

c 考古題

1.  如果想描繪下圖之鑽石形狀

     *     
    ***    
   *****   
  *******  
 ********* 
***********
 ********* 
  *******  
   *****   
    ***    
     *     

請參考以下程式碼

#include <stdio.h>

int main(int argc, const char * argv[])
{
    for(int i = -5; i <= 5; i++)
    {
        for(int j = -5; j <= 5; j++)
        {
            if(i <= j + 5 && i <= -j + 5 && i >= -j - 5 && i >= j - 5)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}

2. 如果想描繪下圖之三角型, 星號由一到十遞增

         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

請參考以下程式碼

1. 第一個迴圈代表執行十次

2. 第二個內迴圈也執行十次, 但裡頭多加一個判斷式

    因這三角形星號是由右邊往左列印, 因此下了 j <= 10 - i

    因此i == 1時印出九個空格, 條件不滿足時才列印出星號

3. 跳出內迴圈後印出換行符號    

#include <stdio.h>

int main(int argc, const char * argv[])
{
    for(int i = 1; i <= 10; i++)
    {
        for(int j = 1; j <= 10; j++)
        {
            if(j <= 10 - i)
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }
}

3. 若想描繪下圖之三角型, 由上而下為一顆, 兩顆星號, 以此類推到十顆

*
**
***
****
*****
******
*******
********
*********
**********

請參考下列程式碼:

1. 第一個for迴圈代表會執行十次

2. 第二個for迴圈代表會印出星號, 印出的次數會以變數i為準

3. 若j <= i條件不成立, 則內迴圈執行結束並印出換行符號

#include <stdio.h>

int main(int argc, const char * argv[])
{
    for(int i = 1; i <= 10; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
}

 

4. 

Thursday, October 1, 2020

變數互換|四種 swap

內容目錄

Simple Swap

Swap by Pointer

Swap by XOR (Bitwise Operation)

Swap by Macro

Simple Swap

1
2
3
4
5
void swap(int a, int b){
    int tmp = a;
    a = b;
    b = tmp;
}

Swap by Pointer

1
2
3
4
5
void swap(int *a, int *b){
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

Swap by XOR (Bitwise Operation)

XOR 可以做為最基本的加密方法,因為他有這個特性:做兩次一樣的運算等於沒做。

所以 A xor B 經過傳輸以後,解密 ( A xor B ) xor B = A;

知道這個特性就可以用 xor 做交換了

A xor B xor A = B
B xor B xor A = A

1
2
3
4
5
6
7
void XorSwap( int* x, int* y ) {
    if (x != y){
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
    }
}

Swap by Macro

1
2
3
4
#define SWAP(x, y) \
    int tmp = x;\
    x = y;\
    y = tmp;

以及和第一個方法很樣的:
Horowitz的Fundamentals of Data Structures in C一書中使用的。

1
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能