linux trim 代码,[Linux C] linux C实现trim功能

Linux C中没有提供trim功能的API。

实际编程过程中,很多地方会使用到类似trim功能的地方。

比如: 从文件中读取一行数据,存到字符串中。

但从文件读取的数据的末尾会包含 “\r\n”(windows文件格式)或者“\n”(linux 文件格式)

或者读取的数据前面会有空格和\t

但往往我们所关心的数据并不包含这些\r或者\n的字符串。

下面是自己编写的一个trim函数。

实现如下功能:

1.  提供三种模式

1.1 只从字符串首开始检测并删除指定字符

1.2 只从字符串尾部开始检测并删除指定字符

1.3 同时从字符串首部和尾部检测并删除指定字符

2.  可以自己指定所要trim的字符,可以指定多个字符

相关Code:

1. 函数原型如下所示:

void trim(char *source_string, const char *trim_string, trim_direction_mode_t trim_mode) 其中:

参数 source_string是需要进行trim操作的原始字符串

参数 trim_string是指定的需要在原始字符串中trim的字符

参数 trim_mode是指定trim的模式

其中 trim_direction_mode_t是自己定义的trim的模式的枚举,code如下:

enum _trim_direction_mode

{

E_TRIM_MODE_NONE = 0x0,

E_TRIM_MODE_FRONT = 0x1,

E_TRIM_MODE_BACK = 0x2,

E_TRIM_MODE_ALL = 0x3,

};

typedef enum _trim_direction_mode trim_direction_mode_t;顾名思义,定义了4种模式:

E_TRIM_MODE_NONE

E_TRIM_MODE_FRONT

E_TRIM_MODE_BACK

E_TRIM_MODE_ALL

2. trim函数code如下:

static void trim(char *source_string, const char *trim_string, trim_direction_mode_t trim_mode)

{

int input_len = strlen(source_string);

int trim_string_len = strlen(trim_string);

char *input_value_ptr = source_string;

const char *trim_string_ptr = trim_string;

int escape_flag = FALSE;

int i = 0, j = 0;

assert(source_string != NULL);

assert(trim_string != NULL);

if((trim_mode & E_TRIM_MODE_FRONT) != 0)

{

/* delete the front character */

for (i = 0; i < input_len; i++)

{

/* cycle the trim string, if the character isn't one of trim string,

* escape the cycle, and move the string */

for (j = 0; j < trim_string_len; j++)

{

if (input_value_ptr[i] == trim_string_ptr[j])

{

break;

}

if(j == (trim_string_len - 1))

{

escape_flag = TRUE;

}

}

if(escape_flag)

{

break;

}

}

memmove(input_value_ptr, input_value_ptr+i,(input_len - i + 1));

}

if((trim_mode & E_TRIM_MODE_BACK) != 0)

{

input_len = strlen(input_value_ptr);

escape_flag = FALSE;

/* delete the end character */

for (i = input_len - 1; i >= 0; i--)

{

/* cycle the trim string, if the character isn't one of trim string,

* escape the cycle, and move the string */

for (j = 0; j < trim_string_len; j++)

{

if (input_value_ptr[i] == trim_string_ptr[j])

{

break;

}

if(j == (trim_string_len - 1))

{

escape_flag = TRUE;

}

}

if(escape_flag)

{

break;

}

}

input_value_ptr[i+1] = '\0';

}

return;

}函数作用就是按照trim mode,从trim_string中选取要trim的字符,对source_string进行trim操作。

其中,TRUE和FALSE是自己定义的boo变量

#ifndef BOOL

#define BOOL int

#define TRUE 1

#define FALSE 0

#endif

代码执行示例:

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

{

char string[100] = "abcdefghijklmn";

char tmp[100] = "";

strncpy(tmp, string, sizeof(tmp));

printf("source string is : %s\n", tmp);

trim(tmp, "abmn", E_TRIM_MODE_FRONT);

printf("trim front string is : %s\n", tmp);

strncpy(tmp, string, sizeof(tmp));

printf("source string is : %s\n", tmp);

trim(tmp, "abmn", E_TRIM_MODE_BACK);

printf("trim back string is : %s\n", tmp);

strncpy(tmp, string, sizeof(tmp));

printf("source string is : %s\n", tmp);

trim(tmp, "abmn", E_TRIM_MODE_ALL);

printf("trim front and back string is : %s\n", tmp);

return 0

}

运行结果如下:

source string is : abcdefghijklmn

trim front string is : cdefghijklmn

source string is : abcdefghijklmn

trim back string is : abcdefghijkl

source string is : abcdefghijklmn

trim front and back string is : cdefghijkl

转载请注明出处:http://blog.csdn.net/jibing57/article/details/7448692

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux的SSD TRIM是一种用于优化固态硬盘(SSD)性能和寿命的技术。TRIM是一项操作系统级别的指令,用于通知SSD哪些数据块不再被使用,可以被擦除和重写。这有助于提高SSD的写入性能,并减少因为重写未使用数据块而引起的性能下降。 要启用SSD TRIM功能,您需要进行以下步骤: 1. 确保您的SSD支持TRIM。大多数现代SSD都支持这个功能,但一些较旧或低端的SSD可能不支持。您可以查看SSD制造商的文档或技术规格来确认它是否支持TRIM。 2. 检查您的Linux发行版是否已启用TRIM。大多数现代Linux发行版默认情况下已启用TRIM。您可以运行以下命令来检查: ``` sudo systemctl status fstrim.timer ``` 如果输出显示"active"或"enabled",则表示TRIM已启用。 3. 确保您的文件系统支持TRIM。大多数常见的文件系统,如ext4和XFS,都支持TRIM。您可以通过检查`/etc/fstab`文件中的文件系统挂载选项来确认是否启用了TRIM。 例如,对于ext4文件系统,您应该看到类似于以下内容的挂载选项: ``` UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx / ext4 discard,noatime,errors=remount-ro 0 1 ``` 请注意,`discard`选项用于启用TRIM。 4. 手动运行TRIM命令。如果TRIM没有自动运行,您可以手动运行TRIM命令来清理未使用的数据块。使用以下命令: ``` sudo fstrim -av ``` 这将触发对所有已挂载文件系统的TRIM操作。 请谨慎使用TRIM命令,因为它会触发SSD上的擦除操作,可能会导致数据丢失。确保在运行TRIM之前备份重要数据。 希望这些信息对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值