Showing posts with label ansi c. Show all posts
Showing posts with label ansi c. Show all posts

Friday, February 24, 2012

[ANSI C] How to extract file from TAR archive

Introduction

tarUnpackFile() ANSI C function extracts file from a .TAR archive.

Source code

https://github.com/vurdalakov/codeblog_examples/tree/master/ansi_c/tarunpack

Usage


#include "tarunpack.h"

...

    packedFileHandle = fopen(packedFileName, "rb");

    if (NULL == packedFileHandle) {
        printf("Error opening file '%s'\n", packedFileName);
        return 0;
    }

    unpackedFileHandle = fopen(unpackedFileName, "wb");

    if (NULL == unpackedFileHandle) {
        printf("Error opening file '%s'\n", unpackedFileName);
    }
    else
    {
        result = tarUnpackFile(packedFileHandle, unpackedFileName, unpackedFileHandle);

        switch (result) {
        case 0:
            printf("File '%s' unpacked successfully\n", unpackedFileName);
            break;
        case 2:
            printf("File '%s' not found in '%s'\n", unpackedFileName, packedFileName);
            break;
        default:
            printf("Error %d unpacking file\n", result);
            break;
        }

        fclose(unpackedFileHandle);
    }

    fclose(packedFileHandle);

Links

Thursday, January 5, 2012

[ANSI C] How to unpack .GZ archive with ZLIB library

Introduction

This ANSI C code uses ZLIB library to unpack .GZ archives.

It also extracts information from .GZ file header.

Source code

https://github.com/vurdalakov/codeblog_examples/tree/master/ansi_c/gzunpack

Usage

#include "gzunpack.h"

...

    // read .GZ file header

    FILE *packedFileHandle = fopen("test1.gz", "rb");

    GzFileHeader *gzFileHeader = gzUnpackHeader(packedFileHandle);

    printf("Header:\n");
    printf("compressionMethod = %d\n", gzFileHeader->compressionMethod);
    printf("originalFileName = '%s'\n", gzFileHeader->originalFileName);
    printf("fileComment = '%s'\n", gzFileHeader->fileComment);

    // unpack .GZ file

    fseek(packedFileHandle, 0, SEEK_SET); // rewind

    FILE *unpackedFileHandle = fopen(gzFileHeader->originalFileName, "wb");

    gzUnpackFile(packedFileHandle, unpackedFileHandle);

    // free resources

    fclose(unpackedFileHandle);
    fclose(packedFileHandle);

    gzFreeHeader(gzFileHeader);

Links

Wednesday, January 4, 2012

[ANSI C] How to convert single CRs and LFs to CRLFs

Introduction

crlf2crlf function written in ANSI C converts all single CR (\r or \x13) and LF (\n or \x10) characters to CRLF (\r\n or \x13\x10) characters.

char *crlf2crlf(const char *text);

Returned string is allocated with malloc() function, and it is the caller's responsibility to free it with free() function.

Source code

https://github.com/vurdalakov/codeblog_examples/tree/master/ansi_c/crlf2crlf

Code is completely covered with test cases.

Usage

void printText(const char *text)
{
    char *convertedText;

    convertedText = crlf2crlf(text);

    printf(convertedText);

    free(convertedText);
}

Thursday, December 29, 2011

64 bit integers in Jansson

Starting from version 2.0, Jansson supports 64 bit integers.

To enable 64 bit support, in jansson_config.h file set JSON_INTEGER_IS_LONG_LONG value to 1.

If you are using Visual C++ compiler, you will also need to define strtoll() function that is missing from Microsoft C runtime library.

#ifndef JANSSON_CONFIG_H
#define JANSSON_CONFIG_H

/* If your compiler supports the inline keyword in C, JSON_INLINE is
   defined to `inline', otherwise empty. In C++, the inline is always
   supported. */
#ifdef __cplusplus
#define JSON_INLINE inline
#else
#define JSON_INLINE
#endif

/* If your compiler supports the `long long` type,
   JSON_INTEGER_IS_LONG_LONG is defined to 1, otherwise to 0. */
#define JSON_INTEGER_IS_LONG_LONG 1

/* If locale.h and localeconv() are available, define to 1,
   otherwise to 0. */
#define JSON_HAVE_LOCALECONV 1

#ifdef _MSC_FULL_VER
#define snprintf _snprintf
#define strtoll _strtoi64
#endif

#endif

[Solution] strtoll() is not supported by Visual C++

Microsoft standard C library does not contain strtoll() function that converts a string to a 64 bit integer.

However, there is a _strtoi64() function that can be used instead:

#if defined(_MSC_VER)
#define strtoll _strtoi64
#endif