Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/text.c
| Show First 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | static void text_free_data(ID *id) | ||||
| MEM_SAFE_FREE(text->filepath); | MEM_SAFE_FREE(text->filepath); | ||||
| #ifdef WITH_PYTHON | #ifdef WITH_PYTHON | ||||
| BPY_text_free_code(text); | BPY_text_free_code(text); | ||||
| #endif | #endif | ||||
| } | } | ||||
| static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address) | static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address) | ||||
| { | { | ||||
| Text *text = (Text *)id; | Text *text = (Text *)id; | ||||
mont29: you can even revert those two blocks, and create/assign the `Text` pointer after you checked ID… | |||||
| /* Note: we are clearing local temp data here, *not* the flag in the actual 'real' ID. */ | /* Note: we are clearing local temp data here, *not* the flag in the actual 'real' ID. */ | ||||
| if ((text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) { | if ((text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) { | ||||
| text->flags &= ~TXT_ISEXT; | text->flags &= ~TXT_ISEXT; | ||||
| } | } | ||||
| /* Clean up, important in undo case to reduce false detection of changed datablocks. */ | /* Clean up, important in undo case to reduce false detection of changed datablocks. */ | ||||
| text->compiled = NULL; | text->compiled = NULL; | ||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | LISTBASE_FOREACH (TextLine *, ln, &text->lines) { | ||||
| if (ln->len != (int)strlen(ln->line)) { | if (ln->len != (int)strlen(ln->line)) { | ||||
| printf("Error loading text, line lengths differ\n"); | printf("Error loading text, line lengths differ\n"); | ||||
| ln->len = strlen(ln->line); | ln->len = strlen(ln->line); | ||||
| } | } | ||||
| } | } | ||||
| text->flags = (text->flags) & ~TXT_ISEXT; | text->flags = (text->flags) & ~TXT_ISEXT; | ||||
| id_us_ensure_real(&text->id); | |||||
| } | } | ||||
| IDTypeInfo IDType_ID_TXT = { | IDTypeInfo IDType_ID_TXT = { | ||||
| .id_code = ID_TXT, | .id_code = ID_TXT, | ||||
| .id_filter = FILTER_ID_TXT, | .id_filter = FILTER_ID_TXT, | ||||
| .main_listbase_index = INDEX_ID_TXT, | .main_listbase_index = INDEX_ID_TXT, | ||||
| .struct_size = sizeof(Text), | .struct_size = sizeof(Text), | ||||
| .name = "Text", | .name = "Text", | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | void BKE_text_free_lines(Text *text) | ||||
| text->curl = text->sell = NULL; | text->curl = text->sell = NULL; | ||||
| } | } | ||||
| Text *BKE_text_add(Main *bmain, const char *name) | Text *BKE_text_add(Main *bmain, const char *name) | ||||
| { | { | ||||
| Text *ta; | Text *ta; | ||||
| ta = BKE_id_new(bmain, ID_TXT, name); | ta = BKE_id_new(bmain, ID_TXT, name); | ||||
| /* Texts always have 'real' user (see also read code). */ | /* Texts have no users by default... Set the fake user flag to ensure that this text block | ||||
| id_us_ensure_real(&ta->id); | * doesn't get deleted when cleaning up data blocks. | ||||
Done Inline Actionsdeleted by default mont29: deleted by default | |||||
| * The user can toggle this flag off if they want this to be automatically cleaned up. */ | |||||
Done Inline ActionsThis last sentence is closer to user manual, not needed in code comment (as not related to this piece of code at all). mont29: This last sentence is closer to user manual, not needed in code comment (as not related to this… | |||||
| id_us_min(&ta->id); | |||||
| id_fake_user_set(&ta->id); | |||||
| return ta; | return ta; | ||||
| } | } | ||||
| /* this function replaces extended ascii characters */ | /* this function replaces extended ascii characters */ | ||||
| /* to a valid utf-8 sequences */ | /* to a valid utf-8 sequences */ | ||||
| int txt_extended_ascii_as_utf8(char **str) | int txt_extended_ascii_as_utf8(char **str) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /** | /** | ||||
| * Load a text file. | * Load a text file. | ||||
| * | * | ||||
| * \param is_internal: If \a true, this text data-block only exists in memory, | * \param is_internal: If \a true, this text data-block only exists in memory, | ||||
| * not as a file on disk. | * not as a file on disk. | ||||
| * | * | ||||
| * \note text data-blocks have no user by default, only the 'real user' flag. | * \note text data-blocks have no user by default. | ||||
Done Inline Actions...`have no real user but have 'fake user' enabled by default' mont29: ...`have no real user but have 'fake user' enabled by default' | |||||
| */ | */ | ||||
| Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal) | Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal) | ||||
| { | { | ||||
| unsigned char *buffer; | unsigned char *buffer; | ||||
| size_t buffer_len; | size_t buffer_len; | ||||
| Text *ta; | Text *ta; | ||||
| char filepath_abs[FILE_MAX]; | char filepath_abs[FILE_MAX]; | ||||
| BLI_stat_t st; | BLI_stat_t st; | ||||
| BLI_strncpy(filepath_abs, file, FILE_MAX); | BLI_strncpy(filepath_abs, file, FILE_MAX); | ||||
| if (relpath) { /* can be NULL (bg mode) */ | if (relpath) { /* can be NULL (bg mode) */ | ||||
| BLI_path_abs(filepath_abs, relpath); | BLI_path_abs(filepath_abs, relpath); | ||||
| } | } | ||||
| buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len); | buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len); | ||||
| if (buffer == NULL) { | if (buffer == NULL) { | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs), 0); | ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs), 0); | ||||
| /* Texts have no user by default... Only the 'real' user flag. */ | /* Texts have no users by default... Set the fake user flag to ensure that this text block | ||||
| id_us_ensure_real(&ta->id); | * doesn't get deleted when cleaning up data blocks. | ||||
| * The user can toggle this flag off if they want this to be automatically cleaned up. */ | |||||
| id_us_min(&ta->id); | id_us_min(&ta->id); | ||||
| id_fake_user_set(&ta->id); | |||||
Done Inline ActionsSame remarks as above, can even be fully removed here (doubling with the note in the function's docstring). mont29: Same remarks as above, can even be fully removed here (doubling with the note in the function's… | |||||
| BLI_listbase_clear(&ta->lines); | BLI_listbase_clear(&ta->lines); | ||||
| ta->curl = ta->sell = NULL; | ta->curl = ta->sell = NULL; | ||||
| if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0) { | if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0) { | ||||
| ta->flags = TXT_TABSTOSPACES; | ta->flags = TXT_TABSTOSPACES; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 992 Lines • Show Last 20 Lines | |||||
you can even revert those two blocks, and create/assign the Text pointer after you checked ID users
(unrelated to that patch, but potential cleanup/refactor would be to move that check to generic calling code actually...)