/* * Ensure that the open file fd is at least size bytes long. * Expands the file as needed using append_zeros(). File locking * is used to ensure no data is overwritten. * Returns the actual size of the file on success, -1 on error. */ static int ensure_file_size(int fd, size_t size) { struct stat st; struct flock lockInfo; char *errMsg = NULL; int error = 0; if (fstat(WOShmem_fd, &st)) { errMsg = WA_errorDescription(WA_error()); WOLog(WO_ERR, "ensure_file_size(): initial fstat() failed: %s", errMsg); error = 1; } if (!error && st.st_size < size) { if (lock_file_section(WOShmem_fd, st.st_size, size - st.st_size, &lockInfo, 1)) { error = 1; } else { /* someone else may have just changed the size, so repeat the stat */ if (fstat(WOShmem_fd, &st)) { errMsg = WA_errorDescription(WA_error()); WOLog(WO_ERR, "ensure_file_size(): second fstat() failed: %s", errMsg); WA_freeErrorDescription(errMsg); error = 1; } if (!error && st.st_size < size) { st.st_size += append_zeros(WOShmem_fd, size - st.st_size); if (st.st_size < size) error = 1; } lockInfo.l_type = F_UNLCK; if (fcntl(WOShmem_fd, F_SETLK, &lockInfo) == -1) { errMsg = WA_errorDescription(WA_error()); WOLog(WO_ERR, "ensure_file_size(): failed to unlock: %s", errMsg); WA_freeErrorDescription(errMsg); error = 1; } } } return error ? -1 : st.st_size; }
static void set_format(GnmStyle *style, const psiconv_sheet_numberformat psi_numberformat) { /* 100 should be long enough, but to be really safe, use strncpy */ char fmt_string[100]; /* TODO: Dates and times are still wrong. What about localisation? */ strcpy(fmt_string,""); if (psi_numberformat->code == psiconv_numberformat_fixeddecimal) { strcpy(fmt_string,"0"); append_zeros(fmt_string, psi_numberformat->decimal); } else if (psi_numberformat->code == psiconv_numberformat_scientific) { strcpy(fmt_string,"0"); append_zeros(fmt_string, psi_numberformat->decimal); strcat (fmt_string, "E+00"); } else if (psi_numberformat->code == psiconv_numberformat_currency) { /* TODO: Determine currency symbol somehow */ strcpy(fmt_string,"$0"); append_zeros(fmt_string, psi_numberformat->decimal); } else if (psi_numberformat->code == psiconv_numberformat_percent) { strcpy(fmt_string,"0"); append_zeros(fmt_string, psi_numberformat->decimal); strcat (fmt_string, "%"); } else if (psi_numberformat->code == psiconv_numberformat_triads) { strcpy(fmt_string,"#,##0"); append_zeros(fmt_string, psi_numberformat->decimal); } else if (psi_numberformat->code == psiconv_numberformat_text) { strcpy(fmt_string,"@"); } else if (psi_numberformat->code == psiconv_numberformat_date_dmm) { strcpy(fmt_string,"d-mm"); } else if (psi_numberformat->code == psiconv_numberformat_date_mmd) { strcpy(fmt_string,"mm-d"); } else if (psi_numberformat->code == psiconv_numberformat_date_ddmmyy) { strcpy(fmt_string,"dd-mm-yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_mmddyy) { strcpy(fmt_string,"mm-dd-yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_yymmdd) { strcpy(fmt_string,"yy-mm-dd"); } else if (psi_numberformat->code == psiconv_numberformat_date_dmmm) { strcpy(fmt_string,"d mmm"); } else if (psi_numberformat->code == psiconv_numberformat_date_dmmmyy) { strcpy(fmt_string,"d mmm yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_ddmmmyy) { strcpy(fmt_string,"dd mmm yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_mmm) { strcpy(fmt_string,"mmm"); } else if (psi_numberformat->code == psiconv_numberformat_date_monthname) { strcpy(fmt_string,"mmmm"); } else if (psi_numberformat->code == psiconv_numberformat_date_mmmyy) { strcpy(fmt_string,"mmm yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_monthnameyy) { strcpy(fmt_string,"mmmm yy"); } else if (psi_numberformat->code == psiconv_numberformat_date_monthnamedyyyy) { strcpy(fmt_string,"mmmm d, yyyy"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_ddmmyyyyhhii) { strcpy(fmt_string,"dd-mm-yyyy h:mm AM/PM"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_ddmmyyyyHHii) { strcpy(fmt_string,"dd-mm-yyyy h:mm"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_mmddyyyyhhii) { strcpy(fmt_string,"mm-dd-yyyy h:mm AM/PM"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_mmddyyyyHHii) { strcpy(fmt_string,"mm-dd-yyyy h:mm"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_yyyymmddhhii) { strcpy(fmt_string,"yyyy-mm-dd h:mm AM/PM"); } else if (psi_numberformat->code == psiconv_numberformat_datetime_yyyymmddHHii) { strcpy(fmt_string,"yyyy-mm-dd h:mm"); } else if (psi_numberformat->code == psiconv_numberformat_time_hhii) { strcpy(fmt_string,"h:mm AM/PM"); } else if (psi_numberformat->code == psiconv_numberformat_time_hhiiss) { strcpy(fmt_string,"h:mm:ss AM/PM"); } else if (psi_numberformat->code == psiconv_numberformat_time_HHii) { strcpy(fmt_string,"h:mm"); } else if (psi_numberformat->code == psiconv_numberformat_time_HHiiss) { strcpy(fmt_string,"h:mm:ss"); } /* TODO: Add True/False */ if (fmt_string[0]) gnm_style_set_format_text (style, fmt_string); }