【收藏】代码片段收集(持续更新)

发表于2017-08-19    类别: 技术(65)    标签: 收藏(19)

看到有一个网站分享代码片段,想起自己以前就曾经有过这样的收集代码片段的设想,到用的时候随时复制粘贴^_^。就在这里收集吧


C++文件操作

#include <stdio.h>
#include <error.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>

int main()
{
    const char *filename = "test/testfile";
    FILE *fp = fopen(filename, "wb");
    if (!fp){
	    printf("Open file %s fail, error code %d, error message: %s\n",
			    filename, errno, strerror(errno));
	    return -1;
    }

    fwrite("hello world", 10, 1, fp);
    fclose(fp);
    return 0;
}

常用code

#include <stdio.h>
perror("message");
fflush(stdout);
printf("%ld\n", ftell(fp));
fseek(fp, 2, SEEK_SET);

#include <unstd.h>
int main(int argc, char *argv[])
{
    int ch;
    while((ch = getopt(argc,argv,"s:pj:"))!= -1)
    {
        //putchar(ch);
        //printf("\n");
        //fflush(stdout);
        switch(ch)
        {
            case 's':
                printf("option s:'%s'\n",optarg);
                /*handle code*/
                break;
            case 'p':
                /*handle code*/
                break;
            case 'j':
                printf("option s:'%s'\n",optarg);
                /*handle code*/
                break;
            default:
                printf("other option: %c\n", ch);
        }
    }
}

String8 tpath;
tpath = tpath.format("/data/");

camera memory monitor

#!/system/bin/sh

i=0
times=150
echo "camera memory usage" >/tmp/tmp.txt
while [ $i -le $times ]
do
echo "#"$i
adb shell cat /proc/meminfo |grep -w "MemFree\|Cached\|IonInUse" |tee -a /tmp/tmp.txt
adb shell dumpsys meminfo com.android.camera2 |grep -w "Pss\|TOTAL\|MEMINFO\|Applications" |tee -a /tmp/tmp.txt
adb shell dumpsys meminfo cameraserver |grep -w "Pss\|TOTAL\|MEMINFO\|Applications" |tee -a /tmp/tmp.txt
sleep 10s
let "i++"
done


capture script

#!/system/bin/sh

filename=`date +%Y%m%d-%H%M%S`
filename=capture_${filename}
date>/data/${filename}
i=0
while true
do
    i=$((i+1))
    input keyevent 27
    echo "capture $i" >> /data/${filename}
    sleep 6
done

Android Mutex

#include <utils/Mutex.h>

Mutex mTest;

void func()
{
    Mutex::Autolock l(mTest);
}

void another_func()
{
    status_t ret = mTest.tryLock();
    if(ret != OK){//func is still running
        ret = mTest.timedLock(2000000000/*2s*/);
    }//wait for 2s
    ...
    if(ret == OK){
        mTest.unlock()
    }
    ...
}

Android property

#include <cutils/log.h>
#include <cutils/properties.h>

    char path[PROPERTY_VALUE_MAX];
    property_get("test.cam.device", path, "/data/test");

    char value[PROPERTY_VALUE_MAX];
    property_get("xxxx.media.camera", value, "0");
    if (atoi(value)) {
        ALOGD("The value is %d", value);
    }

    if (property_get_bool("debug.test.cam", 0))
        ALOGE("Is true");

    ALOGI("cdb: %d %s %s", __LINE__, __func__, __FILE__);
    ALOGE("cdb: %d %s %s", __LINE__, __func__, __FILE__);
    ALOGW("cdb: %d %s %s", __LINE__, __func__, __FILE__);

Android.mk

include $(call all-subdir-makefiles)

######

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= cameratest.cpp
LOCAL_SHARED_LIBRARIES := \
	libcutils \
	libutils \
	libbinder \
	libui \
	libcamera_client \
	libcamera_metadata \
	libgui

LOCAL_C_INCLUDES += \
    $(TOP)/system/media/camera/include \
    
LOCAL_MODULE:= cameratest
include $(BUILD_EXECUTABLE)

######

$(warning Message_to_show $(YY))

######

include $(BUILD_SHARED_LIBRARY)

相关文章

   2023-12-31 【记录】浮点数存储方式
   2023-10-29 【记录】Windows下试用raid1
   2023-10-03 【记录】电脑网卡唤醒开机
   2023-08-12 【原创】以太网接口电路驱动调试总结
   2023-03-05 【记录】MyBB论坛安装与搬迁