diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/compress_8c_source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/compress_8c_source.html Tue Mar 30 16:16:55 2010 +0100 @@ -0,0 +1,238 @@ + + +
+ +00001 /* +00002 * ============================================================================== +00003 * Name : Compress.c +00004 * Part of : OpenCLibz +00005 * Interface : +00006 * Description : +00007 * Version : +00008 * +00009 * Copyright (c) 2007 Nokia Corporation. +00010 * This material, including documentation and any related +00011 * computer programs, is protected by copyright controlled by +00012 * Nokia Corporation. +00013 * ============================================================================== +00014 */ +00015 +00016 +00017 +00026 #include "OpenCLibzheader.h" +00027 #include <limits.h> +00028 +00029 // +00030 // GzCompress +00031 // Compress the input file "in" and generates "out" file +00032 // +00033 void GzCompress(FILE *in,gzFile out) +00034 { +00035 static char buf[BUFLEN]; +00036 int len; +00037 +00038 +00039 #ifdef USE_MMAP +00040 /* Try first compressing with mmap. If mmap fails +00041 use the normal fread loop. +00042 */ +00043 if (GzCompressMmap(in, out) == Z_OK) return; +00044 #endif +00045 for (;;) +00046 { +00047 len = (int)fread(buf, 1, sizeof(buf), in); +00048 if (len < 0) +00049 Error("error reading the file \n"); +00050 +00051 if (len == 0) break; +00052 +00053 if (gzwrite(out, buf, (unsigned)len) != len) +00054 Error("error writing to the file \n");; +00055 +00056 } +00057 fclose(in); +00058 +00059 if (gzclose(out) != Z_OK) // libz api to close a compressed file +00060 Error("failed gzclose"); +00061 } +00062 +00063 +00064 +00065 +00066 +00067 +00068 #ifdef USE_MMAP +00069 +00070 // +00071 // GzCompressMmap +00072 // Try compressing the input file at once using mmap. Return Z_OK +00073 // If success, otherwise it returns Z_ERRNO . +00074 // +00075 int GzCompressMmap(FILE * in,gzFile out) +00076 { +00077 int len; +00078 int err; +00079 int ifd = fileno(in); +00080 caddr_t buf; /* mmap'ed buffer for the entire input file */ +00081 off_t buf_len; /* length of the input file */ +00082 struct stat sb; +00083 +00084 /* Determine the size of the file, needed for mmap: */ +00085 +00086 if (fstat(ifd, &sb) < 0) +00087 return Z_ERRNO; +00088 +00089 buf_len = sb.st_size; +00090 +00091 if (buf_len <= 0) +00092 return Z_ERRNO; +00093 +00094 /* Now do the actual mmap: */ +00095 +00096 buf =(char*) mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); +00097 +00098 //mmap is an openc api +00099 +00100 if (buf == (caddr_t)(-1)) +00101 return Z_ERRNO; +00102 +00103 /* Compress the whole file at once: */ +00104 len = gzwrite(out, (char *)buf, (unsigned)buf_len); +00105 +00106 if (len != (int)buf_len) +00107 Error(gzerror(out, &err)); +00108 +00109 munmap(buf, buf_len); +00110 fclose(in); +00111 if (gzclose(out) != Z_OK) // libz api to close a compressed file +00112 Error("failed gzclose"); +00113 +00114 return Z_OK; +00115 } +00116 #endif /* USE_MMAP */ +00117 +00118 +00119 +00120 +00121 +00122 // +00123 // ----------------------------------------------------------------------------- +00124 // FileCompress +00125 // Compresses the file using mode value +00126 // ----------------------------------------------------------------------------- +00127 // +00128 void FileCompress(char * file,char * mode) +00129 { +00130 static char outfile[MAX_NAME_LEN]; +00131 FILE *in; +00132 gzFile out; +00133 char choice[3]; +00134 +00135 strcpy(outfile, file); +00136 strcat(outfile, GZ_SUFFIX); +00137 +00138 in = fopen(file, "rb"); +00139 if (in == NULL) +00140 { +00141 Error("file not found ...try again \n"); +00142 } +00143 out = gzopen(outfile, mode); +00144 if (out == NULL) +00145 { +00146 Error("error opening outfile\n "); +00147 } +00148 GzCompress(in, out); +00149 +00150 +00151 printf("do you want to delete the original file (y /n) \n"); +00152 +00153 scanf("%s",choice); +00154 /*I am using scanf("%s",...) because of some problem in getchar() as +00155 well as scanf("%c"...) . I have reported the bug */ +00156 +00157 if(choice[0] == 'y') +00158 unlink(file); +00159 +00160 printf(" Congrats .... compression done ...u can find .gz file in the same directory as of the source file\n"); +00161 } +00162 +00163 // +00164 // ----------------------------------------------------------------------------- +00165 // StringCompress +00166 // Compresses the string +00167 // This function compresses the string entered by the user and shows the +00168 // compressed string and then decompressess it +00169 // Actually This function will show the content on the screen and demonstarte +00170 // how strings can be compressed +00171 // ----------------------------------------------------------------------------- +00172 // +00173 +00174 void StringCompress() +00175 { +00176 Byte *compr, *uncompr; +00177 uLong len,comprLen,uncomprLen; +00178 char *hello; +00179 +00180 hello = (char *)malloc(100*sizeof(char)); +00181 +00182 printf("enter the string(maximum 100 characters) to compress....\n"); +00183 +00184 +00185 +00186 scanf("%s",hello); //accepts the string from the user +00187 len = (uLong)strlen(hello)+1; +00188 comprLen = 10 * len; +00189 uncomprLen = comprLen; +00190 +00191 //memory allocation +00192 compr = (Byte*)calloc((uInt)comprLen, 1); +00193 uncompr = (Byte*)calloc((uInt)uncomprLen, 1); +00194 +00195 +00196 +00197 //compress is one of the api of libz library used to compress data +00198 +00199 compress(compr, &comprLen, (const Bytef*)hello, len); +00200 +00201 strcpy((char*)uncompr, "garbage"); //initialization +00202 +00203 printf("compressed string is : %s\n\n", (char *)compr); +00204 +00205 //uncompress is one of the api of libz library used to uncompress data +00206 +00207 uncompress(uncompr, &uncomprLen, compr, comprLen); +00208 +00209 +00210 printf("press enter to uncompress it\n"); +00211 +00212 getchar(); +00213 getchar(); +00214 +00215 if (strcmp((char*)uncompr, hello)) +00216 { +00217 Error("bad uncompress\n"); +00218 free(compr); +00219 free(uncompr); +00220 } +00221 else +00222 { +00223 printf("uncompressed string is : %s\n", (char *)uncompr); +00224 free(compr); +00225 free(uncompr); +00226 } +00227 +00228 } +00229 +00230 /* End of File */ +