ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : Code>C++>replace_with



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

  1. replase_with
  2. ¹®ÀÚ¿­³»ÀÇ ºÎºÐ¹®ÀÚ¿­À» ƯÁ¤¹®ÀÚ¿­·Î º¯È¯ÇÏ´Â C++ ÄÚµå ¿¹Á¦
  3. ¸Û
  4. Version 0.1
  5. 2004/01/28


¼³¸í

¹®ÀÚ¿­³»ÀÇ ºÎºÐ¹®ÀÚ¿­À» ƯÁ¤¹®ÀÚ¿­·Î º¯È¯ÇÏ´Â C++ ÄÚµå ¿¹Á¦

g++ ÄÄÆÄÀÏ Çϼ¼¿ä

»ç¿ë¹æ¹ý

html2text.c

½ÇÇà¹æ¹ý: ./html2text index.html index.txt

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "replace.h"

char *__replace(char *str);

int main(int argc, char *argv[])
{
        struct stat buf;
        char *source_string;
        char *target_string;
        int infd;
        int outfd;
        int len;

        if (argc != 3)
        {
                printf("Usage: html2text soruce target\n");
                return 1;
        }

        infd = open(argv[1], O_RDONLY);
        assert(infd >= 0);

        stat(argv[1], &buf);
        len = buf.st_size;

        if (len > 0)
        {
                source_string = (char *)malloc(sizeof(char) * len + 1); // +1Àº '\0'À» À§ÇÑ °ø°£
                read(infd, source_string, len);
                source_string[len] = '\0';

                target_string = __replace(source_string);

                outfd = open(argv[2], O_CREAT|O_WRONLY, 0x666);
                assert(outfd >= 0);

                write(outfd, target_string, strlen(target_string));
                close(outfd);

                free(source_string);
                free(target_string);
        }

        close(infd);
}

char* __replace(char* str)
{
        string rep_str = str;
        __replace_with(rep_str, "&lt;", "<");
        __replace_with(rep_str, "&gt;", ">");
        __replace_with(rep_str, "&amp;", "&");
        __replace_with(rep_str, "&quot;", "\"");
        __replace_with(rep_str, "&reg;", "");
        __replace_with(rep_str, "&copy;", "");
        __replace_with(rep_str, "&ensp;", "");
        __replace_with(rep_str, "&emsp;", "");
        __replace_with(rep_str, "&nbsp;", "");
        __replace_with(rep_str, "&endash;", "");
        __replace_with(rep_str, "&emdash;", "");
        __replace_with(rep_str, "  ", "");
        __replace_with(rep_str, "\n\n", "");
        __replace_with(rep_str, "\n\n", "");

        return strdup(rep_str.c_str());
}

ÄÚµå

replace.h

#ifndef __REPLACE_H_
#define __REPLACE_H_

#include <string>

#ifdef __cplusplus
extern "C"
{
#endif

static void __replace_with(string& org, string key, string target)
{
        int start;
        start = 0;
        while(start < org.size())
        {
                if((start = org.find(key, start)) == string::npos)
                        break;
                org.replace(start, key.size(), target);
        }
}

#ifdef __cplusplus
}
#endif

#endif

º¯°æ»çÇ×


2004/01/28

EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.