c# 함수를 c로 변경해서 써야 하는데 어렵네요..;

yksnatas의 이미지

public static void HEX_FILE_READ(ref int isPass, string path
, ref List> dataList, string startPattern = "", string endPattern = "")
{
// 0. 변수 선언 및 초기화
List item = new List();
try
{

// 1. 파일 라인별 처리
foreach (string line in File.ReadLines(path))
{

// 2-1. 시작/종료 패턴이 하나라도 설정 안되어 있는경우 줄별로 신규 처리를 위해 item 생성
if (string.IsNullOrEmpty(startPattern) || string.IsNullOrEmpty(endPattern))
item = new List();
// 2-2. 시작/종료 패턴이 둘다 설정되어있고 시작패턴이 일치하는 경우 줄별로 신규 처리를 위해 item 생성
else if (line == startPattern)
item = new List();

// 3. 2글자씩 바이트로 변환
for (int i = 0; i < line.Length; i += 2)
{
var hex = line.Substring(i, 2);
int value = Convert.ToInt32(hex, 16);
item.Add((byte)value);
}

// 4-1. 시작/종료 패턴이 하나라도 설정 안되어 있는경우 줄별 item을 List에 추가
if (string.IsNullOrEmpty(startPattern) || string.IsNullOrEmpty(endPattern))
dataList.Add(item);
// 4-2. 시작/종료 패턴이 둘다 설정되어있고 종료패턴이 일치하는 경우 item을 List에 추가
else if (line == endPattern)
dataList.Add(item);
}
}
catch (Exception ex)
{
// 예외처리
Log4netHelper.Error(ex);
isPass = -1;
}
}

파일 내용을 라인별로 읽어와서 시작패턴과 엔드패턴을 검색해서 내용을 2글자씩 바이트로 변환 후
데이터리스트에 저장하는 함순데 C로 변환을 할려니 감이 안잡힙니다. 어떻게 해야 할까요?

익명 사용자의 이미지

1. C언어에는 내장 string 객체 같은 건 없습니다. ==로 두 문자열을 비교하거나 Length 필드를 사용하는 것 따위는 꿈도 꿀 수 없죠.

더군다나 C언어 기본 문자열 라이브러리(string.h)는 상당히 저수준인데다가, 잘못 사용하면 심각한 문제를 야기할 수 있는 함정들(strcpy 등)이 있어서 까다롭기 그지없습니다. 가변 길이 문자열을 이리저리 조작하며 다루어야 할 때 특히 그렇죠.

귀하의 코드 같은 경우에는 그래도 이 문제로 골치 썩일 일이 아주 많지는 않을 것 같은데, File.ReadLines 부분이 좀 귀찮겠네요. 입력받을 줄의 길이를 미리 알 방법이 없으니까요. 뭐 아무튼 레퍼런스에서 필요로 하는 함수들을 잘 찾아서 사용하면 어떻게든 될 겁니다:

https://en.cppreference.com/w/c/string

귀찮거나 불안할 때 도움이 될 만한 오픈소스 라이브러리도 있지요:

https://github.com/antirez/sds

2. C언어에는 List 같은 편리한 Generic collection도 없어요.

"Generic" 부분만이라면 재밌는 전처리기 테크닉을 이용해서 흉내낼 수 있을겁니다. 뭐 연결 리스트 같은 것도 C언어에서 포인터 써서 구현하면 되고요. 이미 잘 구현해 놓은 라이브러리도 찾아 보면 있을 겁니다.

문제는 C언어에선 Finalizer도 Garbage collector도 없다는 겁니다. 물론 Exception 메커니즘도 없죠. 동적으로 할당된 (메모리 등의) 자원이 더 이상 필요 없게 되었을 경우 반드시 해제가 되도록 작성하는 게 여간 까다로운 일이 아닙니다. 오랫동안 동작해야 하는 프로그램을 작성할 때 특히 중요한 문제입니다.

근데 뭐, 꼼꼼하게 잘 짜면 될 거에요.

yksnatas의 이미지

예상은 했지만 쉽지는 않을것 같네요. 라이브러리 이리저리 찾아봐야겠네요
단기간에 구현할려니 스트레스가 이만저만이 아닙니다..ㅠㅠ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.