long extract_chm(const char *filename, const char *base_path) { struct chmFile *handle; struct extract_context ec; handle = chm_open(filename); if (handle == NULL) { fprintf(stderr, "Cannot open chmfile: %s", filename); return -1; } ec.base_path = base_path; if (!chm_enumerate(handle, CHM_ENUMERATE_NORMAL | CHM_ENUMERATE_SPECIAL, _extract_callback, (void *)&ec)) { fprintf(stderr, "Extract chmfile failed: %s", filename); } chm_close(handle); return 0; }
int main(int c, char **v) { struct chmFile *h; struct extract_context ec; if (c < 3) { fprintf(stderr, "usage: %s <chmfile> <outdir>\n", v[0]); exit(1); } h = chm_open(v[1]); if (h == NULL) { fprintf(stderr, "failed to open %s\n", v[1]); exit(1); } printf("%s:\n", v[1]); ec.base_path = v[2]; if (! chm_enumerate(h, CHM_ENUMERATE_ALL, _extract_callback, (void *)&ec)) printf(" *** ERROR ***\n"); chm_close(h); return 0; }
extern u32 fs_chm_to_menu(const char *chmfile, u32 icolor, u32 selicolor, u32 selrcolor, u32 selbcolor) { int fid; struct chmFile *chm; t_fs_chm_enum cenum; if (menu_renew(&g_menu) == NULL) { return 0; } fid = freq_enter_hotzone(); chm = chm_open(chmfile); if (chm == NULL) { freq_leave(fid); return 0; } add_parent_to_menu(g_menu, icolor, selicolor, selrcolor, selbcolor); cenum.icolor = icolor; cenum.selicolor = selicolor; cenum.selrcolor = selrcolor; cenum.selbcolor = selbcolor; chm_enumerate(chm, CHM_ENUMERATE_NORMAL | CHM_ENUMERATE_FILES, chmEnum, (void *) &cenum); chm_close(chm); freq_leave(fid); return g_menu->size; }
/* * Class: org_chm4j_ChmFile * Method: entries * Signature: (Ljava/lang/String;I)[Lorg/chm4j/ChmEntry; */ JNIEXPORT jobjectArray JNICALL Java_org_chm4j_ChmFile_entries (JNIEnv *env, jobject jobj, jstring jfilename, jstring jpath, jint flags) { // opens the file const char *filename = (*env)->GetStringUTFChars(env, jfilename, 0); struct chmFile* cFile = chm_open(filename); if(cFile == NULL) { J_ThrowException(env, "java/io/IOException", "failed to open the file"); return NULL; } (*env)->ReleaseStringUTFChars(env, jfilename, filename); // creates java list jobject list = J_CreateList(env); if(list == NULL) { // close the file chm_close(cFile); J_ThrowException(env, "java/io/IOException", "failed to create entries list"); return NULL; } // initializes context ENUM_CONTEXT* context = (ENUM_CONTEXT*) malloc(sizeof(ENUM_CONTEXT)); if(context == NULL) { // close the file chm_close(cFile); J_ThrowException(env, "java/io/IOException", "failed to create entries context"); return NULL; } context->env = env; context->file = jobj; context->list = list; // enumerates entries const char *path = (*env)->GetStringUTFChars(env, jpath, NULL); int enumres = chm_enumerate_dir(cFile, path, (int) flags, listEntries, context); (*env)->ReleaseStringUTFChars(env, jpath, path); free(context); // closes the file chm_close(cFile); if(enumres != 1) { J_ThrowException(env, "java/io/IOException", "failed to list entries"); return NULL; } // returns a java array jobjectArray array = J_ListToArray(env, list); return array; }
/* * Class: org_chm4j_ChmEntry * Method: readContent * Signature: (Ljava/lang/String;Ljava/lang/String;)[B */ JNIEXPORT jbyteArray JNICALL Java_org_chm4j_ChmEntry_readContent (JNIEnv *env, jobject jobj, jstring jfilename, jstring jpath) { // opens the file const char *filename = (*env)->GetStringUTFChars(env, jfilename, 0); struct chmFile* cFile = chm_open(filename); (*env)->ReleaseStringUTFChars(env, jfilename, filename); if(cFile == NULL) { J_ThrowException(env, "java/io/IOException", "failed to open the file"); return NULL; } // resolves entry struct chmUnitInfo cUnit; const char *path = (*env)->GetStringUTFChars(env, jpath, NULL); int res = chm_resolve_object(cFile, path, &cUnit); (*env)->ReleaseStringUTFChars(env, jpath, path); if(res != CHM_RESOLVE_SUCCESS) { // close the file chm_close(cFile); J_ThrowException(env, "java.io.IOException", "failed to resolve entry"); return NULL; } // retrieves entry content unsigned char* buf = (unsigned char*) malloc(sizeof(unsigned char) * cUnit.length); if(buf == NULL) { // close the file chm_close(cFile); J_ThrowException(env, "java.io.IOException", "failed to allocate buffer"); return NULL; } jlong nbRead = (jlong) chm_retrieve_object(cFile, &cUnit, buf, 0, cUnit.length); // close the file chm_close(cFile); // creates and fills java byte array jbyteArray data = (*env)->NewByteArray(env, nbRead); if(nbRead > 0) { (*env)->SetByteArrayRegion(env, data, 0, nbRead, buf); } free(buf); return data; }
static void chmfile_file_info(ChmFile *chmfile) { struct chmFile *cfd; cfd = chm_open(chmfile->filename); if (cfd == NULL) { g_error(_("Can not open chm file %s."), chmfile->filename); return; } chmfile_system_info(cfd, chmfile); chmfile_windows_info(cfd, chmfile); /* Convert book title to UTF-8 */ if (chmfile->title != NULL && chmfile->encoding != NULL) { gchar *title_utf8; title_utf8 = g_convert(chmfile->title, -1, "UTF-8", chmfile->encoding, NULL, NULL, NULL); g_free(chmfile->title); chmfile->title = title_utf8; } /* Convert filename to UTF-8 */ if (chmfile->hhc != NULL && chmfile->encoding != NULL) { gchar *filename_utf8; filename_utf8 = convert_filename_to_utf8(chmfile->hhc, chmfile->encoding); g_free(chmfile->hhc); chmfile->hhc = filename_utf8; } if (chmfile->hhk != NULL && chmfile->encoding != NULL) { gchar *filename_utf8; filename_utf8 = convert_filename_to_utf8(chmfile->hhk, chmfile->encoding); g_free(chmfile->hhk); chmfile->hhk = filename_utf8; } chm_close(cfd); }
bool ChmDoc::Load(const WCHAR *fileName) { chmHandle = chm_open((WCHAR *)fileName); if (!chmHandle) return false; ParseWindowsData(); if (!ParseSystemData()) return false; UINT fileCodepage = codepage; char header[24] = { 0 }; if (file::ReadN(fileName, header, sizeof(header))) { DWORD lcid = ByteReader(header, sizeof(header)).DWordLE(20); fileCodepage = LcidToCodepage(lcid); } if (!codepage) codepage = fileCodepage; // if file and #SYSTEM codepage disagree, prefer #SYSTEM's (unless it leads to wrong paths) FixPathCodepage(homePath, fileCodepage); FixPathCodepage(tocPath, fileCodepage); FixPathCodepage(indexPath, fileCodepage); if (GetACP() == codepage) codepage = CP_ACP; if (!HasData(homePath)) { const char *pathsToTest[] = { "/index.htm", "/index.html", "/default.htm", "/default.html" }; for (int i = 0; i < dimof(pathsToTest); i++) { if (HasData(pathsToTest[i])) { homePath.SetCopy(pathsToTest[i]); } } if (!HasData(homePath)) return false; } return true; }
int main (int argc, char *argv[]) { struct chmFile *handle; if (argc < 2) { fprintf(stderr, "Oops: nothing to do!\n"); exit(1); } handle = chm_open(argv[1]); if (NULL == handle) { fprintf( stderr, "Oops: something's wrong...\n"); exit(2); } do_something_useful_with(handle); chm_close(handle); return 0; }
bool ChmDoc::Load(const WCHAR *fileName) { chmHandle = chm_open((WCHAR *)fileName); if (!chmHandle) return false; ParseWindowsData(); if (!ParseSystemData()) return false; if (!HasData(homePath)) { const char *pathsToTest[] = { "/index.htm", "/index.html", "/default.htm", "/default.html" }; for (int i = 0; i < dimof(pathsToTest); i++) { if (HasData(pathsToTest[i])) homePath.Set(str::Dup(pathsToTest[i])); } } if (!HasData(homePath)) return false; if (!codepage) { char header[24]; if (file::ReadAll(fileName, header, sizeof(header))) { DWORD lcid = ByteReader(header, sizeof(header)).DWordLE(20); codepage = LcidToCodepage(lcid); } else codepage = CP_CHM_DEFAULT; } if (GetACP() == codepage) codepage = CP_ACP; return true; }
static gboolean extract_chm(const gchar *filename, ChmFile *chmfile) { struct chmFile *handle; struct extract_context ec; handle = chm_open(filename); if (handle == NULL) { g_message(_("cannot open chmfile: %s"), filename); return FALSE; } ec.base_path = (const char *)chmfile->dir; if (!chm_enumerate(handle, CHM_ENUMERATE_NORMAL, _extract_callback, (void *)&ec)) { g_message(_("Extract chmfile failed: %s"), filename); return FALSE; } chm_close(handle); return TRUE; }